pub struct TableState<T: TableRow> { /* private fields */ }Expand description
State for a Table component.
Holds the rows, columns, selection state, and sort configuration.
Implementations§
Source§impl<T: TableRow> TableState<T>
impl<T: TableRow> TableState<T>
Sourcepub fn new(rows: Vec<T>, columns: Vec<Column>) -> Self
pub fn new(rows: Vec<T>, columns: Vec<Column>) -> Self
Creates a new table state with the given rows and columns.
If there are rows, the first row is selected by default.
§Example
use envision::component::{Column, TableRow, TableState};
use ratatui::layout::Constraint;
#[derive(Clone)]
struct Item { name: String }
impl TableRow for Item {
fn cells(&self) -> Vec<String> {
vec![self.name.clone()]
}
}
let state = TableState::new(
vec![Item { name: "A".into() }, Item { name: "B".into() }],
vec![Column::new("Name", Constraint::Length(10))],
);
assert_eq!(state.len(), 2);
assert_eq!(state.selected_index(), Some(0));Sourcepub fn with_selected(
rows: Vec<T>,
columns: Vec<Column>,
selected: usize,
) -> Self
pub fn with_selected( rows: Vec<T>, columns: Vec<Column>, selected: usize, ) -> Self
Creates a table state with a specific row selected.
The index is clamped to the valid range.
Sourcepub fn selected_index(&self) -> Option<usize>
pub fn selected_index(&self) -> Option<usize>
Returns the currently selected display index.
This is the index in the display order, not the original row index.
Sourcepub fn selected_row(&self) -> Option<&T>
pub fn selected_row(&self) -> Option<&T>
Returns a reference to the currently selected row.
Returns None if no row is selected or the table is empty.
Sourcepub fn sort(&self) -> Option<(usize, SortDirection)>
pub fn sort(&self) -> Option<(usize, SortDirection)>
Returns the current sort configuration.
Returns None if no sort is applied.
Sourcepub fn set_rows(&mut self, rows: Vec<T>)
pub fn set_rows(&mut self, rows: Vec<T>)
Sets the rows, resetting sort and adjusting selection.
If there were rows selected, the selection is preserved if valid, otherwise clamped to the last row.
Sourcepub fn set_selected(&mut self, index: Option<usize>)
pub fn set_selected(&mut self, index: Option<usize>)
Sets the selected row by display index.
Pass None to clear the selection.
Out of bounds indices are ignored.
Sourcepub fn is_disabled(&self) -> bool
pub fn is_disabled(&self) -> bool
Returns true if the table is disabled.
Sourcepub fn set_disabled(&mut self, disabled: bool)
pub fn set_disabled(&mut self, disabled: bool)
Sets the disabled state.
Disabled tables do not respond to messages.
Trait Implementations§
Source§impl<T: Clone + TableRow> Clone for TableState<T>
impl<T: Clone + TableRow> Clone for TableState<T>
Source§fn clone(&self) -> TableState<T>
fn clone(&self) -> TableState<T>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl<T> Freeze for TableState<T>
impl<T> RefUnwindSafe for TableState<T>where
T: RefUnwindSafe,
impl<T> Send for TableState<T>where
T: Send,
impl<T> Sync for TableState<T>where
T: Sync,
impl<T> Unpin for TableState<T>where
T: Unpin,
impl<T> UnwindSafe for TableState<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more