Grouping Table
Group rows by column value, expand and collapse groups, and roll up sums and counts.
This example groups orders into region, status or calendar-month bands from the column actions menu. Use it to summarize large lists. It uses the same Group by and Stop grouping by pattern as MUI X and other data grids.
- Nested groups follow the order you group columns.
- Numeric columns can roll up with TanStack
aggregationFn. The demo sums Amount.
Preview with Controlled State
Introduction
Section titled “Introduction”Grouping folds rows that share a value into one expandable band. The group row can show a rollup (sum, count, and so on).
- Add Group by to a column’s actions menu. Users choose the grouping at runtime.
- Group by more than one column to nest bands.
- It uses TanStack Table’s
getGroupedRowModel. You add one registry piece and mount the option where you want it.
Installation
Section titled “Installation”pnpm dlx shadcn@latest add @niko-table/data-table @niko-table/data-table-column-group @niko-table/data-table-column-sort @niko-table/data-table-pagination @niko-table/data-table-search-filter @niko-table/data-table-view-menuFirst time using
@niko-table? See the Installation Guide to set up the registry.
Group rows
Section titled “Group rows”Nest DataTableGroupedRows in the body. That one line turns grouping on.
- It renders the group bands.
- It also turns on
enableGroupingandenableExpanding(the table detects it). You set no config flag and wire no renderer.
import { DataTableGroupedRows } from "@/components/niko-table/components/data-table-grouped-rows"
<DataTable> <DataTableHeader /> <DataTableBody> <DataTableGroupedRows /> </DataTableBody></DataTable>Leave it out and the table does not change. A grouped row model without DataTableGroupedRows renders no bands.
The built-in band shows a chevron, the group value and a · N rows count. It also shows one aggregated cell per column that defines aggregatedCell or aggregationFn.
Your own group row
Section titled “Your own group row”Nest your own component instead. It reads the group from useDataTableGroupRow(). You need no render prop or callback:
import { DataTableGroupedRows, useDataTableGroupRow,} from "@/components/niko-table/components/data-table-grouped-rows"
function RegionGroupRow() { const { groupLabel, count, isExpanded, toggle } = useDataTableGroupRow()
return ( <TableCell colSpan={4} onClick={toggle}> {groupLabel} — {count} orders {isExpanded ? "▾" : "▸"} </TableCell> )}
<DataTableBody> <DataTableGroupedRows> <RegionGroupRow /> </DataTableGroupedRows></DataTableBody>Your component renders the row’s cells. The body keeps the surrounding <TableRow>, so column widths, row striping and data attributes still line up with the rows below.
Two escape hatches:
enabledFor: a per-group check. Returnfalseand that band uses the built-in row.DataTableGroupedRowCellsis exported. A custom row can render the built-in cells for cases it does not handle itself.
Column menu: Group by
Section titled “Column menu: Group by”Add the group actions to the header menu like other column controls:
import { DataTableColumnGroupOptions } from "@/components/niko-table/components/data-table-column-group"
header: () => ( <DataTableColumnHeader> <DataTableColumnTitle title="Region" /> <DataTableColumnActions> <DataTableColumnSortOptions /> <DataTableColumnGroupOptions /> </DataTableColumnActions> </DataTableColumnHeader>),Set enableGrouping: false on a column to hide Group by for it (IDs, free-text fields, and so on).
Expand / collapse all
Section titled “Expand / collapse all”Each band has its own toggle. A toolbar button to expand or collapse all is handy. table.toggleAllRowsExpanded() reads from context, so you need no extra state:
import { useDataTable } from "@/components/niko-table/core/data-table-context"
function GroupingExpansionControls() { const { table } = useDataTable<Order>() const hasGrouping = table.state.grouping.length > 0
return ( <> <Button variant="outline" size="sm" disabled={!hasGrouping} onClick={() => table.toggleAllRowsExpanded(true)} > Expand All </Button> <Button variant="outline" size="sm" disabled={!hasGrouping} onClick={() => table.toggleAllRowsExpanded(false)} > Collapse All </Button> </> )}Put <GroupingExpansionControls /> inside DataTableToolbarSection, next to the search input.
Aggregations
Section titled “Aggregations”Set aggregationFn on columns that should roll up on group rows. Built-ins include sum, count, min, max, mean, median and more. See TanStack Grouping.
{ accessorKey: "amount", aggregationFn: "sum", cell: ({ getValue }) => formatCurrency(Number(getValue())), aggregatedCell: ({ getValue }) => ( <span className="font-medium tabular-nums"> {formatCurrency(Number(getValue()))} </span> ),}Group by month
Section titled “Group by month”Use getGroupingValue to make a bucket (for example YYYY-MM). The date cell in the leaf rows does not change:
{ accessorKey: "date", getGroupingValue: row => row.date.slice(0, 7), cell: ({ getValue, row }) => { const value = String(getValue()) if (row.getIsGrouped()) return formatMonthKey(value) return new Date(`${value}T00:00:00`).toLocaleDateString("en-US") },}Controlled state
Section titled “Controlled state”const [grouping, setGrouping] = useState<GroupingState>(["region"])const [expanded, setExpanded] = useState<ExpandedState>(true)
<DataTableRoot data={data} columns={columns} state={{ grouping, expanded }} onGroupingChange={setGrouping} onExpandedChange={setExpanded}>Or set the starting state once with initialState={{ grouping: ["region"], expanded: true }}.
Config
Section titled “Config”| Option | Purpose |
|---|---|
config.enableGrouping |
Force grouping on or off. Usually not needed: DataTableGroupedRows or a column group menu turns it on |
config.groupedColumnMode |
'reorder' (default), 'remove', or false. Where TanStack places grouped columns |
config.enableExpanding |
Forced on when grouping is active, so group rows can collapse |
Tree vs Grouping
Section titled “Tree vs Grouping”Both expand and collapse, but they are different features. Use one per table:
| Tree Table | Grouping | |
|---|---|---|
| Source | Nested data via getSubRows |
Flat data + column buckets |
| Who defines structure | Your data model | User / grouping state |
| Aggregations | Not the main model | aggregationFn / aggregatedCell |
| Typical UI | Indent + chevron per level | Group by in the column menu + (n) count |
Row Expansion is a third pattern. One flat row opens an inline detail panel. It has no nested rows and no column buckets.
Constraints
Section titled “Constraints”- Bodies: group bands render in
DataTableBodyandDataTableVirtualizedBody, with the sameDataTableGroupedRowsmarker in either. The DnD bodies do not render bands, because manual row order and grouped order conflict. This is by design. - Virtualized body: leave
fixedRowHeightoff. A band is taller than a normal row, so the body must measure it. With fixed heights on, the rows below drift. - Row DnD: do not combine it with grouping. Manual order and grouped order conflict. The same advice applies to sorting with row DnD.
- Still works: column DnD, sorting, filtering and pagination.
- Server-side:
manualGroupingis out of scope for this release. TanStack notes it needs heavy custom rendering.
When to use
Section titled “When to use”✅ Use grouping when:
- You summarize large lists by region, status, category or month
- You want rollups (sum of amount, count of rows) on group headers
- You are moving from MUI X or a similar grid with Group by in the column menu
❌ Consider other options when:
- Rows form a fixed parent and child hierarchy: use a Tree Table
- You need a detail panel per row: use Row Expansion
- Grouping is computed on the server:
manualGroupingis out of scope for this release
Next steps
Section titled “Next steps”- Tree Table: nested data with
getSubRows - Row Expansion Table: inline detail panels
- Column Pinning Table: keep ID columns visible while scrolling grouped views
- Faceted Filter Table: narrow the rows before grouping