gridcn2.0.0
Row examples

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.

Row markers
Name
Email
Role
Score
Silent Tiger
user0@example.com
Viewer
4
Quick Lion
user1@example.com
Viewer
89
Swift Wolf
user2@example.com
Manager
80
Eager Lion
user3@example.com
User
51
Eager Tiger
user4@example.com
Viewer
83
Silent Wolf
user5@example.com
User
15
Swift Lion
user6@example.com
Viewer
62
Eager Lion
user7@example.com
Manager
42
Eager Eagle
user8@example.com
Editor
35
Bold Tiger
user9@example.com
Editor
52
Bold Bear
user10@example.com
User
28
Swift Wolf
user11@example.com
Manager
9
Check some rows, then drag one by its drag handle or press Move down — the emitted 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 moveRowsDown shortcut. The shortcut calls moveRowsBy for 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 move operations, the first moved row, and the source.

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 dragreorderRows(rows, to)moveRowsBy(rows, delta)
InputThe pressed row. A pressed row with a check drags every checked rowView indexes and the final index of the first rowView indexes and an offset
Rows landTogether at the drop lineTogether, from to onEach 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 to leaves no room for the rows.
  • For moveRowsBy, delta is 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.

On this page