pub struct TableView<T, H> { /* private fields */ }Expand description
View to select an item among a list, supporting multiple columns for sorting.
§Examples
// Provide a type for the table's columns
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
enum BasicColumn {
Name,
Count,
Rate
}
// Define the item type
#[derive(Clone, Debug)]
struct Foo {
name: String,
count: usize,
rate: usize
}
impl TableViewItem<BasicColumn> for Foo {
fn to_column(&self, column: BasicColumn) -> String {
match column {
BasicColumn::Name => self.name.to_string(),
BasicColumn::Count => format!("{}", self.count),
BasicColumn::Rate => format!("{}", self.rate)
}
}
fn cmp(&self, other: &Self, column: BasicColumn) -> Ordering where Self: Sized {
match column {
BasicColumn::Name => self.name.cmp(&other.name),
BasicColumn::Count => self.count.cmp(&other.count),
BasicColumn::Rate => self.rate.cmp(&other.rate)
}
}
}
// Configure the actual table
let table = TableView::<Foo, BasicColumn>::new()
.column(BasicColumn::Name, "Name", |c| c.width(20))
.column(BasicColumn::Count, "Count", |c| c.align(HAlign::Center))
.column(BasicColumn::Rate, "Rate", |c| {
c.ordering(Ordering::Greater).align(HAlign::Right).width(20)
})
.default_column(BasicColumn::Name);Implementations§
Source§impl<T, H> TableView<T, H>
impl<T, H> TableView<T, H>
Sourcepub fn set_items_stable(&mut self, items: Vec<T>)
pub fn set_items_stable(&mut self, items: Vec<T>)
Sets the contained items of the table.
The currently active sort order is preserved and will be applied to all items.
Compared to set_items, the current selection will be preserved.
(But this is only available for T: PartialEq.)
Source§impl<T, H> TableView<T, H>
impl<T, H> TableView<T, H>
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new empty TableView without any columns.
A TableView should be accompanied by a enum of type H representing
the table columns.
Sourcepub fn column<S: Into<String>, C: FnOnce(TableColumn<H>) -> TableColumn<H>>(
self,
column: H,
title: S,
callback: C,
) -> Self
pub fn column<S: Into<String>, C: FnOnce(TableColumn<H>) -> TableColumn<H>>( self, column: H, title: S, callback: C, ) -> Self
Adds a column for the specified table colum from type H along with
a title for its visual display.
The provided callback can be used to further configure the
created TableColumn.
Sourcepub fn add_column<S: Into<String>, C: FnOnce(TableColumn<H>) -> TableColumn<H>>(
&mut self,
column: H,
title: S,
callback: C,
)
pub fn add_column<S: Into<String>, C: FnOnce(TableColumn<H>) -> TableColumn<H>>( &mut self, column: H, title: S, callback: C, )
Adds a column for the specified table colum from type H along with
a title for its visual display.
The provided callback can be used to further configure the
created TableColumn.
Sourcepub fn remove_column(&mut self, i: usize)
pub fn remove_column(&mut self, i: usize)
Remove a column.
Sourcepub fn insert_column<S: Into<String>, C: FnOnce(TableColumn<H>) -> TableColumn<H>>(
&mut self,
i: usize,
column: H,
title: S,
callback: C,
)
pub fn insert_column<S: Into<String>, C: FnOnce(TableColumn<H>) -> TableColumn<H>>( &mut self, i: usize, column: H, title: S, callback: C, )
Adds a column for the specified table colum from type H along with
a title for its visual display.
The provided callback can be used to further configure the
created TableColumn.
Sourcepub fn default_column(self, column: H) -> Self
pub fn default_column(self, column: H) -> Self
Sets the initially active column of the table.
Sourcepub fn set_default_column(&mut self, column: H)
pub fn set_default_column(&mut self, column: H)
Sets the initially active column of the table.
Sourcepub fn sort_by(&mut self, column: H, order: Ordering)
pub fn sort_by(&mut self, column: H, order: Ordering)
Sorts the table using the specified table column and the passed
order.
Sourcepub fn order(&self) -> Option<(H, Ordering)>
pub fn order(&self) -> Option<(H, Ordering)>
Returns the currently active column that is used for sorting along with its ordering.
Might return None if there are currently no items in the table
and it has not been sorted yet.
Sourcepub fn set_enabled(&mut self, enabled: bool)
pub fn set_enabled(&mut self, enabled: bool)
Enable or disable this view.
Sourcepub fn is_enabled(&self) -> bool
pub fn is_enabled(&self) -> bool
Returns true if this view is enabled.
Sourcepub fn set_on_sort<F>(&mut self, cb: F)
pub fn set_on_sort<F>(&mut self, cb: F)
Sourcepub fn set_on_submit<F>(&mut self, cb: F)
pub fn set_on_submit<F>(&mut self, cb: F)
Sourcepub fn on_submit<F>(self, cb: F) -> Self
pub fn on_submit<F>(self, cb: F) -> Self
Sets a callback to be used when <Enter> is pressed while an item
is selected.
Both the currently selected row and the index of the corresponding item within the underlying storage vector will be given to the callback.
Chainable variant.
§Example
table.on_submit(|siv: &mut Cursive, row: usize, index: usize| {
});Sourcepub fn set_on_select<F>(&mut self, cb: F)
pub fn set_on_select<F>(&mut self, cb: F)
Sourcepub fn set_selected_row(&mut self, row_index: usize)
pub fn set_selected_row(&mut self, row_index: usize)
Selects the row at the specified index.
Sourcepub fn selected_row(self, row_index: usize) -> Self
pub fn selected_row(self, row_index: usize) -> Self
Selects the row at the specified index.
Chainable variant.
Sourcepub fn set_items(&mut self, items: Vec<T>)
pub fn set_items(&mut self, items: Vec<T>)
Sets the contained items of the table.
The currently active sort order is preserved and will be applied to all items.
Sourcepub fn items(self, items: Vec<T>) -> Self
pub fn items(self, items: Vec<T>) -> Self
Sets the contained items of the table.
The order of the items will be preserved even when the table is sorted.
Chainable variant.
Sourcepub fn borrow_item(&self, index: usize) -> Option<&T>
pub fn borrow_item(&self, index: usize) -> Option<&T>
Returns a immmutable reference to the item at the specified index within the underlying storage vector.
Sourcepub fn borrow_item_mut(&mut self, index: usize) -> Option<&mut T>
pub fn borrow_item_mut(&mut self, index: usize) -> Option<&mut T>
Returns a mutable reference to the item at the specified index within the underlying storage vector.
Sourcepub fn borrow_items(&mut self) -> &[T]
pub fn borrow_items(&mut self) -> &[T]
Returns a immmutable reference to the items contained within the table.
Sourcepub fn borrow_items_mut(&mut self) -> &mut [T]
pub fn borrow_items_mut(&mut self) -> &mut [T]
Returns a mutable reference to the items contained within the table.
Can be used to modify the items in place.
Sourcepub fn item(&self) -> Option<usize>
pub fn item(&self) -> Option<usize>
Returns the index of the currently selected item within the underlying storage vector.
Sourcepub fn set_selected_item(&mut self, item_index: usize)
pub fn set_selected_item(&mut self, item_index: usize)
Selects the item at the specified index within the underlying storage vector.
Sourcepub fn selected_item(self, item_index: usize) -> Self
pub fn selected_item(self, item_index: usize) -> Self
Selects the item at the specified index within the underlying storage vector.
Chainable variant.
Sourcepub fn insert_item(&mut self, item: T)
pub fn insert_item(&mut self, item: T)
Inserts a new item into the table.
The currently active sort order is preserved and will be applied to the newly inserted item.
If no sort option is set, the item will be added to the end of the table.
Sourcepub fn insert_item_at(&mut self, index: usize, item: T)
pub fn insert_item_at(&mut self, index: usize, item: T)
Inserts a new item into the table.
The currently active sort order is preserved and will be applied to the newly inserted item.
If no sort option is set, the item will be inserted at the given index.
§Panics
If index > self.len().
Sourcepub fn remove_item(&mut self, item_index: usize) -> Option<T>
pub fn remove_item(&mut self, item_index: usize) -> Option<T>
Removes the item at the specified index within the underlying storage vector and returns it.
Sourcepub fn take_items(&mut self) -> Vec<T>
pub fn take_items(&mut self) -> Vec<T>
Removes all items from the underlying storage and returns them.
Trait Implementations§
Source§impl<T, H> Default for TableView<T, H>
impl<T, H> Default for TableView<T, H>
Source§fn default() -> Self
fn default() -> Self
Creates a new empty TableView without any columns.
See TableView::new().
Source§impl<T, H> Scroller for TableView<T, H>
impl<T, H> Scroller for TableView<T, H>
Source§fn get_scroller_mut(&mut self) -> &mut Core
fn get_scroller_mut(&mut self) -> &mut Core
Source§fn get_scroller(&self) -> &Core
fn get_scroller(&self) -> &Core
Source§impl<T, H> View for TableView<T, H>
impl<T, H> View for TableView<T, H>
Source§fn draw(&self, printer: &Printer<'_, '_>)
fn draw(&self, printer: &Printer<'_, '_>)
Source§fn layout(&mut self, size: Vec2)
fn layout(&mut self, size: Vec2)
Source§fn take_focus(&mut self, _: Direction) -> Result<EventResult, CannotFocus>
fn take_focus(&mut self, _: Direction) -> Result<EventResult, CannotFocus>
Source§fn on_event(&mut self, event: Event) -> EventResult
fn on_event(&mut self, event: Event) -> EventResult
Source§fn important_area(&self, size: Vec2) -> Rect
fn important_area(&self, size: Vec2) -> Rect
Source§fn needs_relayout(&self) -> bool
fn needs_relayout(&self) -> bool
Source§fn required_size(&mut self, constraint: XY<usize>) -> XY<usize>
fn required_size(&mut self, constraint: XY<usize>) -> XY<usize>
Source§fn call_on_any(
&mut self,
_: &Selector<'_>,
_: &mut dyn FnMut(&mut (dyn View + 'static)),
)
fn call_on_any( &mut self, _: &Selector<'_>, _: &mut dyn FnMut(&mut (dyn View + 'static)), )
Source§fn focus_view(&mut self, _: &Selector<'_>) -> Result<EventResult, ViewNotFound>
fn focus_view(&mut self, _: &Selector<'_>) -> Result<EventResult, ViewNotFound>
Auto Trait Implementations§
impl<T, H> Freeze for TableView<T, H>
impl<T, H> !RefUnwindSafe for TableView<T, H>
impl<T, H> Send for TableView<T, H>
impl<T, H> Sync for TableView<T, H>
impl<T, H> Unpin for TableView<T, H>
impl<T, H> !UnwindSafe for TableView<T, H>
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> Finder for Twhere
T: View,
impl<T> Finder for Twhere
T: View,
Source§fn call_on_all<V, F>(&mut self, sel: &Selector<'_>, callback: F)
fn call_on_all<V, F>(&mut self, sel: &Selector<'_>, callback: F)
sel. Read moreSource§fn call_on<V, F, R>(&mut self, sel: &Selector<'_>, callback: F) -> Option<R>
fn call_on<V, F, R>(&mut self, sel: &Selector<'_>, callback: F) -> Option<R>
sel. Read moreSource§fn call_on_name<V, F, R>(&mut self, name: &str, callback: F) -> Option<R>
fn call_on_name<V, F, R>(&mut self, name: &str, callback: F) -> Option<R>
call_on with a view::Selector::Name.Source§impl<T> IntoBoxedView for Twhere
T: View,
impl<T> IntoBoxedView for Twhere
T: View,
Source§fn into_boxed_view(self) -> Box<dyn View>
fn into_boxed_view(self) -> Box<dyn View>
Box<View>.Source§impl<T> Resizable for Twhere
T: View,
impl<T> Resizable for Twhere
T: View,
Source§fn resized(
self,
width: SizeConstraint,
height: SizeConstraint,
) -> ResizedView<Self>
fn resized( self, width: SizeConstraint, height: SizeConstraint, ) -> ResizedView<Self>
self in a ResizedView with the given size constraints.Source§fn fixed_size<S>(self, size: S) -> ResizedView<Self>
fn fixed_size<S>(self, size: S) -> ResizedView<Self>
self into a fixed-size ResizedView.Source§fn fixed_width(self, width: usize) -> ResizedView<Self>
fn fixed_width(self, width: usize) -> ResizedView<Self>
self into a fixed-width ResizedView.Source§fn fixed_height(self, height: usize) -> ResizedView<Self>
fn fixed_height(self, height: usize) -> ResizedView<Self>
self into a fixed-width ResizedView.Source§fn full_screen(self) -> ResizedView<Self>
fn full_screen(self) -> ResizedView<Self>
self into a full-screen ResizedView.Source§fn full_width(self) -> ResizedView<Self>
fn full_width(self) -> ResizedView<Self>
self into a full-width ResizedView.Source§fn full_height(self) -> ResizedView<Self>
fn full_height(self) -> ResizedView<Self>
self into a full-height ResizedView.Source§fn max_size<S>(self, size: S) -> ResizedView<Self>
fn max_size<S>(self, size: S) -> ResizedView<Self>
self into a limited-size ResizedView.Source§fn max_width(self, max_width: usize) -> ResizedView<Self>
fn max_width(self, max_width: usize) -> ResizedView<Self>
self into a limited-width ResizedView.Source§fn max_height(self, max_height: usize) -> ResizedView<Self>
fn max_height(self, max_height: usize) -> ResizedView<Self>
self into a limited-height ResizedView.Source§fn min_size<S>(self, size: S) -> ResizedView<Self>
fn min_size<S>(self, size: S) -> ResizedView<Self>
self into a ResizedView at least sized size.Source§fn min_width(self, min_width: usize) -> ResizedView<Self>
fn min_width(self, min_width: usize) -> ResizedView<Self>
self in a ResizedView at least min_width wide.Source§fn min_height(self, min_height: usize) -> ResizedView<Self>
fn min_height(self, min_height: usize) -> ResizedView<Self>
self in a ResizedView at least min_height tall.Source§impl<T> Scrollable for Twhere
T: View,
impl<T> Scrollable for Twhere
T: View,
Source§fn scrollable(self) -> ScrollView<Self>
fn scrollable(self) -> ScrollView<Self>
self in a ScrollView.