Skip to main content

TableState

Struct TableState 

Source
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>

Source

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));
Source

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.

Source

pub fn rows(&self) -> &[T]

Returns a reference to the rows.

Source

pub fn columns(&self) -> &[Column]

Returns a reference to the columns.

Source

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.

Source

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.

Source

pub fn sort(&self) -> Option<(usize, SortDirection)>

Returns the current sort configuration.

Returns None if no sort is applied.

Source

pub fn len(&self) -> usize

Returns the number of rows.

Source

pub fn is_empty(&self) -> bool

Returns true if the table has no rows.

Source

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.

Source

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.

Source

pub fn is_disabled(&self) -> bool

Returns true if the table is disabled.

Source

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>

Source§

fn clone(&self) -> TableState<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + TableRow> Debug for TableState<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: TableRow> Default for TableState<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto 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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.