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.
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.
| Prop | Type | Default | Description |
|---|---|---|---|
className | string | none | Merged into the classes of the frame. |
children | ReactNode | none | The pieces, in render order. |
The pieces
DataGridSearchis the quick-search input with match navigation. See Quick search.DataGridFilterMenuis the filter popover. See Filtering.DataGridSortListis the sort popover from its own add-on. See Sorting.DataGridColumnsMenushows and hides columns. Pin controls live in the header menu.DataGridHeaderFilteris 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.
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.
Related
- Toolbar options: the full prop list
- Quick search: the search input
- Filtering: the filter menu
- Column visibility: what the columns menu changes