Row examples
Row grouping example
Group 200 rows by role, show a count and two reduced values on each group row, and change the groups with buttons and the header menu.
One grid groups its rows by role. Each group row shows the row count, the average age, and the total score. The row grouping feature owns the props.
pnpm dlx shadcn@latest add @gridcn/data-grid-row-grouping-demo
What the demo shows
defaultGroupBy={["role"]}groups the rows. Every group starts closed.aggregate: "avg"on Age andaggregate: "sum"on Score set the reduced values of the group rows.groupable: falseon Name keeps it out of the Group by items.- The buttons call
expandAll,collapseAll, andsetGroupByfromuseDataGridActions. - The header menu has Group by and Ungroup items. The cell menu has a menu for each group row.
Wire the groups
Set defaultGroupBy on the provider and call the actions inside it:
import {
DataGrid,
defineColumns,
useDataGridActions,
} from "@/components/data-grid/data-grid";
type Row = { id: string; role: string; score: number };
const columns = defineColumns<Row>()([
{ id: "role", header: "Role", accessorKey: "role" },
{
id: "score",
header: "Score",
accessorKey: "score",
type: "number",
aggregate: "sum",
},
]);
const rows: Row[] = [{ id: "1", role: "Admin", score: 90 }];
function OpenAll() {
const actions = useDataGridActions();
return <button onClick={() => actions.expandAll()}>Expand all</button>;
}
function GroupedGrid() {
return (
<DataGrid defaultData={rows} columns={columns} defaultGroupBy={["role"]}>
<OpenAll />
</DataGrid>
);
}Behavior
- On a group row, Enter and Space open or close the group. ArrowRight opens it and ArrowLeft closes it.
- The grid takes the role
treegridwhile it groups. - The row marker numbers the rows inside each innermost group.
Related
- Row grouping: the props, the keys, and the pages of groups
- Server grouping example: the same groups on a database