Skip to main content

Crate datagrid_core

Crate datagrid_core 

Source
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§

CellFocus
Which cell currently holds focus, in view coordinates.
ColumnFilter
The conditions on one column: all of them (AND) or any of them (OR).
ColumnId
A stable, human-readable column identifier.
ColumnSpec
Everything the core logic needs to know about one column.
Condition
One condition: an operator and its operands.
DistinctValues
The values of one column, each with how many rows hold it: what a value list in a filter menu offers to tick.
GridLocale
The texts and formats of one language.
GridQuery
Everything a server needs to produce one page of rows.
GridState
Everything the user can change about how rows are presented.
Page
One page of rows from a server, and how many rows match in total.
PageState
Which page of the filtered rows is shown.
RequestId
Identifies one request issued through a RequestTracker.
RequestTracker
Decides whether a response is still wanted.
Selection
The set of selected row keys, plus the anchor a shift-range extends from.
SortState
One entry of the sort. Within GridState::sort the position in the vector is the priority — earlier entries win.
View
The result of compute_view.

Enums§

CellAlign
Horizontal alignment of a cell’s content.
CellFormat
How a column’s value is turned into text.
CellOverflow
What happens to cell content wider than its column.
CellValue
A typed value read from one cell of a row.
ColumnWidth
How wide a column should be laid out.
FilterOp
How a condition compares a cell with its operand.
FilterValue
A value a filter compares against. The owned counterpart of CellValue, so that it can live in GridState and travel to a server.
NavKey
A navigation key press, already resolved from the raw keyboard event.
SelectionMode
How many rows the user may select.
SortDirection
The direction a column is sorted in.
TextCollation
How text is compared when sorting.
ValueKind
What kind of value a column holds, which decides the operators a filter menu offers and how typed text is read.

Constants§

DEFAULT_MIN_COLUMN_WIDTH
The narrowest a column can be resized to when it sets no min_width, in CSS pixels. Wide enough for a short label and the resize handle.
DEFAULT_REMOTE_PAGE_SIZE
Rows per page when a remote grid is not given a page size. A server must page, so “no paging” is not an option there.

Traits§

DataSource
Where a remote grid gets its rows from.
GridRow
A row the grid can identify across sorting, filtering and paging.

Functions§

compute_view
Filters, sorts and pages rows, in that order.
distinct_values
The distinct values of column among the rows that pass every other filter and the search.
navigate
Moves the focus according to the ARIA grid pattern.
offset_of
The offset of the first rendered row, in CSS pixels.
reveal_scroll_top
The scroll position that brings row index fully into view, or None when it already is.
rows_per_viewport
How many whole rows fit in the viewport; what PageUp and PageDown move by.
total_height
The total scrollable height of total_rows rows, in CSS pixels.
visible_range
Which rows a virtualized body should render.

Type Aliases§

FilterTextFn
Extracts the text a column filters and searches on.
SortKeyFn
The name ValueFn had while it was only used for sorting.
SortValue
The value a column sorts by.
ValueFn
Reads a column’s typed value from a row, borrowing from it where it can.