Struct ratatui::widgets::TableState
source · pub struct TableState { /* private fields */ }
Expand description
State of a Table
widget
This state can be used to scroll through the rows and select one of them. When the table is
rendered as a stateful widget, the selected row will be highlighted and the table will be
shifted to ensure that the selected row is visible. This will modify the TableState
object
passed to the Frame::render_stateful_widget
method.
The state consists of two fields:
offset
: the index of the first row to be displayedselected
: the index of the selected row, which can beNone
if no row is selected
See the table`` example and the
recipe and `traceroute
tabs in the demo2 example in the
Examples directory for a more in depth example of the various configuration options and for
how to handle state.
§Example
let table = Table::new(rows, widths).widths(widths);
// Note: TableState should be stored in your application state (not constructed in your render
// method) so that the selected row is preserved across renders
let mut table_state = TableState::default();
*table_state.offset_mut() = 1; // display the second row and onwards
table_state.select(Some(3)); // select the forth row (0-indexed)
frame.render_stateful_widget(table, area, &mut table_state);
Note that if Table::widths
is not called before rendering, the rendered columns will have
equal width.
Implementations§
source§impl TableState
impl TableState
sourcepub const fn with_offset(self, offset: usize) -> Self
pub const fn with_offset(self, offset: usize) -> Self
Sets the index of the first row to be displayed
This is a fluent setter method which must be chained or used as it consumes self
§Examples
let state = TableState::new().with_offset(1);
sourcepub fn with_selected<T>(self, selected: T) -> Self
pub fn with_selected<T>(self, selected: T) -> Self
Sets the index of the selected row
This is a fluent setter method which must be chained or used as it consumes self
§Examples
let state = TableState::new().with_selected(Some(1));
Examples found in repository?
More examples
150 151 152 153 154 155 156 157 158 159 160 161 162 163
fn render_ingredients(selected_row: usize, area: Rect, buf: &mut Buffer) {
let mut state = TableState::default().with_selected(Some(selected_row));
let rows = INGREDIENTS.iter().copied();
let theme = THEME.recipe;
StatefulWidget::render(
Table::new(rows, [Constraint::Length(7), Constraint::Length(30)])
.block(Block::new().style(theme.ingredients))
.header(Row::new(vec!["Qty", "Ingredient"]).style(theme.ingredients_header))
.highlight_style(Style::new().light_yellow()),
area,
buf,
&mut state,
);
}
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
fn render_hops(selected_row: usize, area: Rect, buf: &mut Buffer) {
let mut state = TableState::default().with_selected(Some(selected_row));
let rows = HOPS
.iter()
.map(|hop| Row::new(vec![hop.host, hop.address]))
.collect_vec();
let block = Block::new()
.padding(Padding::new(1, 1, 1, 1))
.title_alignment(Alignment::Center)
.title("Traceroute bad.horse".bold().white());
StatefulWidget::render(
Table::new(rows, [Constraint::Max(100), Constraint::Length(15)])
.header(Row::new(vec!["Host", "Address"]).set_style(THEME.traceroute.header))
.highlight_style(THEME.traceroute.selected)
.block(block),
area,
buf,
&mut state,
);
let mut scrollbar_state = ScrollbarState::default()
.content_length(HOPS.len())
.position(selected_row);
let area = Rect {
width: area.width + 1,
y: area.y + 3,
height: area.height - 4,
..area
};
Scrollbar::default()
.orientation(ScrollbarOrientation::VerticalLeft)
.begin_symbol(None)
.end_symbol(None)
.track_symbol(None)
.thumb_symbol("▌")
.render(area, buf, &mut scrollbar_state);
}
sourcepub const fn offset(&self) -> usize
pub const fn offset(&self) -> usize
Index of the first row to be displayed
§Examples
let state = TableState::new();
assert_eq!(state.offset(), 0);
sourcepub fn offset_mut(&mut self) -> &mut usize
pub fn offset_mut(&mut self) -> &mut usize
Mutable reference to the index of the first row to be displayed
§Examples
let mut state = TableState::default();
*state.offset_mut() = 1;
sourcepub const fn selected(&self) -> Option<usize>
pub const fn selected(&self) -> Option<usize>
Index of the selected row
Returns None
if no row is selected
§Examples
let state = TableState::new();
assert_eq!(state.selected(), None);
Examples found in repository?
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
pub fn next(&mut self) {
let i = match self.state.selected() {
Some(i) => {
if i >= self.items.len() - 1 {
0
} else {
i + 1
}
}
None => 0,
};
self.state.select(Some(i));
self.scroll_state = self.scroll_state.position(i * ITEM_HEIGHT);
}
pub fn previous(&mut self) {
let i = match self.state.selected() {
Some(i) => {
if i == 0 {
self.items.len() - 1
} else {
i - 1
}
}
None => 0,
};
self.state.select(Some(i));
self.scroll_state = self.scroll_state.position(i * ITEM_HEIGHT);
}
sourcepub fn selected_mut(&mut self) -> &mut Option<usize>
pub fn selected_mut(&mut self) -> &mut Option<usize>
Mutable reference to the index of the selected row
Returns None
if no row is selected
§Examples
let mut state = TableState::default();
*state.selected_mut() = Some(1);
sourcepub fn select(&mut self, index: Option<usize>)
pub fn select(&mut self, index: Option<usize>)
Sets the index of the selected row
Set to None
if no row is selected. This will also reset the offset to 0
.
§Examples
let mut state = TableState::default();
state.select(Some(1));
Examples found in repository?
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
pub fn next(&mut self) {
let i = match self.state.selected() {
Some(i) => {
if i >= self.items.len() - 1 {
0
} else {
i + 1
}
}
None => 0,
};
self.state.select(Some(i));
self.scroll_state = self.scroll_state.position(i * ITEM_HEIGHT);
}
pub fn previous(&mut self) {
let i = match self.state.selected() {
Some(i) => {
if i == 0 {
self.items.len() - 1
} else {
i - 1
}
}
None => 0,
};
self.state.select(Some(i));
self.scroll_state = self.scroll_state.position(i * ITEM_HEIGHT);
}
Trait Implementations§
source§impl Clone for TableState
impl Clone for TableState
source§fn clone(&self) -> TableState
fn clone(&self) -> TableState
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for TableState
impl Debug for TableState
source§impl Default for TableState
impl Default for TableState
source§fn default() -> TableState
fn default() -> TableState
source§impl<'de> Deserialize<'de> for TableState
impl<'de> Deserialize<'de> for TableState
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl Hash for TableState
impl Hash for TableState
source§impl PartialEq for TableState
impl PartialEq for TableState
source§fn eq(&self, other: &TableState) -> bool
fn eq(&self, other: &TableState) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl Serialize for TableState
impl Serialize for TableState
impl Eq for TableState
impl StructuralPartialEq for TableState
Auto Trait Implementations§
impl Freeze for TableState
impl RefUnwindSafe for TableState
impl Send for TableState
impl Sync for TableState
impl Unpin for TableState
impl UnwindSafe for TableState
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<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
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