Guides
Column Grouping Guide
How to use and customize the column grouping features of Shadcn Table
Shadcn Table has built-in column grouping features. There are options for both automatic client-side grouping as well as manual server-side grouping. This guide will walk you through the different options and how to use and customize them.
Example
Relevant Table Options
| Prop Name | Type | Description |
|---|---|---|
| enableExpandAll | boolean | |
| enableGrouping | boolean | |
| groupedColumnMode | false | 'reorder' | 'remove' | Grouping columns are automatically reordered by default to the start of the columns list. If you would rather remove them or leave them as-is, set the appropriate mode here. |
| manualGrouping | boolean | Enables manual grouping. If this option is set to true, the table will not automatically group rows using getGroupedRowModel() and instead will expect you to manually group the rows before passing them to the table. This is useful if you are doing server-side grouping and aggregation. |
| onGroupingChange | OnChangeFn<GroupingState> | If this function is provided, it will be called when the grouping state changes and you will be expected to manage the state yourself. You can pass the managed state back to the table via the tableOptions.state.grouping option. |
| positionToolbarAlertBanner | 'bottom' | 'top' | 'none' | |
| renderToolbarAlertBannerContent | ({ table, groupedAlert, selectedAlert }) => ReactNode | |
| stToolbarAlertBannerChipProps | ChipProps| ({ table }} => ChipProps |
Relevant Column Options
| Column Option | Type | Description |
|---|---|---|
| AggregatedCell | ({ cell, column, row, table }) => ReactNode | Define a custom cell render for an aggregated cell. |
| GroupedCell | ({ cell, column, row, table }) => ReactNode | Define a custom cell render for a grouped cell. |
| PlaceholderCell | ({ cell, column, row, table }) => ReactNode | Define a custom cell render for a placeholder cell. |
| enableGrouping | boolean |
Relevant State
| State Option | Type | Description |
|---|---|---|
| expanded | Record<string, boolean> | boolean | |
| grouping | Array<string> |
Enable Grouping
To enable grouping, set the enableGrouping table option to true. This makes the header cells draggable, so that columns can be dragged to the dropzone to be grouped, and adds an item to the column menu to group or ungroup a column.
const table = useShadcnTable({
columns,
data,
enableGrouping: true,
});Grouped Column Mode
The groupedColumnMode table option controls how grouped columns are displayed once a column has been grouped. There are three options:
"reorder"(default) - Grouped columns will be displayed as the first columns in the table, followed by the 'st-row-expand' display column, followed by the remaining columns in their original order."remove"- Grouped columns will be removed from the table and only their aggregate values will be displayed alongside the expand button in the 'st-row-expand' display column, followed by the remaining columns in their original order.false- Grouping columns will have no effect on the column order. The 'st-row-expand' display column will be displayed as the first column in the table, followed by the remaining columns in their original order.
const table = useShadcnTable({
columns,
data,
enableGrouping: true,
groupedColumnMode: 'remove', //instead of default "reorder"
});Disable Grouping Per Column
const columns = [
{
accessorKey: 'name',
header: 'Name',
enableGrouping: false, // disable grouping for this column
},
{
accessorKey: 'age',
header: 'Age',
},
];
const table = useShadcnTable({
columns,
data,
enableGrouping: true,
});
return <ShadcnTable table={table} />;Turn Off Header Dragging for Grouping
With grouping on, users can drag a header cell onto the grouping drop zone above the table to group by that column. If you do not want headers to be draggable, you can turn dragging off without disabling the grouping feature by setting the enableColumnDragging table option to false. This also stops column reordering by drag, which is on by default.
const table = useShadcnTable({
columns,
data,
enableGrouping: true,
enableColumnDragging: false, //headers are not draggable, but the column menu still has the grouping items
});
return <ShadcnTable table={table} />;Group Columns by Default
If you want columns to be grouped by default, you can set the grouping state in either the initialState or state table option.
const table = useShadcnTable({
columns,
data,
enableGrouping: true,
initialState: { grouping: ['location', 'department'] }, //group by location and department by default
});
return <ShadcnTable table={table} />;Expand Grouped Rows by Default
In addition to grouping columns by default, you may also want those grouped rows to be expanded and visible by default, too. You can do this by setting the expanded state to true in either the initialState or state table option.
const table = useShadcnTable({
columns,
data,
enableGrouping: true,
initialState: {
grouping: ['location', 'department'], //group by location and department by default and expand grouped rows
expanded: true, //show grouped rows by default
},
});
return <ShadcnTable table={table} />;Customize Expand Column
You can customize the expand column by using the displayColumnDefOptions table option.
const table = useShadcnTable({
columns,
data,
enableGrouping: true,
displayColumnDefOptions: {
'st-row-expand': {
size: 120, //make the expand column wider
},
},
});Going further, you can completely overhaul how the expand column renders using custom Cell and Header renders here for advanced use cases.
const table = useShadcnTable({
columns,
data,
enableGrouping: true,
displayColumnDefOptions: {
'st-row-expand': {
Cell: ({ row }) => {
return (
<>
<ST_ExpandButton row={row} />
{/*custom content*/}
</>
);
},
Header: () => {
return (
<>
<ST_ExpandAllButton />
{/*custom content*/}
</>
);
},
},
},
});Manual Grouping
Manual Grouping means that the data that you pass to the table is already grouped and aggregated, and you do not want Shadcn Table to do any of the grouping or aggregation for you. This is useful if you are using a backend API to do the grouping and aggregation for you, and you just want to display the results. However, you will need to put your data in the specific format that the expanding features understand.
Learn more about Expanding Sub Rows and Aggregation Features in their own dedicated guides.