Data Table
Powerful table and datagrids — sorting, filtering, pagination, row selection, column visibility and row actions.
Overview
shadcn 示例全特性:过滤 + 列可见性 + 排序 + 复选 + 分页 + 行操作。
Installation
Data Table 是单个 Rust 文件(src/data_table.rs)—— 过滤框复用 gpui-base 的 InputState,弹层走 deferred() overlay pass,排序箭头/复选标记/⋯ 全部 PathBuilder 手绘。
[dependencies]gpui-kit = "0.6.6"# data_table.rs is self-contained: filter uses gpui-base's# InputState, popups go through deferred(), all glyphs are# PathBuilder — no extra deps.# Download the runnable example crate, then:cargo run --locked -- --data-table# Other demos:cargo run --locked -- --data-table sortingcargo run --locked -- --data-table filteringcargo run --locked -- --data-table selectioncargo run --locked -- --data-table fullUsage
DataTable 是实体:DataColumn 定义列(固定宽或 flex、可排序、数值列右对齐),DataRow 提供 id + cells。所有特性都是 builder 开关 —— 与 TanStack 的 features 思路一致:不启用就不渲染。
use justdo_command::data_table::{ DataColumn, DataRow, DataTable, DataTableActionEvent,};let input = cx.new(|cx| { InputState::new(window, cx).placeholder("Filter emails…".to_string())});let table = cx.new(|cx| { DataTable::new( vec![ DataColumn::new("status", "Status").width(px(110.)), DataColumn::new("email", "Email").sortable(true), DataColumn::new("amount", "Amount").numeric(true).width(px(110.)), ], vec![ DataRow::new("728ed52f", vec![ "success".into(), "ken99@example.com".into(), "$316.00".into(), ]), // … ], cx, ) .filter_input(input, Some(1), cx) .selection(true) .column_visibility(true) .actions(&["Copy payment ID", "View customer"]) .paginate(5)});cx.subscribe(&table, |view, _, ev: &DataTableActionEvent, cx| { view.handle_row_action(ev.row.clone(), ev.action.clone()); cx.notify();});div().size_full().child(table)网页预览与桌面端运行同一份 Rust / GPUI 组件。
Composition
结构对应 Table → TableHeader + TableBody + TableFooter,外加工具栏(过滤框 + 列可见性菜单)。行渲染只画可见列;表头复选框对过滤结果做 全选/半选/未选 三态。
DataTable entity — columns, rows, every feature flag├── DataTableToolbar filter InputState + "Columns ▾" visibility menu│ .filter_input(input, Some(col), cx)│ .column_visibility(true)├── TableHeader .sortable(true) header → click cycles ↑/↓/原序│ ├── head checkbox .selection(true) — all/indeterminate over filtered│ └── ⋯ column shown when .actions(&[..]) is non-empty├── TableBody DataRow { id, cells } — hover + selected accent│ ├── row checkbox per-row toggle (stable by row index)│ ├── cells parallel to columns; .numeric(true) right-aligns│ └── row menu ⋯ → DataTableActionEvent { action, row }└── TableFooter "N of M selected" / "Page x of y" + Previous·Next .paginate(page_size)// Rows flow through three stages, each opt-in://// rows ──filter──▶ filtered ──sort──▶ sorted ──paginate──▶ page// 12 query column page_size 5//// filtered_indices() returns stable indices into rows, so a// checkbox keeps its identity no matter how the view reorders.//// Query changes reset to page 0; sort cycles none→asc→desc→none;// numeric columns parse digits out of "$1,240.00" → 1240.0.Interactions
Sorting
点击 sortable 列头循环 升序 → 降序 → 原序,箭头颜色区分激活态。数值列(numeric(true))从文本里提取数字比较 —— $1,240.00 > $316.00。
Popups
行操作菜单与 Columns 清单都锚定在表格自身 bounds(每帧 prepaint 捕获),滚动时位置实时校正;点击外部或再点 ⋯ 关闭。
// The ⋯ row menu and the Columns checklist are popups drawn// in the deferred pass, anchored against the table's own// bounds (captured each prepaint)://// row menu x = table right − MENU_W, y = row bottom − scroll// cols menu x = table right − COLS_W, y = toolbar bottom//// A full-size overlay swallows outside clicks → closes both.Accessibility
States
表头复选框三态(unchecked / indeterminate / checked)反映过滤后的选中比例;分页按钮在边界页半透明并禁用;空结果渲染 "No results." 占位行。
Honest visuals
排序方向、选中行、禁用按钮、开关状态全部由绘制态驱动 —— 不依赖 DOM 语义,状态也可从实体读取(selected_ids())。
API Reference
实体暴露的构造器、builder 方法与事件。
| API | Signature | Notes |
|---|---|---|
DataTable::new(..) | Vec<DataColumn>, Vec<DataRow> | Plain table — everything else is opt-in. |
DataColumn::new(..) | id, header | .sortable(..) .numeric(..) .width(..) |
DataRow::new(..) | id, Vec<SharedString> | cells parallel to columns. |
.filter_input(..) | Entity<InputState>, Option<col> | Toolbar search box; live substring filter. |
.selection(..) | bool | Checkbox column + footer count. |
.selected_ids() | -> Vec<SharedString> | Stable row ids, view-independent. |
.paginate(..) | page_size | Footer pager + page slice. |
.column_visibility(..) | bool | "Columns ▾" dropdown checklist. |
.actions(..) | &[&str] | ⋯ column + row menu items. |
DataTableActionEvent | { action, row } | Menu item label + row id. |
Source & inspiration
特性组合参考 shadcn/ui Data Table (TanStack Table) 。本页交互由 Rust / GPUI 绘制,基础原语来自 gpui-kit 。