Docs Sorting, grouping & expanding Grouping & aggregation 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.
Grouping & aggregation Group by a column; aggregate with a sticky footer total.
Department Sort by Department ascending ID
First name Sort by First name ascending Last name Sort by Last name ascending Email Sort by Email ascending Role Sort by Role ascending Status Sort by Status ascending Salary Sort by Salary ascending Start date Sort by Start date ascending Progress Sort by Progress ascending Engineering (10) ~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 U-1000 Ava Thompson ava.thompson@example.com Owner active 22 $45,000 Jan 2, 2023 U-1005 William Walker william.walker@example.com Admin pending 37 $55,685 Feb 26, 2023 U-1010 Mia Johansson mia.johansson@example.com Editor inactive 52 $66,370 Apr 22, 2023 U-1015 Jack Diaz jack.diaz@example.com Viewer active 27 $77,055 Jun 16, 2023 U-1020 Ava Thompson ava.thompson@example.com Owner pending 42 $87,740 Aug 10, 2023 U-1025 William Walker william.walker@example.com Admin inactive 57 $98,425 Oct 4, 2023 U-1030 Mia Johansson mia.johansson@example.com Editor active 32 $109,110 Nov 28, 2023 U-1035 Jack Diaz jack.diaz@example.com Viewer pending 47 $119,795 Jan 22, 2024 U-1040 Ava Thompson ava.thompson@example.com Owner inactive 22 $130,480 Mar 17, 2024 U-1045 William Walker william.walker@example.com Admin active 37 $51,165 May 11, 2024 Marketing (10) ~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 U-1001 Liam Nguyen liam.nguyen@example.com Admin inactive 25 $47,137 Jan 13, 2023 U-1006 Sophia Patel sophia.patel@example.com Editor active 40 $57,822 Mar 9, 2023 U-1011 Benjamin Costa benjamin.costa@example.com Viewer pending 55 $68,507 May 3, 2023 U-1016 Harper Dubois harper.dubois@example.com Owner inactive 30 $79,192 Jun 27, 2023 U-1021 Liam Nguyen liam.nguyen@example.com Admin active 45 $89,877 Aug 21, 2023 U-1026 Sophia Patel sophia.patel@example.com Editor pending 60 $100,562 Oct 15, 2023 U-1031 Benjamin Costa benjamin.costa@example.com Viewer inactive 35 $111,247 Dec 9, 2023 U-1036 Harper Dubois harper.dubois@example.com Owner active 50 $121,932 Feb 2, 2024 U-1041 Liam Nguyen liam.nguyen@example.com Admin pending 25 $132,617 Mar 28, 2024 U-1046 Sophia Patel sophia.patel@example.com Editor inactive 40 $53,302 May 22, 2024 Design (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 U-1002 Noah Silva noah.silva@example.com Editor pending 28 $49,274 Jan 24, 2023 U-1007 James Muller james.muller@example.com Viewer inactive 43 $59,959 Mar 20, 2023 U-1012 Charlotte Chen charlotte.chen@example.com Owner active 58 $70,644 May 14, 2023 U-1017 Leo Haddad leo.haddad@example.com Admin pending 33 $81,329 Jul 8, 2023 U-1022 Noah Silva noah.silva@example.com Editor inactive 48 $92,014 Sep 1, 2023 U-1027 James Muller james.muller@example.com Viewer active 23 $102,699 Oct 26, 2023 U-1032 Charlotte Chen charlotte.chen@example.com Owner pending 38 $113,384 Dec 20, 2023 U-1037 Leo Haddad leo.haddad@example.com Admin inactive 53 $124,069 Feb 13, 2024 U-1042 Noah Silva noah.silva@example.com Editor active 28 $134,754 Apr 8, 2024 U-1047 James Muller james.muller@example.com Viewer pending 43 $55,439 Jun 2, 2024 Sales (9) ~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 U-1003 Emma Carter emma.carter@example.com Viewer active 31 $51,411 Feb 4, 2023 U-1008 Isabella Park isabella.park@example.com Owner pending 46 $62,096 Mar 31, 2023 U-1013 Henry Cohen henry.cohen@example.com Admin inactive 61 $72,781 May 25, 2023 U-1018 Ella Khan ella.khan@example.com Editor active 36 $83,466 Jul 19, 2023 U-1023 Emma Carter emma.carter@example.com Viewer pending 51 $94,151 Sep 12, 2023 U-1028 Isabella Park isabella.park@example.com Owner inactive 26 $104,836 Nov 6, 2023 U-1033 Henry Cohen henry.cohen@example.com Admin active 41 $115,521 Dec 31, 2023 U-1038 Ella Khan ella.khan@example.com Editor pending 56 $126,206 Feb 24, 2024 U-1043 Emma Carter emma.carter@example.com Viewer inactive 31 $46,891 Apr 19, 2024 Support (9) ~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 U-1004 Olivia Rossi olivia.rossi@example.com Owner inactive 34 $53,548 Feb 15, 2023 U-1009 Lucas Reyes lucas.reyes@example.com Admin active 49 $64,233 Apr 11, 2023 U-1014 Amelia Novak amelia.novak@example.com Editor pending 24 $74,918 Jun 5, 2023 U-1019 Mason Tanaka mason.tanaka@example.com Viewer inactive 39 $85,603 Jul 30, 2023 U-1024 Olivia Rossi olivia.rossi@example.com Owner active 54 $96,288 Sep 23, 2023 U-1029 Lucas Reyes lucas.reyes@example.com Admin pending 29 $106,973 Nov 17, 2023 $4,120,536
Initial groups
Seed the grouped state with initialState.grouping and expand all groups on first render with initialState.expanded.
tsx Copy
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", ...), a custom function (built with TanStack v9's constructAggregationFn({ aggregate })), or the name of one registered with the aggregationFns option (see below), render the rolled-up value with aggregatedCell, and add a footer for a grand total.
tsx Copy
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>
},
},
]
To reuse an aggregation across columns, register it by name with aggregationFns. Each entry has an aggregate function that receives the group's rows and a getValue(row) helper:
tsx Copy
const table = useDataTable({
data,
columns: [
{ accessorKey: "department", header: "Department" },
{ accessorKey: "salary", header: "Salary", aggregationFn: "spread" },
],
aggregationFns: {
spread: {
aggregate: ({ rows, getValue }) => {
const values = rows.map((row) => Number(getValue(row)))
return Math.max(...values) - Math.min(...values)
},
},
},
enableGrouping: true,
})
Pass several aggregations as an array and each result shows with its label — Sum 4,000 · Mean 2,000, with numbers in the table's language. Rename the labels with localization.aggregationFns, or render them yourself with aggregatedCell, which receives every result (getValue() returns { sum, mean }).
tsx Copy
{
accessorKey: "salary",
header: "Salary",
aggregationFn: ["sum", "mean"],
}
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 aggregation A named aggregation (salary spread) in grouped rows.
Department Sort by Department ascending First name Sort by First name ascending Salary Sort by Salary ascending Engineering (10) spread $85,480 Marketing (10) spread $85,480 Design (10) spread $85,480 Sales (9) spread $79,315 Support (9) spread $79,315
Several aggregations Sum, mean and max of a column in each group.
Department Sort by Department ascending First name Sort by First name ascending Salary Sort by Salary ascending Engineering (10) Sum 840,825 Mean 84,082.5 Max 130,480 Min 22 Max 57 Marketing (10) Sum 862,195 Mean 86,219.5 Max 132,617 Min 25 Max 60 Design (10) Sum 883,565 Mean 88,356.5 Max 134,754 Min 23 Max 58 Sales (9) Sum 757,359 Mean 84,151 Max 126,206 Min 26 Max 61 Support (9) Sum 776,592 Mean 86,288 Max 128,343 Min 24 Max 59
Grouped column mode
TanStack's groupedColumnMode decides where a grouped column goes: "reorder" (default) moves it first, "remove" hides it, and false leaves it in place.
Grouped column mode Move grouped columns first, hide them, or leave them in place. groupedColumnMode false remove
Department Sort by Department ascending ID
First name Sort by First name ascending Last name Sort by Last name ascending Email Sort by Email ascending Role Sort by Role ascending Status Sort by Status ascending Salary Sort by Salary ascending Start date Sort by Start date ascending Progress Sort by Progress ascending Engineering (10) ~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 U-1000 Ava Thompson ava.thompson@example.com Owner active 22 $45,000 Jan 2, 2023 U-1005 William Walker william.walker@example.com Admin pending 37 $55,685 Feb 26, 2023 U-1010 Mia Johansson mia.johansson@example.com Editor inactive 52 $66,370 Apr 22, 2023 U-1015 Jack Diaz jack.diaz@example.com Viewer active 27 $77,055 Jun 16, 2023 U-1020 Ava Thompson ava.thompson@example.com Owner pending 42 $87,740 Aug 10, 2023 U-1025 William Walker william.walker@example.com Admin inactive 57 $98,425 Oct 4, 2023 U-1030 Mia Johansson mia.johansson@example.com Editor active 32 $109,110 Nov 28, 2023 U-1035 Jack Diaz jack.diaz@example.com Viewer pending 47 $119,795 Jan 22, 2024 U-1040 Ava Thompson ava.thompson@example.com Owner inactive 22 $130,480 Mar 17, 2024 U-1045 William Walker william.walker@example.com Admin active 37 $51,165 May 11, 2024 Marketing (10) ~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 U-1001 Liam Nguyen liam.nguyen@example.com Admin inactive 25 $47,137 Jan 13, 2023 U-1006 Sophia Patel sophia.patel@example.com Editor active 40 $57,822 Mar 9, 2023 U-1011 Benjamin Costa benjamin.costa@example.com Viewer pending 55 $68,507 May 3, 2023 U-1016 Harper Dubois harper.dubois@example.com Owner inactive 30 $79,192 Jun 27, 2023 U-1021 Liam Nguyen liam.nguyen@example.com Admin active 45 $89,877 Aug 21, 2023 U-1026 Sophia Patel sophia.patel@example.com Editor pending 60 $100,562 Oct 15, 2023 U-1031 Benjamin Costa benjamin.costa@example.com Viewer inactive 35 $111,247 Dec 9, 2023 U-1036 Harper Dubois harper.dubois@example.com Owner active 50 $121,932 Feb 2, 2024 $4,120,536
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).
tsx Copy
{
accessorKey: "department",
header: "Department",
meta: {
renderGroupedCell: ({ row }) => (
<span className="font-medium">
{row.getValue("department")} · {row.subRows.length}
</span>
),
},
}
Customized grouping A custom group header with the count and a total.
Department Sort by Department ascending ID
First name Sort by First name ascending Last name Sort by Last name ascending Email Sort by Email ascending Role Sort by Role ascending Status Sort by Status ascending Salary Sort by Salary ascending Start date Sort by Start date ascending Progress Sort by Progress ascending Engineering 10 $840,825 (10) ~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 Marketing 10 $862,195 (10) ~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 Design 10 $883,565 (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 Sales 9 $757,359 (9) ~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 Support 9 $776,592 (9) ~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 $4,120,536