Skip to main content

TableState

Struct TableState 

Source
pub struct TableState {
    pub headers: Vec<String>,
    pub rows: Vec<Vec<String>>,
    pub selected: usize,
    pub multi_selected: HashSet<usize>,
    pub sort_column: Option<usize>,
    pub sort_ascending: bool,
    pub filter: String,
    pub page: usize,
    pub page_size: usize,
    pub zebra: bool,
    /* private fields */
}
Expand description

State for a data table widget.

Pass a mutable reference to Context::table each frame. Up/Down arrow keys move the row selection when the widget is focused. Column widths are computed automatically from header and cell content, or constrained per column via column_widths_spec.

Multi-row selection (Space / Shift+Up/Down / Ctrl+Space and modifier clicks) is tracked in multi_selected; the selected field always remains the focused/cursor row.

Fields§

§headers: Vec<String>

Column header labels.

§rows: Vec<Vec<String>>

Table rows, each a Vec of cell strings.

§selected: usize

Focused/cursor row (view index). Unchanged single-select semantics.

§multi_selected: HashSet<usize>

Multi-row selection as view indices. Empty means no multi-selection.

Available since v0.21.0.

§sort_column: Option<usize>

Sorted column index (None means no sorting).

§sort_ascending: bool

Sort direction (true for ascending).

§filter: String

Case-insensitive substring filter applied across all cells.

§page: usize

Current page (0-based) when pagination is enabled.

§page_size: usize

Rows per page (0 disables pagination).

§zebra: bool

Whether alternating row backgrounds are enabled.

Implementations§

Source§

impl TableState

Source

pub fn new( headers: Vec<impl Into<String>>, rows: Vec<Vec<impl Into<String>>>, ) -> Self

Create a table with headers and rows. Column widths are computed immediately.

Source

pub fn set_rows(&mut self, rows: Vec<Vec<impl Into<String>>>)

Replace all rows, preserving the selection index if possible.

If the current selection is beyond the new row count, it is clamped to the last row.

Source

pub fn toggle_sort(&mut self, column: usize)

Sort by a specific column index. If already sorted by this column, toggles direction.

Source

pub fn sort_by(&mut self, column: usize)

Sort by column without toggling (always sets to ascending first).

Source

pub fn set_filter(&mut self, filter: impl Into<String>)

Set the filter string. Multiple space-separated tokens are AND’d together — all tokens must match across any cell in the same row. Empty string disables filtering.

Source

pub fn clear_sort(&mut self)

Clear sorting.

Source

pub fn next_page(&mut self)

Move to the next page. Does nothing if already on the last page.

Source

pub fn prev_page(&mut self)

Move to the previous page. Does nothing if already on page 0.

Source

pub fn total_pages(&self) -> usize

Total number of pages based on filtered rows and page_size. Returns 1 if page_size is 0.

Source

pub fn visible_indices(&self) -> &[usize]

Get the visible row indices after filtering and sorting (used internally by table()).

Source

pub fn selected_row(&self) -> Option<&[String]>

Get the currently selected row data, or None if the table is empty.

Source

pub fn column_widths_spec(&mut self, specs: &[TableColumn])

Set the per-column width policy.

The slice is index-aligned with headers; a shorter slice leaves trailing columns at TableColumn::Auto. Passing an empty slice resets every column to automatic sizing.

Available since v0.21.0.

§Example
use slt::{TableColumn, widgets::TableState};
let mut table = TableState::new(
    vec!["Name", "Note"],
    vec![vec!["a", "a very long note that should be capped"]],
);
table.column_widths_spec(&[TableColumn::Fixed(6), TableColumn::Max(10)]);
ui.table(&mut table);
Source

pub fn selected_rows(&self) -> Vec<&[String]>

Return the multi-selected rows in ascending view order.

View indices are resolved against the current sort/filter view, so the returned slices reflect what the user sees. Stale indices (beyond the current view) are skipped.

Available since v0.21.0.

§Example
use slt::widgets::TableState;
let mut table = TableState::new(
    vec!["Name"],
    vec![vec!["a"], vec!["b"]],
);
ui.table(&mut table);
for row in table.selected_rows() {
    let _ = row;
}
Source

pub fn is_row_selected(&self, view_idx: usize) -> bool

Returns true if the row at view_idx is in the multi-selection set.

Available since v0.21.0.

§Example
use slt::widgets::TableState;
let mut table = TableState::new(vec!["Name"], vec![vec!["a"]]);
ui.table(&mut table);
let _ = table.is_row_selected(0);
Source

pub fn clear_selection(&mut self)

Clear the multi-selection set and the range anchor.

The focused selected cursor row is unaffected.

Available since v0.21.0.

§Example
use slt::widgets::TableState;
let mut table = TableState::new(vec!["Name"], vec![vec!["a"]]);
ui.table(&mut table);
table.clear_selection();

Trait Implementations§

Source§

impl Clone for TableState

Source§

fn clone(&self) -> TableState

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for TableState

Source§

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

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

impl Default for TableState

Source§

fn default() -> Self

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

Auto Trait Implementations§

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