gridcn2.0.0
Column examples

Column layout example

Save column widths, order, pins, and hidden columns with onColumnLayoutChange and restore them with defaultColumnLayout.

defaultColumnLayout seeds the widths, order, pins, and hidden columns once at mount. onColumnLayoutChange reports the layout after each change. The demo saves the layout to localStorage. Resize a header, drag a column, or hide one, then reload the page to see the grid restore the layout.

Resize a header edge, drag a header, or pin/hide a column (header menu / columns menu) — every change persists and is restored on the next reload. Current order: (defaults or saved)

Name
Role
Age
Joined
Score
Silent Tiger
Viewer
63
2023-07-05
4
Quick Lion
Viewer
67
2021-04-03
89
Swift Wolf
Manager
40
2020-10-27
80
Eager Lion
User
50
2022-10-08
51
Eager Tiger
Viewer
45
2024-10-07
83
Silent Wolf
User
24
2020-06-01
15
Swift Lion
Viewer
34
2022-10-18
62
Eager Lion
Manager
21
2021-10-07
42
Eager Eagle
Editor
25
2024-06-11
35
Bold Tiger
Editor
19
2024-03-20
52
pnpm dlx shadcn@latest add @gridcn/data-grid-column-layout-demo

What the demo shows

  • defaultColumnLayout={readLayout} passes a function. The grid calls it once on the client after mount, before the first paint, so the server renders the default layout and never reads localStorage. The grid ignores later changes to this prop.
  • onColumnLayoutChange={persist} runs once for each committed change: a resize release, a reorder drop, a pin or unpin, and a hide or show. It does not run for each drag frame.
  • DataGridColumnsMenu hides and restores columns, which updates hidden.
  • Reset layout clears the saved layout and mounts the grid again with a new key, because the grid reads defaultColumnLayout once.
  • The text above the grid lists the order of the most recent snapshot.

The snapshot

onColumnLayoutChange receives the whole layout after each change. widths lists only the columns that the user resized. order lists every column id. pins maps a column id to "start" or "end", and hidden lists the hidden column ids. This grid saves the layout and restores it after mount:

"use client";

import {
  DataGrid,
  defineColumns,
  type ColumnLayout,
} from "@/components/data-grid/data-grid";

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" },
]);

const rows: Person[] = [{ id: "1", name: "Ada", age: 36 }];

const STORAGE_KEY = "people-grid-layout";

function readLayout(): ColumnLayout | undefined {
  try {
    const raw = window.localStorage.getItem(STORAGE_KEY);
    return raw ? (JSON.parse(raw) as ColumnLayout) : undefined;
  } catch {
    return undefined;
  }
}

function saveLayout(layout: ColumnLayout) {
  try {
    window.localStorage.setItem(STORAGE_KEY, JSON.stringify(layout));
  } catch {
    return;
  }
}

export function PeopleGrid() {
  return (
    <DataGrid
      defaultData={rows}
      columns={columns}
      className="h-60"
      defaultColumnLayout={readLayout}
      onColumnLayoutChange={saveLayout}
    />
  );
}

To reset without a remount, pass columnLayout with onColumnLayoutChange.

On this page