Link multiple grids
Mount several grids on one page, then link them so the active row in one grid sets the rows of another.
Without a store prop, every DataGrid and every DataGridProvider creates its own store. No module-level store exists, so several grids work on one page with no setup. Sort, filter, selection, edits, and focus stay apart. To make two grids share one state, create a store with useDataGridStoreProps and pass it as store to both. External store shows how. To link two grids that keep their own state, pass state through React. A callback of one grid sets state, and the other grid reads it.
Click a customer. The order grid shows the orders of the customer in the active cell.
pnpm dlx shadcn@latest add @gridcn/data-grid-linked-grids-demo
Before you start
- Give every row of every grid a stable
id, or passgetRowId. - Give each grid a height.
className="h-[400px]"is enough.
Steps
- Hold the shared value in state above both grids.
- Set it from a callback of the first grid. With no active cell,
onActiveCellChangepassesnull. - Derive the rows of the second grid from that state with
useMemo. - Pass the derived rows as
datato the second grid. For a display-only grid, addreadOnly.
Show the orders of the active customer
The sample links a customer grid to an order grid:
"use client";
import { useMemo, useState } from "react";
import { DataGrid, defineColumns } from "@/components/data-grid/data-grid";
type Customer = { id: string; name: string };
type Order = { id: string; customerId: string; total: number };
const customerColumns = defineColumns<Customer>()([
{ id: "name", header: "Customer", accessorKey: "name", type: "text" },
] as const);
const orderColumns = defineColumns<Order>()([
{ id: "id", header: "Order", accessorKey: "id", type: "text" },
{ id: "total", header: "Total", accessorKey: "total", type: "number" },
] as const);
type Props = { customers: Customer[]; orders: Order[] };
export function CustomersAndOrders({ customers, orders }: Props) {
const [customerId, setCustomerId] = useState<string | null>(null);
const customerOrders = useMemo(
() => orders.filter((order) => order.customerId === customerId),
[orders, customerId],
);
return (
<div className="grid grid-cols-2 gap-4">
<DataGrid
className="h-[400px]"
defaultData={customers}
columns={customerColumns}
onActiveCellChange={(next) => setCustomerId(next?.rowId ?? null)}
/>
<DataGrid
className="h-[400px]"
data={customerOrders}
columns={orderColumns}
readOnly
/>
</div>
);
}Result
The order grid shows the orders of the customer in the active cell. It shows no rows while no cell is active. Each grid keeps its own sort, filter, and selection.
Set a different prefix on each grid that uses data-grid-url-state. Two grids on one prefix share every URL key, such as sort, filter, and q, and a development warning names the clash. See URL state.
Related
- Global shortcuts: which grid owns a page-wide shortcut.
- Selection and cell events: the
onActiveCellChangeevent. - Selection model: what a grid keeps in its own selection.