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: usizeFocused/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: boolSort direction (true for ascending).
filter: StringCase-insensitive substring filter applied across all cells.
page: usizeCurrent page (0-based) when pagination is enabled.
page_size: usizeRows per page (0 disables pagination).
zebra: boolWhether alternating row backgrounds are enabled.
Implementations§
Source§impl TableState
impl TableState
Sourcepub fn new(
headers: Vec<impl Into<String>>,
rows: Vec<Vec<impl Into<String>>>,
) -> Self
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.
Sourcepub fn set_rows(&mut self, rows: Vec<Vec<impl Into<String>>>)
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.
Sourcepub fn toggle_sort(&mut self, column: usize)
pub fn toggle_sort(&mut self, column: usize)
Sort by a specific column index. If already sorted by this column, toggles direction.
Sourcepub fn sort_by(&mut self, column: usize)
pub fn sort_by(&mut self, column: usize)
Sort by column without toggling (always sets to ascending first).
Sourcepub fn set_filter(&mut self, filter: impl Into<String>)
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.
Sourcepub fn clear_sort(&mut self)
pub fn clear_sort(&mut self)
Clear sorting.
Sourcepub fn total_pages(&self) -> usize
pub fn total_pages(&self) -> usize
Total number of pages based on filtered rows and page_size. Returns 1 if page_size is 0.
Sourcepub fn visible_indices(&self) -> &[usize]
pub fn visible_indices(&self) -> &[usize]
Get the visible row indices after filtering and sorting (used internally by table()).
Sourcepub fn selected_row(&self) -> Option<&[String]>
pub fn selected_row(&self) -> Option<&[String]>
Get the currently selected row data, or None if the table is empty.
Sourcepub fn column_widths_spec(&mut self, specs: &[TableColumn])
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);Sourcepub fn selected_rows(&self) -> Vec<&[String]>
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;
}Sourcepub fn is_row_selected(&self, view_idx: usize) -> bool
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);Sourcepub fn clear_selection(&mut self)
pub fn clear_selection(&mut self)
Trait Implementations§
Source§impl Clone for TableState
impl Clone for TableState
Source§fn clone(&self) -> TableState
fn clone(&self) -> TableState
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more