data_table: Add collapsible row groups to DataTable#2436
Conversation
|
Thanks for the PR! But this is not a real group design, so we can't merge it yet. A real group should come from the data. The user gives the group data (for example, group by a field like This PR does not do that. The group is hardcoded by hand — the demo just splits rows by position (every 6 rows), and the user must build the parent/children relation themselves. The group does not come from the data, so it is very confusing to read (row 0 becomes the parent of the next 5 rows, even though they are not related). So the group logic needs a redesign to be data-driven. Can you tell me what you want first: group by a field, or tree rows (a parent row that is also real data)? |
|
Got it. I'll redesign it to group by a field (e.g.,
|
1ed5054 to
c197d24
Compare
TableState::group_by(&[col_ix]) scans delegate.cell_text() to automatically build a group tree from flat data, rendering group header rows with expand/collapse toggles on the right side. - Multi-level grouping via group_by(&[1, 0]) with nested children - Group tree is cached and only recomputed when row count or columns change, avoiding per-frame cell_text() scanning - Delegate can customize toggle icon (render_row_toggle) and group header row (render_group_tr) - TableEvent::ToggleGroup emitted on expand/collapse - groups() accessor returns all group info
huacnlee
left a comment
There was a problem hiding this comment.
I'm sorry, but after reviewing the overall API design, I still don't think it's a good solution.
I can't point out every single error in the current PR implementation, but I think the underlying approach is flawed.
It appears that group already exists, but it's actually built on a specific implementation, which isn't good API design. For example, why is group handled internally? This makes DataTable manage too many things, which is very limiting.

Closes #2370
Add collapsible row groups to DataTable
Introduce row grouping with optional expand/collapse toggles via a new leftmost
toggle column. Three new TableDelegate methods (row_depth, row_children_count,
render_row_toggle) let delegates describe tree-structured data stored in a flat
row list.
TableState provides toggle_row_group, expand_all, and automatically computes
visible rows to skip collapsed descendants. Toggle column width is configurable
via DataTable::row_toggle_width().
Description
This PR adds collapsible row groups to
DataTable, enabling tree-likeexpand/collapse behavior for grouped data rows.
New TableDelegate methods (all with default implementations):
row_depth(row_ix)— returns the tree depth of a rowrow_children_count(row_ix)— returns the total number of descendant rowsfor a group header, or
Nonefor leaf rowsrender_row_toggle(row_ix, is_expanded)— renders the chevron toggle icon,customizable by the delegate
New TableState API:
row_groupable(bool)— enable/disable the feature, defaultfalsetoggle_row_group(row_ix)— programmatically expand/collapse a groupexpand_all()— expand all groupsNew DataTable builder:
row_toggle_width(px)— configure toggle column width, default 24pxHow it works:
row_children_count(total descendantsper group) and
row_depthTableStatemanages collapsed state in aHashSetand computes visiblerows via
compute_visible_rows()updates dynamically
existing row-header column pattern
Screenshot
Break Changes
None. All new trait methods have default implementations, and
row_groupabledefaults to
false.How to Test
cargo run▶/▼) icons in the left toggle column toexpand/collapse groups
Checklist
the guidelines.
is accurate.
cargo runfor story tests related to the changes.platform-specific)