Expand description
Framework-agnostic data grid logic.
This crate contains the pure, deterministic half of the data grid: sorting, filtering, paging, selection, virtualization math and keyboard navigation. It has no UI framework dependency and can be unit-tested and benchmarked on its own.
The Dioxus bindings live in the companion crate dioxus-datagrid.
§The shape of it
Rows stay where they are. compute_view takes a row slice, the column
specs and the current GridState, and returns a View of indices
into the original slice. Nothing is cloned, so a view over a hundred thousand
expensive rows costs a vector of usize.
use datagrid_core::{compute_view, ColumnSpec, GridState, GridRow};
#[derive(Clone)]
struct User {
id: u32,
name: String,
age: u32,
}
impl GridRow for User {
type Key = u32;
fn key(&self) -> u32 {
self.id
}
}
let rows = vec![
User { id: 1, name: "Zoe".into(), age: 30 },
User { id: 2, name: "adam".into(), age: 25 },
User { id: 3, name: "Mia".into(), age: 30 },
];
let columns = vec![
ColumnSpec::new("name")
.sort_by_text(|user: &User| user.name.as_str())
.filter_by(|user: &User| user.name.clone()),
ColumnSpec::new("age").sort_by_value(|user: &User| user.age),
];
let mut state = GridState::new();
state.toggle_sort("name", false);
let view = compute_view(&rows, &columns, &state);
// Sorting is case-insensitive by default, so "adam" comes first.
let names: Vec<&str> = view
.indices
.iter()
.filter_map(|&i| rows.get(i))
.map(|user| user.name.as_str())
.collect();
assert_eq!(names, ["adam", "Mia", "Zoe"]);Structs§
- Cell
Focus - Which cell currently holds focus, in view coordinates.
- Column
Id - A stable, human-readable column identifier.
- Column
Spec - Everything the core logic needs to know about one column.
- Grid
State - Everything the user can change about how rows are presented.
- Page
State - Which page of the filtered rows is shown.
- Selection
- The set of selected row keys, plus the anchor a shift-range extends from.
- Sort
State - One entry of the sort. Within
GridState::sortthe position in the vector is the priority — earlier entries win. - View
- The result of
compute_view.
Enums§
- Column
Width - How wide a column should be laid out.
- NavKey
- A navigation key press, already resolved from the raw keyboard event.
- Selection
Mode - How many rows the user may select.
- Sort
Direction - The direction a column is sorted in.
- Sort
Value - A value extracted from a row to sort by.
- Text
Collation - How text is compared when sorting.
Traits§
- GridRow
- A row the grid can identify across sorting, filtering and paging.
Functions§
- compute_
view - Filters, sorts and pages
rows, in that order. - navigate
- Moves the focus according to the ARIA grid pattern.
- offset_
of - The offset of the first rendered row, in CSS pixels.
- total_
height - The total scrollable height of
total_rowsrows, in CSS pixels. - visible_
range - Which rows a virtualized body should render.
Type Aliases§
- Filter
Text Fn - Extracts the text a column filters and searches on.
- Sort
KeyFn - Extracts the value a column sorts by, borrowed from the row.