Column examples
Column groups example
Put a shared header over a run of columns with columnGroups, nested up to 8 levels deep.
Three groups sit above the columns: Contact, Work, and Standing. Work holds a nested History group, so the header has three rows. The demo pins Name to the start side, so the Contact header splits at the pin edge.
Click a group header to select its columns. Drag a group header to move the whole block, or drag a column within its group. Hide every column of a group and its header disappears.
pnpm dlx shadcn@latest add @gridcn/data-grid-column-groups-demo
What the demo shows
columnGroupsis a list of{ id, header, children }objects.childrenlists column ids and nested groups.- In the default
selectmode, a press on a group header selects the columns under that header cell. The Contact header has two cells, so a press on its pinned cell selects Name only. - A drag on a group header moves the group as one block inside its parent. A drag on a column moves it inside its group.
- The Contact header splits in two cells because the demo pins Name and leaves Email unpinned.
- If the user hides every column of a group, the group header disappears.
DataGridColumnsMenulists the members of each group under the group label.
The demo passes this list to columnGroups:
import type { ColumnGroupDef } from "@/components/data-grid/data-grid";
export const columnGroups: readonly ColumnGroupDef[] = [
{ id: "contact", header: "Contact", children: ["name", "email"] },
{
id: "work",
header: "Work",
children: [
"role",
{ id: "history", header: "History", children: ["joined", "age"] },
],
},
{ id: "standing", header: "Standing", children: ["score", "active"] },
];Related
- Column headers and groups: the rules for order, pin, selection, and limits
- Screen reader support: header row indices and spans
- Pinned columns example: pin columns to the start or end side
- Column layout example: save the order of grouped columns