gridcn2.0.0
Column examples

Column headers example

Set a column header to your own node with icons that follow the sort state, and keep a plain-text label.

ColumnDef.header takes a string or a React node. Click the Name or Score header to sort. A minus icon means no sort. An up icon means ascending. A down icon means descending.

Click the Name or Score header to sort: a minus means unsorted, an up icon ascending, a down icon descending.
Name
Email
Score
Silent Tiger
user0@example.com
4
Quick Lion
user1@example.com
89
Swift Wolf
user2@example.com
80
Eager Lion
user3@example.com
51
Eager Tiger
user4@example.com
83
Silent Wolf
user5@example.com
15
Swift Lion
user6@example.com
62
Eager Lion
user7@example.com
42
Eager Eagle
user8@example.com
35
Bold Tiger
user9@example.com
52
pnpm dlx shadcn@latest add @gridcn/data-grid-custom-headers-demo

What the demo shows

  • headerClickBehavior="sort" makes a header click sort the column up, then down, then clear the sort.
  • header on Name and Score is a node. A node header renders as it is and owns its display. Both set sortIndicator: false because their own icon replaces the built-in arrow.
  • SortStateIcon calls useDataGridSortState() to read the sort list and picks an icon from the entry of its column.
  • No demo column sets headerText. The plain-text label of a node header is the text of its HTML elements, or the column id when they hold no text. A component inside the node adds no text. Set headerText to give the label directly.
  • Email keeps a string header and the built-in sort arrow.

A node header shows the built-in arrow after its content. To place the arrow inside the node, set sortIndicator: false and render DataGridSortIndicator there:

import {
  DataGridSortIndicator,
  defineColumns,
} from "@/components/data-grid/data-grid";

type Row = { id: string; score: number };

export const columns = defineColumns<Row>()([
  {
    id: "score",
    accessorKey: "score",
    type: "number",
    sortIndicator: false,
    header: (
      <span>
        Score <DataGridSortIndicator columnId="score" />
      </span>
    ),
  },
]);

On this page