Row ordering
Set enableRowOrdering: true to add a drag-handle column for drag-and-drop row reordering.
You own the data: the table reports the move via onRowOrderChange: (activeRowId, overRowId) => void, and it is up to you to reorder your array. Provide getRowId so the ids passed to the callback are stable, and typically set enablePagination: false so dragging spans the full list.
Row ordering
ID | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| Ava | Thompson | Owner | Engineering | active | 22 | $45,000 | Jan 2, 2023 | 0% | |||
| Liam | Nguyen | Admin | Marketing | inactive | 25 | $47,137 | Jan 13, 2023 | 9% | |||
| Noah | Silva | Editor | Design | pending | 28 | $49,274 | Jan 24, 2023 | 18% | |||
| Emma | Carter | Viewer | Sales | active | 31 | $51,411 | Feb 4, 2023 | 27% | |||
| Olivia | Rossi | Owner | Support | inactive | 34 | $53,548 | Feb 15, 2023 | 36% | |||
| William | Walker | Admin | Engineering | pending | 37 | $55,685 | Feb 26, 2023 | 45% | |||
| Sophia | Patel | Editor | Marketing | active | 40 | $57,822 | Mar 9, 2023 | 54% | |||
| James | Muller | Viewer | Design | inactive | 43 | $59,959 | Mar 20, 2023 | 63% | |||
| Isabella | Park | Owner | Sales | pending | 46 | $62,096 | Mar 31, 2023 | 72% | |||
| Lucas | Reyes | Admin | Support | active | 49 | $64,233 | Apr 11, 2023 | 81% | |||
| Mia | Johansson | Editor | Engineering | inactive | 52 | $66,370 | Apr 22, 2023 | 90% | |||
| Benjamin | Costa | Viewer | Marketing | pending | 55 | $68,507 | May 3, 2023 | 99% | |||
| $681,042 |
const [data, setData] = useState(initialData)
const table = useDataTable({
data,
columns,
getRowId: (row) => row.id,
enableRowOrdering: true,
enablePagination: false,
onRowOrderChange: (activeRowId, overRowId) => {
setData((prev) => {
const next = [...prev]
const from = next.findIndex((row) => row.id === activeRowId)
const to = next.findIndex((row) => row.id === overRowId)
const [moved] = next.splice(from, 1)
next.splice(to, 0, moved)
return next
})
},
})
return <DataTable table={table} />
Not compatible with virtualization
Row drag-and-drop is incompatible with row virtualization. Disable virtualization when you enable row ordering.