Skip to main content

DataGridState

Struct DataGridState 

Source
pub struct DataGridState<T: TableRow> { /* private fields */ }
Expand description

State for a DataGrid component.

Contains the table data, column cursor, and inline editor state.

Implementations§

Source§

impl<T: TableRow> DataGridState<T>

Source

pub fn new(rows: Vec<T>, columns: Vec<Column>) -> Self

Creates a new data grid with the given rows and columns.

The first row is selected by default.

§Example
use envision::component::{DataGridState, TableRow, Column};
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 = DataGridState::new(
    vec![Item { name: "A".into() }],
    vec![Column::new("Name", Constraint::Min(10))],
);
assert_eq!(state.selected_index(), Some(0));
assert_eq!(state.selected_column(), 0);
Source

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

Returns the rows.

§Example
use envision::component::{DataGridState, TableRow, Column};
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 = DataGridState::new(
    vec![Item { name: "A".into() }],
    vec![Column::new("Name", Constraint::Min(10))],
);
assert_eq!(state.rows().len(), 1);
Source

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

Returns the columns.

§Example
use envision::component::{DataGridState, TableRow, Column};
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 = DataGridState::new(
    vec![Item { name: "A".into() }],
    vec![Column::new("Name", Constraint::Min(10))],
);
assert_eq!(state.columns().len(), 1);
assert_eq!(state.columns()[0].header(), "Name");
Source

pub fn selected_index(&self) -> Option<usize>

Returns the currently selected row index.

Source

pub fn selected(&self) -> Option<usize>

Alias for selected_index().

§Example
use envision::component::{DataGridState, TableRow, Column};
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 = DataGridState::new(
    vec![Item { name: "A".into() }],
    vec![Column::new("Name", Constraint::Min(10))],
);
assert_eq!(state.selected(), Some(0));
Source

pub fn selected_row(&self) -> Option<&T>

Returns a reference to the currently selected row.

§Example
use envision::component::{DataGridState, TableRow, Column};
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 = DataGridState::new(
    vec![Item { name: "Alice".into() }],
    vec![Column::new("Name", Constraint::Min(10))],
);
assert_eq!(state.selected_row().unwrap().name, "Alice");
Source

pub fn selected_item(&self) -> Option<&T>

Returns a reference to the currently selected item.

§Example
use envision::component::{DataGridState, TableRow, Column};
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 = DataGridState::new(
    vec![Item { name: "Bob".into() }],
    vec![Column::new("Name", Constraint::Min(10))],
);
assert_eq!(state.selected_item().unwrap().name, "Bob");
Source

pub fn set_selected(&mut self, index: Option<usize>)

Sets the selected row index.

The index is clamped to the valid range. Has no effect on empty grids. Cancels any active edit.

§Example
use envision::component::{DataGridState, TableRow, Column};
use ratatui::layout::Constraint;

#[derive(Clone)]
struct Item { name: String }
impl TableRow for Item {
    fn cells(&self) -> Vec<String> { vec![self.name.clone()] }
}

let mut state = DataGridState::new(
    vec![Item { name: "A".into() }, Item { name: "B".into() }],
    vec![Column::new("Name", Constraint::Min(10))],
);
state.set_selected(Some(1));
assert_eq!(state.selected_index(), Some(1));
Source

pub fn selected_column(&self) -> usize

Returns the currently selected column index.

Source

pub fn is_editing(&self) -> bool

Returns true if a cell is currently being edited.

§Example
use envision::component::{DataGridState, TableRow, Column};
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 = DataGridState::new(
    vec![Item { name: "A".into() }],
    vec![Column::new("Name", Constraint::Min(10))],
);
assert!(!state.is_editing());
Source

pub fn editor_value(&self) -> &str

Returns the current editor value (while editing).

§Example
use envision::component::{DataGridState, TableRow, Column};
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 = DataGridState::new(
    vec![Item { name: "A".into() }],
    vec![Column::new("Name", Constraint::Min(10))],
);
assert_eq!(state.editor_value(), "");
Source

pub fn current_cell_value(&self) -> Option<String>

Returns the value of the currently selected cell.

§Example
use envision::component::{DataGridState, TableRow, Column};
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 = DataGridState::new(
    vec![Item { name: "Alice".into() }],
    vec![Column::new("Name", Constraint::Min(10))],
);
assert_eq!(state.current_cell_value(), Some("Alice".to_string()));
Source

pub fn row_count(&self) -> usize

Returns the number of rows.

§Example
use envision::component::{DataGridState, TableRow, Column};
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 = DataGridState::new(
    vec![Item { name: "A".into() }, Item { name: "B".into() }],
    vec![Column::new("Name", Constraint::Min(10))],
);
assert_eq!(state.row_count(), 2);
Source

pub fn column_count(&self) -> usize

Returns the number of columns.

§Example
use envision::component::{DataGridState, TableRow, Column};
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 = DataGridState::new(
    vec![Item { name: "A".into() }],
    vec![
        Column::new("Name", Constraint::Min(10)),
        Column::new("Value", Constraint::Min(5)),
    ],
);
assert_eq!(state.column_count(), 2);
Source

pub fn is_empty(&self) -> bool

Returns true if the grid has no rows.

§Example
use envision::component::{DataGridState, TableRow, Column};

#[derive(Clone)]
struct Item { name: String }
impl TableRow for Item {
    fn cells(&self) -> Vec<String> { vec![self.name.clone()] }
}

let state: DataGridState<Item> = DataGridState::default();
assert!(state.is_empty());
Source

pub fn set_rows(&mut self, rows: Vec<T>)

Sets the rows, resetting selection and cancelling any edit.

§Example
use envision::component::{DataGridState, TableRow, Column};
use ratatui::layout::Constraint;

#[derive(Clone)]
struct Item { name: String }
impl TableRow for Item {
    fn cells(&self) -> Vec<String> { vec![self.name.clone()] }
}

let mut state = DataGridState::new(
    vec![Item { name: "A".into() }],
    vec![Column::new("Name", Constraint::Min(10))],
);
state.set_rows(vec![Item { name: "X".into() }, Item { name: "Y".into() }]);
assert_eq!(state.row_count(), 2);
Source§

impl<T: TableRow + 'static> DataGridState<T>

Source

pub fn update(&mut self, msg: DataGridMessage) -> Option<DataGridOutput<T>>

Updates the state with a message, returning any output.

Trait Implementations§

Source§

impl<T: Clone + TableRow> Clone for DataGridState<T>

Source§

fn clone(&self) -> DataGridState<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 DataGridState<T>

Source§

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

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

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

Source§

fn default() -> Self

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

impl<'de, T> Deserialize<'de> for DataGridState<T>
where T: Deserialize<'de> + TableRow,

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T: TableRow + PartialEq> PartialEq for DataGridState<T>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T> Serialize for DataGridState<T>
where T: Serialize + TableRow,

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<T> Freeze for DataGridState<T>

§

impl<T> !RefUnwindSafe for DataGridState<T>

§

impl<T> Send for DataGridState<T>
where T: Send,

§

impl<T> Sync for DataGridState<T>
where T: Sync,

§

impl<T> Unpin for DataGridState<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for DataGridState<T>

§

impl<T> !UnwindSafe for DataGridState<T>

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> StateExt for T

Source§

fn updated(self, cmd: Command<impl Clone>) -> UpdateResult<Self, impl Clone>

Updates self and returns a command.
Source§

fn unchanged(self) -> UpdateResult<Self, ()>

Returns self with no command.
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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,