Server and data examples
Server grouping example
Group 5,000 database rows on the server with the query layer, open a group to load its rows, and read the statement of the last request.
One grid shows 5,000 users grouped by role, on an in-browser PostgreSQL. The query layer answers each request with one statement. The panel under the grid shows the last statement and its bound parameters.
Starting the in-browser database…
pnpm dlx shadcn@latest add @gridcn/data-grid-server-grouping-demo
What the demo shows
defaultSpec: { groupBy: ["role"] }makes the first request ask for the groups of the top level.- Opening a group sends a request with
groupPathandchildKind: "rows". The grid loads the rows in blocks of 50. aggregate: "avg"on Score adds an average to each group row. The database computes it.- The buttons call
setGroupBy. A newgroupByreturns the grid to the first list. fetchRowspasses the request todemo.query.fetchRows. The route needs no group code.
Wire the grid
Pass the whole request to the query layer. The request carries groupPath and childKind:
"use client";
import { DataGrid, defineColumns } from "@/components/data-grid/data-grid";
import { useDataGridServerRows } from "@/components/data-grid-server/data-grid-server";
type User = { id: number; role: string; score: number | null };
const columns = defineColumns<User>()([
{ id: "role", header: "Role", accessorKey: "role" },
{
id: "score",
header: "Score",
accessorKey: "score",
type: "number",
aggregate: "avg",
},
]);
export function UsersGrid() {
const server = useDataGridServerRows<User>({
mode: "scroll",
getRowId: (row) => String(row.id),
defaultSpec: { sorts: [], filters: [], searchText: "", groupBy: ["role"] },
fetchRows: async (request) => {
const response = await fetch("/api/users", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify(request),
signal: request.signal,
});
return response.json();
},
});
return <DataGrid {...server.gridProps} columns={columns} readOnly />;
}Related
- Group server rows: the request, the answer, and the checks
- Query layer: answer group requests from Drizzle or Kysely
- Row grouping example: the same groups on a local array
Server pagination example
Fetch one page of rows per request with TanStack Query or SWR, and see the page and page size act as the cache key.
Cell comments example
Write, edit, resolve, and delete comments on cells, first on a client-only grid, then with a simulated second user, a forced conflict, and an offline switch.