gridcn2.0.0
Column examples

Column order example

Drag a header to reorder columns inside their pin zone, or move a column with setColumnOrder.

Three paths change the column order: drag a header, use the header menu, or call actions.setColumnOrder(id, targetId, position). The demo offers the drag and the call. The demo sets reorderable: false on Name and pins Score to the end side.

Drag a header to reorder it — the move stays inside its pin zone. Name cannot be moved (reorderable: false) and Score is pinned to the end. The picker below the line moves the chosen column programmatically.

Move
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-drag-demo

What the demo shows

  • enableColumnReorder lets the user drag a header. The grid shows a drop indicator. The prop defaults to true.
  • reorderable: false on Name keeps that column in place, while enableColumnReorder is true.
  • A column moves only inside its pin zone. A drop across zones does nothing. Score is the only column pinned to the end side, so it has nowhere to move.
  • With DataGridContextMenu, the header menu has the items Move toward start and Move toward end. From the keyboard, the commands moveColumnTowardStart and moveColumnTowardEnd have no default key. Bind them with the keymap prop, as Column order and pinning shows.
  • The picker and the Left and Right buttons call setColumnOrder(selectedId, neighborId, "before" | "after"). The buttons call setColumnOrder and canSetColumnOrder from useDataGridActions(). A button is off when the move is not possible. That is the case at the ends of the order, next to a column with reorderable: false, and across a pin zone.

This grid keeps Name in place and moves Email one step later with a button:

import {
  DataGridBody,
  DataGridHeader,
  DataGridProvider,
  DataGridRoot,
  defineColumns,
  useDataGridActions,
} from "@/components/data-grid/data-grid";
import { Button } from "@/components/ui/button";

type Person = { id: string; name: string; email: string; role: string };

const columns = defineColumns<Person>()([
  { id: "name", header: "Name", accessorKey: "name", reorderable: false },
  { id: "email", header: "Email", accessorKey: "email" },
  { id: "role", header: "Role", accessorKey: "role" },
]);

const rows: Person[] = [
  { id: "1", name: "Ada", email: "ada@example.com", role: "Admin" },
];

function MoveEmailButton() {
  const actions = useDataGridActions();
  return (
    <Button
      variant="outline"
      size="sm"
      disabled={!actions.canSetColumnOrder("email", "role", "after").ok}
      onClick={() => actions.setColumnOrder("email", "role", "after")}
    >
      Move Email after Role
    </Button>
  );
}

export function PeopleGrid() {
  return (
    <DataGridProvider defaultData={rows} columns={columns}>
      <MoveEmailButton />
      <DataGridRoot className="h-60">
        <DataGridHeader />
        <DataGridBody />
      </DataGridRoot>
    </DataGridProvider>
  );
}

On this page