Basic examples
Minimal grid example
Render a grid from one registry item, three columns, and seed rows, with no state of your own.
The smallest working grid needs the data-grid item, a column list, and defaultData. The grid seeds its rows once from defaultData, then owns them and applies every edit itself.
pnpm dlx shadcn@latest add @gridcn/data-grid-minimal-demo
What the demo shows
DataGridcomposes the provider, the scroll root, the header, and the body.defineColumnstypes three columns:text,number, andcheckbox.defaultDataseeds the rows. The grid ignores later changes to the prop.classNamesizes the grid. The demo setsw-full h-[240px]. The grid needs a bounded height to virtualize. Without one, it draws at most 200 rows.
The grid reads the row id from the id field of each row. If a row has no string or number id field, the grid throws. Pass getRowId to read the id from another field. If the app must own the rows, pass data and onDataChange.
A minimal version of the demo, with its imports:
import { DataGrid, defineColumns } from "@/components/data-grid/data-grid";
type Person = { id: string; name: string; age: number; active: boolean };
const columns = defineColumns<Person>()([
{ id: "name", header: "Name", accessorKey: "name", type: "text" },
{ id: "age", header: "Age", accessorKey: "age", type: "number" },
{ id: "active", header: "Active", accessorKey: "active", type: "checkbox" },
]);
const rows: Person[] = [
{ id: "1", name: "Ada Lovelace", age: 28, active: true },
{ id: "2", name: "Grace Hopper", age: 34, active: true },
];
export function PeopleGrid() {
return <DataGrid defaultData={rows} columns={columns} className="h-60" />;
}Related
- Render your first grid: the same minimum, step by step
- Editable grid example: the split composition with an add-on