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.
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.headeron Name and Score is a node. A node header renders as it is and owns its display. Both setsortIndicator: falsebecause their own icon replaces the built-in arrow.SortStateIconcallsuseDataGridSortState()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. SetheaderTextto 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>
),
},
]);Related
- Column headers and groups: custom header content and
renderHeaderCell - Sorting: the sort state and the click cycle
- Pinned columns example: the header menu and resize edges