Skip to content
Niko Table

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.
grouping.tsx
Region
Order ID
Customer
Status
Date
Product
Amount
North America· 5 rows
$1,977.98
ORD-001John DoedeliveredJan 15, 2024Premium Widget$299.99
ORD-004Alice WilliamsdeliveredJan 22, 2024Starter Pack$79.99
ORD-007Ethan HuntpendingFeb 1, 2024Basic Kit$149.50
ORD-010Hannah LeedeliveredFeb 12, 2024Enterprise Suite$1,299.00
ORD-012Julia RossdeliveredMar 1, 2024Basic Kit$149.50
Europe· 4 rows
$2,347.49
ORD-002Jane SmithshippedJan 18, 2024Basic Kit$149.50
ORD-005Charlie BrownshippedJan 25, 2024Enterprise Suite$1,299.00
ORD-008Fiona GreendeliveredFeb 5, 2024Pro Bundle$599.00
Preview with Controlled State
grouping-state.tsx
Region
Order ID
Customer
Status
Date
Amount
North America(4)
undefined$1,828.48
ORD-001John DoedeliveredJan 15, 2024$299.99
ORD-004Alice WilliamsdeliveredJan 22, 2024$79.99
ORD-007Ethan HuntpendingFeb 1, 2024$149.50
ORD-010Hannah LeedeliveredFeb 12, 2024$1,299.00
Europe(3)
undefined$2,047.50
ORD-002Jane SmithshippedJan 18, 2024$149.50
ORD-005Charlie BrownshippedJan 25, 2024$1,299.00
ORD-008Fiona GreendeliveredFeb 5, 2024$599.00
Asia Pacific(3)
undefined$978.98
Current Table State
Live view of the current table state for demonstration purposes
Grouped By:region
Expanded Groups:All
Total Items:10
Sorting:None
Page:1 (Size: 10)
Hidden Columns:0
View Full State Object
Grouping:
[
  "region"
]
Expanded:
true
Sorting:
[]

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.
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-menu

First time using @niko-table? See the Installation Guide to set up the registry.

Nest DataTableGroupedRows in the body. That one line turns grouping on.

  • It renders the group bands.
  • It also turns on enableGrouping and enableExpanding (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.

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. Return false and that band uses the built-in row.
  • DataTableGroupedRowCells is exported. A custom row can render the built-in cells for cases it does not handle itself.

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).

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.

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>
),
}

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")
},
}
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 }}.

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

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.

  • Bodies: group bands render in DataTableBody and DataTableVirtualizedBody, with the same DataTableGroupedRows marker in either. The DnD bodies do not render bands, because manual row order and grouped order conflict. This is by design.
  • Virtualized body: leave fixedRowHeight off. 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: manualGrouping is out of scope for this release. TanStack notes it needs heavy custom rendering.

✅ 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: manualGrouping is out of scope for this release