gridcn2.0.0
Sort, filter, and search

Toolbar

Lay out the grid toolbar with quick search, the filter menu, the sort list, the columns menu, and your own buttons.

data-grid-toolbar is a flex frame for the toolbar of a grid. It renders its children from the start side to the end side in the order you write them. Each piece works alone. Leave one out, and the others keep their order. The add-on also ships DataGridSearch, DataGridFilterMenu, and DataGridColumnsMenu for the frame, and DataGridHeaderFilter for the column headers.

Name
Score
Silent Tiger
4
Quick Lion
89
Swift Wolf
80
Eager Lion
51
Eager Tiger
83
Silent Wolf
15
Swift Lion
62
Eager Lion
42
Eager Eagle
35
Bold Tiger
52
Bold Bear
28
Swift Wolf
9
Swift Bear
45
Eager Wolf
44
Swift Eagle
51
Quick Lion
28
Bold Wolf
76
Swift Eagle
26
Eager Eagle
70
Eager Bear
67
Silent Lion
89
Silent Tiger
40
Eager Tiger
82
Silent Lion
16
Bold Eagle
38
Bold Lion
3
Quick Tiger
41
Bold Eagle
10
Swift Tiger
6
Silent Lion
42
pnpm dlx shadcn@latest add @gridcn/data-grid-toolbar

Wiring

The toolbar sits between the provider and the root. The <DataGrid> wrapper renders its children inside the root, so the toolbar needs the split form of the grid:

"use client";

import {
  DataGridBody,
  DataGridHeader,
  DataGridProvider,
  DataGridRoot,
  defineColumns,
} from "@/components/data-grid/data-grid";
import {
  DataGridColumnsMenu,
  DataGridFilterMenu,
  DataGridSearch,
  DataGridToolbar,
} 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" },
]);

export function PeopleGrid({ rows }: { rows: Person[] }) {
  return (
    <DataGridProvider defaultData={rows} columns={columns}>
      <DataGridToolbar>
        <DataGridSearch />
        <DataGridFilterMenu />
        <DataGridColumnsMenu />
      </DataGridToolbar>
      <DataGridRoot className="h-96">
        <DataGridHeader />
        <DataGridBody />
      </DataGridRoot>
    </DataGridProvider>
  );
}

DataGridToolbar carries the data-grid-toolbar attribute, which scopes the search shortcut to the grid.

PropTypeDefaultDescription
classNamestringnoneMerged into the classes of the frame.
childrenReactNodenoneThe pieces, in render order.

The pieces

  • DataGridSearch is the quick-search input with match navigation. See Quick search.
  • DataGridFilterMenu is the filter popover. See Filtering.
  • DataGridSortList is the sort popover from its own add-on. See Sorting.
  • DataGridColumnsMenu shows and hides columns. Pin controls live in the header menu.
  • DataGridHeaderFilter is not a toolbar child. See Header filter.

The filter menu and the sort list offer the visible columns. Pass allColumns to either one to offer hidden columns for new filters and sorts. A row for an existing filter or sort always shows its own column. The demo below lists the hidden Email column in the sort list this way. It also sets filter.operators on the Name column, which limits it to equals and notEquals.

Filter Name for only equals/not equals, or Score for only greater/less than. Sort by Email even though it is hidden from the grid.

Name
Score
Silent Tiger
4
Quick Lion
89
Swift Wolf
80
Eager Lion
51
Eager Tiger
83
Silent Wolf
15
Swift Lion
62
Eager Lion
42
Eager Eagle
35
Bold Tiger
52
Bold Bear
28
Swift Wolf
9
Swift Bear
45
Eager Wolf
44
Swift Eagle
51
Quick Lion
28
Bold Wolf
76
Swift Eagle
26
Eager Eagle
70
Eager Bear
67
Silent Lion
89
Silent Tiger
40
Eager Tiger
82
Silent Lion
16
Bold Eagle
38
Bold Lion
3
Quick Tiger
41
Bold Eagle
10
Swift Tiger
6
Silent Lion
42

Toolbar order

The convention is search first, because users look for data before they shape it. Then come the filter menu and the sort list, which change the rows. Then comes the columns menu. Import and export buttons follow, import first. Your own buttons come last.

On this page