iced-cube

Data

Data table

Records in rows and columns that readers can sort, search, filter, select and page through, and that turn into cards on a phone.

use iced_cube::data::data_table::{self, Align, Event, Output, State, data_table};
API reference
Payments: Sortable headers, a search box, a status filter, a Columns menu, status badges and right-aligned amounts.Payments: Sortable headers, a search box, a status filter, a Columns menu, status badges and right-aligned amounts.
Static preview
Payments. Sortable headers, a search box, a status filter, a Columns menu, status badges and right-aligned amounts.

Usage

A data table shows records that share the same fields, such as payments or invoices. State is generic over your own row type: the columns read each row through plain functions, so sorting, searching and filtering work on your data without copying it into strings first. The app owns the state, routes the table’s events into it and acts on the outputs.

Pressing a header sorts by that column, then reverses the order, then restores the original order. The search box looks through every visible searchable column, and a filterable column adds a select of its values to the toolbar. The Columns menu hides and shows columns, but always leaves one. The footer changes the page size and turns the pages, and only the rows of the current page that are in view are built, so a table of thousands of rows stays quick.

With selection on, every row has a checkbox and the header has one that selects or clears the rows on the current page only. The selection belongs to row keys, so it survives sorting, filtering and turning pages. Build bulk actions from state.selected() next to the table, as the selection example does.

For actions on one row, pass one context_menu::State for every row with .context_menu. It opens on a right-click or a long press, and .row_actions(true) adds a visible button for touch screens. While the table has focus, Shift+F10 opens the menu on the highlighted row. The row the menu opened on carries the ring while it is open.

The table takes focus when pressed and resolves its keys only then, so its arrow keys, Space and Escape never clash with other components. The highlight moves on to the next page after the last row, and scrolls into view in a table with a fixed height.

Below its breakpoint the table becomes a stack of cards, one per row: the first column heads each card, and the other columns become labelled lines. A select above the cards takes over from the sortable headers. Nothing scrolls sideways or runs off the edge of a phone screen.

Examples

Pagination

243 invoices in pages of 10, 25, 50 or 100. The rows scroll under a header that stays put, and only the rows in view are built.

Pagination: 243 invoices in pages of 10, 25, 50 or 100. The rows scroll under a header that stays put, and only the rows in view are built.Pagination: 243 invoices in pages of 10, 25, 50 or 100. The rows scroll under a header that stays put, and only the rows in view are built.
Static preview

Selection and bulk actions

A checkbox on every row, and a header checkbox that selects or clears the rows on the page. Buttons above act on the selection.

Selection and bulk actions: A checkbox on every row, and a header checkbox that selects or clears the rows on the page. Buttons above act on the selection.Selection and bulk actions: A checkbox on every row, and a header checkbox that selects or clears the rows on the page. Buttons above act on the selection.
Static preview

Row actions

One context menu shared by every row, opened by a right-click, a long press, the row's menu button or Shift+F10. The progress column is a custom cell.

Row actions: One context menu shared by every row, opened by a right-click, a long press, the row's menu button or Shift+F10. The progress column is a custom cell.Row actions: One context menu shared by every row, opened by a right-click, a long press, the row's menu button or Shift+F10. The progress column is a custom cell.
Static preview

Loading

Placeholder rows while the data loads. The switch toggles them.

Loading: Placeholder rows while the data loads. The switch toggles them.Loading: Placeholder rows while the data loads. The switch toggles them.
Static preview

Empty

A message in place of rows when nothing matches, with a button that clears the search.

Empty: A message in place of rows when nothing matches, with a button that clears the search.Empty: A message in place of rows when nothing matches, with a button that clears the search.
Static preview

Keyboard shortcuts

The default keymap with J and K added. The table resolves keys only while it has focus, and the highlight moves on to the next page after the last row.

Keyboard shortcuts: The default keymap with J and K added. The table resolves keys only while it has focus, and the highlight moves on to the next page after the last row.Keyboard shortcuts: The default keymap with J and K added. The table resolves keys only while it has focus, and the highlight moves on to the next page after the last row.
Static preview

API

ItemDescription
State::new(columns, rows, key)Holds your own row type, the columns that read it, and a key function giving each row an identity that survives sorting, such as its id.
data_table::column(id, header, value)A sortable, searchable and hideable column. value reads a Value (text, a number or empty) from a row. Add .width(length), .align(Align::End), .format(fn) for the text shown, .filterable(true) for a filter of its values, .sortable(false), .hideable(false) or .searchable(false).
.with_selection(bool) / .with_page_sizes(sizes) / .with_sort(id, direction) / .with_hidden(ids)Row checkboxes, the page sizes offered (the first is used), a starting sort and columns hidden to start with.
state.update(Event)Handles sorting, searching, filters, pages, selection, the keyboard and the Columns menu. Returns Output::Activated(key) or Output::Selected(keys).
state.page_rows() / state.selected() / state.highlighted()The rows on the current page, every selected key in row order, and the row the keyboard acts on.
state.set_rows(rows) / state.push(row) / state.update_row(&key, f) / state.retain(f)Changes the rows. The sort, filters and the selection of rows that remain are kept.
state.showing() / state.page() / state.page_count() / state.filtered_len()The footer text, such as "Showing 1-10 of 243", and the numbers behind it.
data_table(&state)Renders the toolbar, the table and the footer. Controls render disabled until .on_event is set.
.cell(id, f)Draws a column's cells with your own element, such as a badge or a progress bar.
.toolbar(bool) / .placeholder(text)Shows or hides the search box, column filters and Columns menu, and sets the search placeholder.
.loading(bool) / .empty(text)Placeholder rows while loading, and the message when nothing matches.
.height(length) / .row_height(f32)A fixed or filling height scrolls the rows under a header that stays put. Rows are 44 pixels tall unless set, such as 32 for a compact table.
.breakpoint(width)Below this width the rows become cards. Defaults to 560.
.context_menu(&menu, f) / .row_actions(bool)Gives every row the same context menu, keyed by row key, and a button at the end of each row that opens it.
.keymap(keymap) / .context_menu_keymap(keymap)Replaces the table's or the context menu's default keymap.
data_table::default_keymap() / action.event(&state)The default shortcuts, and the Event a resolved Action sends. State::key_event does both.
data_table::row_style(tokens, Status)The row colours: muted when selected, lighter under the pointer and a ring for the keyboard highlight.

Keyboard

Default shortcuts. Every one can be changed or removed with a keymap; see Keyboard shortcuts.

KeysAction
ArrowDownNext Highlights the next row, going on to the next page after the last.
ArrowUpPrevious Highlights the previous row, going back a page before the first.
HomeFirst Highlights the first row on the page.
EndLast Highlights the last row on the page.
PageDownNextPage Shows the next page.
PageUpPreviousPage Shows the previous page.
SpaceSelect Selects or clears the highlighted row.
EnterActivate Activates the highlighted row.
Ctrl+ASelectAll Selects every row on the page.
EscapeClearSelection Clears the selection.
Esc