gridcn2.0.0
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.

Contact
Contact
Work
Standing
History
Name
Email
Role
Score
Active
Joined
Age
Silent Tiger
user0@example.com
Viewer
2023-07-05
63
4
Quick Lion
user1@example.com
Viewer
2021-04-03
67
89
Swift Wolf
user2@example.com
Manager
2020-10-27
40
80
Eager Lion
user3@example.com
User
2022-10-08
50
51
Eager Tiger
user4@example.com
Viewer
2024-10-07
45
83
Silent Wolf
user5@example.com
User
2020-06-01
24
15
Swift Lion
user6@example.com
Viewer
2022-10-18
34
62
Eager Lion
user7@example.com
Manager
2021-10-07
21
42
Eager Eagle
user8@example.com
Editor
2024-06-11
25
35
Bold Tiger
user9@example.com
Editor
2024-03-20
19
52
Bold Bear
user10@example.com
User
2020-04-16
42
28
Swift Wolf
user11@example.com
Manager
2022-08-04
47
9
pnpm dlx shadcn@latest add @gridcn/data-grid-column-groups-demo

What the demo shows

  • columnGroups is a list of { id, header, children } objects. children lists column ids and nested groups.
  • In the default select mode, 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.
  • DataGridColumnsMenu lists 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"] },
];

On this page