Row order example
Reorder one row or many rows by the drag handle, the keyboard, or code, and undo the move in one step.
Three paths reorder rows. Drag the handle in the marker column. Press Shift+Alt+PageDown or Shift+Alt+PageUp. Or call reorderRows or moveRowsBy. Each path reports one change with one move operation per moved row. If the pressed row has a check, the drag moves every checked row. The rows land together at the drop line.
move operations show up here.pnpm dlx shadcn@latest add @gridcn/data-grid-row-reorder-demo
What the demo shows
rowMarkers="reorder-checkbox"puts a drag handle and a checkbox in the marker column.- A thin line marks the drop target while you drag. The row moves on release.
- The Move down button runs the
moveRowsDownshortcut. The shortcut callsmoveRowsByfor the checked rows. With no checked row, it moves the rows of the selected range, or the active row. - The Undo button comes from the history add-on. One click restores the order before the move.
- The panel under the grid prints the number of
moveoperations, the first moved row, and thesource.
Wire the paths
The checked rows and the active cell follow their rows after a move. This sample adds a Move down button and an Undo button:
import {
DataGridBody,
DataGridHeader,
DataGridProvider,
DataGridRoot,
defineColumns,
useDataGridApi,
} from "@/components/data-grid/data-grid";
import {
useDataGridState,
} from "@/components/data-grid-history/data-grid-history";
import { Button } from "@/components/ui/button";
type Row = { id: string; title: string };
const columns = defineColumns<Row>()([
{ id: "title", header: "Title", accessorKey: "title" },
]);
const rows: Row[] = [
{ id: "1", title: "Intro" },
{ id: "2", title: "Verse" },
{ id: "3", title: "Chorus" },
];
function MoveDownButton() {
const api = useDataGridApi();
return (
<Button onClick={() => api.runShortcut("moveRowsDown")}>Move down</Button>
);
}
function Playlist() {
const grid = useDataGridState(rows);
return (
<DataGridProvider
{...grid}
columns={columns}
rowMarkers="reorder-checkbox"
onDataChange={(next, change) => {
grid.onDataChange(next, change);
console.log(change.source, change.ops);
}}
>
<MoveDownButton />
<Button disabled={!grid.history.canUndo} onClick={grid.history.undo}>
Undo
</Button>
<DataGridRoot>
<DataGridHeader />
<DataGridBody />
</DataGridRoot>
</DataGridProvider>
);
}Drag and code compared
| Handle drag | reorderRows(rows, to) | moveRowsBy(rows, delta) | |
|---|---|---|---|
| Input | The pressed row. A pressed row with a check drags every checked row | View indexes and the final index of the first row | View indexes and an offset |
| Rows land | Together at the drop line | Together, from to on | Each row moves by delta, and the gaps stay |
to is a final position, not an offset. Moving row 0 to index 3 shifts rows 1 to 3 up. The keyboard keys call moveRowsBy. The keys and runShortcut("moveRowsUp") or runShortcut("moveRowsDown") work only with a reorder rowMarkers mode ("reorder" or "reorder-checkbox"). Without one, they do nothing and fire onRowActionSkipped with reason: "disabled".
When a move does nothing
reorderRows and moveRowsBy return false and change nothing in these cases:
- The grid is
readOnly. - A sort is active, or a client filter is active. With
sortMode="server", the next fetch puts a moved row back in the server order. - The grid holds unloaded rows in scroll mode.
- The move leaves the rows where they are.
- The list is empty, an index is outside the grid, or
toleaves no room for the rows. - For
moveRowsBy,deltais 0. - For
moveRowsBy, a row leaves the grid.
Each no-op fires onRowActionSkipped with action: "moveRows" and a reason: readOnly, disabled, view-ordered (a sort or filter), unloaded-rows, or nothing-to-do (any other case above). On a paged grid, a move acts on the rows of the shown page.
Related
- Row order: the handle zone, the keys, and the server contract
- Row markers: the reorder marker modes
- Undo and redo: the history add-on behind the Undo button
- Row operations example: insert, duplicate, and delete