Grouping & aggregation
Set enableGrouping: true to let users group rows by a column. Once enabled, users can drag a column header onto the drop-to-group zone, or pick Group by from a column's actions menu.
ID | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| ~38 | $840,825 | Mon Jan 02 2023 00:00:00 GMT+0000 (Coordinated Universal Time),Sat May 11 2024 00:00:00 GMT+0000 (Coordinated Universal Time) | 409 | |||||||
| Ava | Thompson | Owner | active | 22 | $45,000 | Jan 2, 2023 | 0% | |||
| William | Walker | Admin | pending | 37 | $55,685 | Feb 26, 2023 | 45% | |||
| Mia | Johansson | Editor | inactive | 52 | $66,370 | Apr 22, 2023 | 90% | |||
| Jack | Diaz | Viewer | active | 27 | $77,055 | Jun 16, 2023 | 34% | |||
| Ava | Thompson | Owner | pending | 42 | $87,740 | Aug 10, 2023 | 79% | |||
| William | Walker | Admin | inactive | 57 | $98,425 | Oct 4, 2023 | 23% | |||
| Mia | Johansson | Editor | active | 32 | $109,110 | Nov 28, 2023 | 68% | |||
| Jack | Diaz | Viewer | pending | 47 | $119,795 | Jan 22, 2024 | 12% | |||
| Ava | Thompson | Owner | inactive | 22 | $130,480 | Mar 17, 2024 | 57% | |||
| William | Walker | Admin | active | 37 | $51,165 | May 11, 2024 | 1% | |||
| ~41 | $862,195 | Fri Jan 13 2023 00:00:00 GMT+0000 (Coordinated Universal Time),Wed May 22 2024 00:00:00 GMT+0000 (Coordinated Universal Time) | 499 | |||||||
| Liam | Nguyen | Admin | inactive | 25 | $47,137 | Jan 13, 2023 | 9% | |||
| Sophia | Patel | Editor | active | 40 | $57,822 | Mar 9, 2023 | 54% | |||
| Benjamin | Costa | Viewer | pending | 55 | $68,507 | May 3, 2023 | 99% | |||
| Harper | Dubois | Owner | inactive | 30 | $79,192 | Jun 27, 2023 | 43% | |||
| Liam | Nguyen | Admin | active | 45 | $89,877 | Aug 21, 2023 | 88% | |||
| Sophia | Patel | Editor | pending | 60 | $100,562 | Oct 15, 2023 | 32% | |||
| Benjamin | Costa | Viewer | inactive | 35 | $111,247 | Dec 9, 2023 | 77% | |||
| Harper | Dubois | Owner | active | 50 | $121,932 | Feb 2, 2024 | 21% | |||
| Liam | Nguyen | Admin | pending | 25 | $132,617 | Mar 28, 2024 | 66% | |||
| Sophia | Patel | Editor | inactive | 40 | $53,302 | May 22, 2024 | 10% | |||
| ~40 | $883,565 | Tue Jan 24 2023 00:00:00 GMT+0000 (Coordinated Universal Time),Sun Jun 02 2024 00:00:00 GMT+0000 (Coordinated Universal Time) | 488 | |||||||
| Noah | Silva | Editor | pending | 28 | $49,274 | Jan 24, 2023 | 18% | |||
| James | Muller | Viewer | inactive | 43 | $59,959 | Mar 20, 2023 | 63% | |||
| Charlotte | Chen | Owner | active | 58 | $70,644 | May 14, 2023 | 7% | |||
| Leo | Haddad | Admin | pending | 33 | $81,329 | Jul 8, 2023 | 52% | |||
| Noah | Silva | Editor | inactive | 48 | $92,014 | Sep 1, 2023 | 97% | |||
| James | Muller | Viewer | active | 23 | $102,699 | Oct 26, 2023 | 41% | |||
| Charlotte | Chen | Owner | pending | 38 | $113,384 | Dec 20, 2023 | 86% | |||
| Leo | Haddad | Admin | inactive | 53 | $124,069 | Feb 13, 2024 | 30% | |||
| Noah | Silva | Editor | active | 28 | $134,754 | Apr 8, 2024 | 75% | |||
| James | Muller | Viewer | pending | 43 | $55,439 | Jun 2, 2024 | 19% | |||
| ~42 | $757,359 | Sat Feb 04 2023 00:00:00 GMT+0000 (Coordinated Universal Time),Fri Apr 19 2024 00:00:00 GMT+0000 (Coordinated Universal Time) | 449 | |||||||
| Emma | Carter | Viewer | active | 31 | $51,411 | Feb 4, 2023 | 27% | |||
| Isabella | Park | Owner | pending | 46 | $62,096 | Mar 31, 2023 | 72% | |||
| Henry | Cohen | Admin | inactive | 61 | $72,781 | May 25, 2023 | 16% | |||
| Ella | Khan | Editor | active | 36 | $83,466 | Jul 19, 2023 | 61% | |||
| Emma | Carter | Viewer | pending | 51 | $94,151 | Sep 12, 2023 | 5% | |||
| Isabella | Park | Owner | inactive | 26 | $104,836 | Nov 6, 2023 | 50% | |||
| Henry | Cohen | Admin | active | 41 | $115,521 | Dec 31, 2023 | 95% | |||
| Ella | Khan | Editor | pending | 56 | $126,206 | Feb 24, 2024 | 39% | |||
| Emma | Carter | Viewer | inactive | 31 | $46,891 | Apr 19, 2024 | 84% | |||
| ~41 | $776,592 | Wed Feb 15 2023 00:00:00 GMT+0000 (Coordinated Universal Time),Tue Apr 30 2024 00:00:00 GMT+0000 (Coordinated Universal Time) | 429 | |||||||
| Olivia | Rossi | Owner | inactive | 34 | $53,548 | Feb 15, 2023 | 36% | |||
| Lucas | Reyes | Admin | active | 49 | $64,233 | Apr 11, 2023 | 81% | |||
| Amelia | Novak | Editor | pending | 24 | $74,918 | Jun 5, 2023 | 25% | |||
| Mason | Tanaka | Viewer | inactive | 39 | $85,603 | Jul 30, 2023 | 70% | |||
| Olivia | Rossi | Owner | active | 54 | $96,288 | Sep 23, 2023 | 14% | |||
| Lucas | Reyes | Admin | pending | 29 | $106,973 | Nov 17, 2023 | 59% | |||
| $4,120,536 |
Initial groups
Seed the grouped state with initialState.grouping and expand all groups on first render with initialState.expanded.
const table = useDataTable({
data,
columns,
getRowId: (row) => row.id,
enableGrouping: true,
initialState: {
grouping: ["department"],
expanded: true,
},
})
return <DataTable table={table} />
Aggregations
Aggregations use the standard TanStack Table column options. Set aggregationFn to a built-in ("sum", "mean", ...) or a custom function, render the rolled-up value with aggregatedCell, and add a footer for a grand total.
const columns = [
{
accessorKey: "salary",
header: "Salary",
aggregationFn: "sum",
aggregatedCell: ({ getValue }) => (
<strong>{formatCurrency(getValue<number>())}</strong>
),
footer: ({ table }) => {
const total = table
.getRowModel()
.rows.reduce((sum, row) => sum + row.getValue<number>("salary"), 0)
return <strong>{formatCurrency(total)}</strong>
},
},
]
Keep totals in view
Grouping pairs well with a pinned footer. Add enableStickyFooter: true so
the grand total stays visible while scrolling long groups.
Custom group cells
For finer control over how grouped rows render, the column meta exposes render hooks that receive the full table instance: renderGroupedCell (the group-header cell), renderAggregatedCell (overrides aggregatedCell), and renderPlaceholderCell (cells with no value because another column owns the group; empty by default).
{
accessorKey: "department",
header: "Department",
meta: {
renderGroupedCell: ({ row }) => (
<span className="font-medium">
{row.getValue("department")} · {row.subRows.length}
</span>
),
},
}