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
pnpm dlx shadcn@latest add @gridcn/data-grid-column-drag-demo
What the demo shows
enableColumnReorderlets the user drag a header. The grid shows a drop indicator. The prop defaults totrue.reorderable: falseon Name keeps that column in place, whileenableColumnReorderistrue.- 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 commandsmoveColumnTowardStartandmoveColumnTowardEndhave no default key. Bind them with thekeymapprop, as Column order and pinning shows. - The picker and the Left and Right buttons call
setColumnOrder(selectedId, neighborId, "before" | "after"). The buttons callsetColumnOrderandcanSetColumnOrderfromuseDataGridActions(). A button is off when the move is not possible. That is the case at the ends of the order, next to a column withreorderable: 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>
);
}Related
- Column order and pinning: the drag rules and the layout change event
- Row order example: the same two paths for rows
- Column layout example: save the order with the layout