View as Markdown

Row Drag and Drop

Row drag and drop

Reorder rows by pointer or keyboard, drag several rows, drop into another grid or a custom element, with cancelable events and persisted order.

rowDrag adds a drag handle column; dragging a handle reorders the rows. Selection is not required. api.moveRows(rowIds, toIndex) moves rows by data row index; it also takes { targetRowId, position: 'before' | 'after' | 'into' } to place the rows next to a row. Local data is reordered in place; a data source receives the move through write(), and rowDrag.orderField writes new order values to a field so a server can persist the order.

<DataGrid aria-label="Tasks" data={tasks} columns={columns} rowDrag={{ targetId: 'done' }} onRowDrop={(e) => save(e.rowIds, e.toIndex)} />
<DataGrid aria-label="Done" id="done" data={[]} columns={columns} rowDrag />

Targets

rowDrag.targetId names another grid (by its id) that receives the dragged rows: the target adds them and the source removes them. When the id names any other element, dropping on it ends the drag without moving rows, and onRowDrop carries the rows and the target element. Dragging a selected row drags every selected row. Grids exchange rows through a registry shared by every DataGrid of the page; pass rowDrag.handoff (from createRowDragHandoff()) to give a set of grids their own.

Events

onRowDragStart and onBeforeRowDrop cancel with preventDefault(); onRowDrag fires at most once per animation frame while the target changes; onRowDrop follows the drop. Each carries rowIds, rows(), fromIndex, toIndex, the target row and side (targetRowId, position), valid, reason, targetGridId and the target element. renderRowDragPreview replaces the preview and receives { rowIds, rows }.

Rules

Drops onto a dragged row, into a sorted grid (unless sorted ascending by orderField) or into another group are invalid; rowDrag.acrossGroups allows group changes and rewrites the grouped values. Rows are moved by id over every loaded row, so dragging works with virtualization and auto-scrolls at the edges. Alt+Shift+ArrowUp and Alt+Shift+ArrowDown move the focused (or selected) rows by keyboard. Escape cancels a pointer drag.

Row drag works with row pinning and with hierarchy:

  • Pinned rows (row pinning) are neither drag sources nor drop targets: their place comes from pinning and the sort, not from the row order. They show no drag handle, a pointer over them is an invalid target (reason: 'pinnedRow'), a selected pinned row stays behind when the selection is dragged, and moveRows / keyboard moves never move a pinned row or place a row above the first scrolling row. Data indexes (fromIndex, toIndex, moveRows) count the scrolling rows only. With orderField, pinned rows keep their order values.
  • A child grid (childGrid with its own rowDrag) reorders its own rows only. Its drags and events stay in the child: releasing a child row over the parent grid drops nothing, and the parent's row drag ignores the rows of nested child grids. To move rows between grids, use rowDrag.targetId.

Row drag and drop

Open demo

Reorder rows by the drag handle or the keyboard, drag the selected rows together, and move rows into a second grid, with cancelable drop events.

Reorder
Order
Customer
Priority
ORD-10000
Hana Sato
Normal
ORD-10001
Aiko Tan
Normal
ORD-10002
Farah Aziz
Normal
ORD-10003
Farah Aziz
Normal
ORD-10004
Elif Kaya
High
ORD-10005
Elif Kaya
Normal
ORD-10006
Ines Duarte
Normal
ORD-10007
Chen Wei
Normal
ORD-10008
Farah Aziz
Low
ORD-10009
Farah Aziz
Normal
ORD-10010
Dewi Lestari
High
ORD-10011
Dewi Lestari
Normal
ORD-10012
Farah Aziz
Normal
ORD-10013
Chen Wei
Normal
ORD-10014
Ines Duarte
Normal
ORD-10015
Chen Wei
Normal
ORD-10016
Goran Petrov
Normal
ORD-10017
Dewi Lestari
Normal
ORD-10018
Farah Aziz
Low
ORD-10019
Dewi Lestari
Normal
Reorder
Order
Customer
Priority
No records to display

Drag the grip to reorder, or into the Sprint grid; select several rows to drag them together. Alt+Shift+Up / Down moves the focused row.

No events yet.

API reference

DataGridRowDrag lists the rowDrag options, DataGridRowDragEvent the event payload, DataGridRowDragPreviewContext the preview context and DataGridApi moveRows.

Drop targets and persisted order

Open demo

A ranked list whose drops write new rank values through dataSource.write, and an inbox whose rows drop onto a custom archive element.

Reorder
Task
Title
Rank
No records to display
Reorder
Task
Title
M-1
Mail: Write release notes
M-2
Mail: Fix the login redirect
M-3
Mail: Review the pricing page
M-4
Mail: Book the team offsite
M-5
Mail: Answer support mail
Archive: drop inbox rows here
No events yet.