gridcn2.0.0
Column examples

Header filter example

Filter a column from its header with a searchable list of values and counts, and read the filter it writes.

Each header holds a filter button. The button opens a popover with a search box and the set values of the column, each with a row count. Check the values to keep and press Apply.

Name
Role
Age
Score
Silent Tiger
Viewer
63
4
Quick Lion
Viewer
67
89
Swift Wolf
Manager
40
80
Eager Lion
User
50
51
Eager Tiger
Viewer
45
83
Silent Wolf
User
24
15
Swift Lion
Viewer
34
62
Eager Lion
Manager
21
42
Eager Eagle
Editor
25
35
Bold Tiger
Editor
19
52
Bold Bear
User
42
28
Swift Wolf
Manager
47
9
Swift Bear
Viewer
41
45
Eager Wolf
Editor
35
44
Swift Eagle
Viewer
65
51
Quick Lion
Manager
22
28
Bold Wolf
Editor
30
76
Swift Eagle
Admin
19
26
Eager Eagle
Manager
35
70
Eager Bear
Editor
33
67
Silent Lion
Manager
48
89
Silent Tiger
Manager
62
40
Eager Tiger
Manager
51
82
Silent Lion
Admin
26
16
Bold Eagle
Admin
26
38
Bold Lion
Viewer
49
3
Quick Tiger
Viewer
26
41
Bold Eagle
Editor
50
10
Swift Tiger
Editor
55
6
Silent Lion
Viewer
35
42
pnpm dlx shadcn@latest add @gridcn/data-grid-header-filter-demo

What the demo shows

  • renderHeaderFilter on DataGridRoot renders DataGridHeaderFilter in each header. The demo has two text columns and two number columns, and all four get a button.
  • With a mouse, the button stays hidden until you hover or focus the header. On a touch screen, the button is always visible. While the column has a filter, the button stays visible. From the keyboard, Shift+Alt+ArrowUp opens the popover of the focused header button's column, or else the active cell's column.
  • The search box narrows the list. The checkbox at its start checks or unchecks the values that match the search.
  • Apply writes the filter. Clear removes every filter of the column.

Wire the filter

The header filter is one prop of DataGridRoot:

import {
  DataGridBody,
  DataGridHeader,
  DataGridProvider,
  DataGridRoot,
  defineColumns,
} from "@/components/data-grid/data-grid";
import { DataGridHeaderFilter } from "@/components/data-grid-toolbar/data-grid-toolbar";

type Person = { id: string; name: string; age: number };

const columns = defineColumns<Person>()([
  { id: "name", header: "Name", accessorKey: "name" },
  { id: "age", header: "Age", accessorKey: "age", type: "number" },
]);

const rows: Person[] = [{ id: "1", name: "Ada", age: 36 }];

export function FilteredGrid() {
  return (
    <DataGridProvider defaultData={rows} columns={columns}>
      <DataGridRoot
        className="h-60"
        aria-label="People"
        renderHeaderFilter={(ctx) => <DataGridHeaderFilter {...ctx} />}
      >
        <DataGridHeader />
        <DataGridBody />
      </DataGridRoot>
    </DataGridProvider>
  );
}

The filter it writes

Apply picks the operator from the counts:

  • If the user checks fewer values than they leave unchecked, Apply stores isAnyOf with the checked values.
  • Otherwise it stores isNoneOf with the unchecked values.

On a tie, Apply keeps the operator of the current set filter, or uses isAnyOf if there is none. The filter menu writes the same shape. Header filter lists every Apply rule.

A role column has five values: Admin, User, Editor, Viewer, and Manager. Checking Admin and Editor writes this filter, which defaultFilterState can start a grid with:

import type { FilterNode } from "@/components/data-grid/data-grid";

export const defaultFilterState: FilterNode[] = [
  { columnId: "role", operator: "isAnyOf", value: ["Admin", "Editor"] },
];

Pass it as defaultFilterState to DataGridProvider or DataGrid.

On this page