Row grouping
Group rows by one or more columns into collapsible group rows. Each group row shows the value, the number
of rows in it, and a subtotal for every column that has an aggregation.
Loading demo…
const columns = [
{ accessorKey: 'region', header: 'Region' },
{ accessorKey: 'quarter', header: 'Quarter' },
{ accessorKey: 'amount', header: 'Amount', type: 'number', aggregation: 'sum' }, // group subtotal
];
<DataTable
columns={columns}
data={deals}
enableRowGrouping
initialState={{ grouping: ['region', 'quarter'] }} // outermost first
/>
Grouping from the UI and the API
- Column ⋮ menu: Group by this column adds the column as the innermost level; Stop grouping by this column removes it.
apiRef:
apiRef.current.grouping.setGrouping(['region']); // replace
apiRef.current.grouping.groupByColumn('quarter'); // add a level
apiRef.current.grouping.ungroupColumn('region'); // remove a level
apiRef.current.grouping.clearGrouping();
apiRef.current.grouping.expandAllGroups(true); // open / close every group
onRowGroupingChange(grouping) reports every change. The grouping is saved in
saved views and persistence, and reset by Reset
layout when you add 'grouping' to resetActions.
How it behaves
- Grouped columns move to the start of the grid, right after the checkbox column. Group rows show their value there; data rows leave those cells empty, since the group row above already says the value. Ungrouping restores your column order.
- Only columns with an
aggregationget a subtotal. Other columns stay empty on group rows, numbers included. - Subtotals use the same math as the footer totals: empty and non-numeric
values are skipped. Format them with a column's
aggregatedCellrenderer. Without one, the value is shown with thousands separators. - A group row is not a record. TanStack lends a group row its first child's data, so the grid never
passes group rows to your code: they get no row actions, no
onRowClick, no detail panel, and can't be edited. Other cells on a group row stay empty. - Selection: a group's checkbox selects every row in it (half-checked when some are). The header checkbox selects the rows in every visible group. Group rows themselves are never in the selection.
- Sorting, filtering, pagination keep working: filtering happens before grouping, and pages count the visible rows (group rows and the rows of open groups).
- Footer totals count the underlying rows, not the group rows.
- Export writes the data rows (flat), not group rows.
Client data only
Grouping is ignored for server data (dataMode="server" or server pagination): the grid holds one page of
rows, so groups and subtotals would only cover that page. Group on your backend and return
tree data instead.
Props
| Prop | Type | Description |
|---|---|---|
enableRowGrouping | boolean | Turn grouping on (client data). |
initialState.grouping | string[] | The grouped column ids, outermost first. |
onRowGroupingChange | (grouping: string[]) => void | The grouping changed. |
column aggregation | 'sum' | 'avg' | 'min' | 'max' | 'count' | (rows) => any | The group subtotal (and footer total). |
column aggregatedCell | (ctx) => ReactNode | Render a group row's subtotal yourself. |
column enableGrouping | boolean | false hides "Group by" for that column. |