The KendoReact Grid comes with a built-in reordering column type, that allows you to easily change the order of each row by dragging it to the target position.
KendoReact Drag&DropImplementing row reordering requires the following steps:
rowReorderable
prop of the Grid.<Grid rowReorderable={{ enabled: true }}>
columnType
to render the built-in drag handles to every row.<Column columnType="reorder" width={60} />
onRowReorder
event of the Grid in order to update the data with the new row indexes. You can use the dropPosition
available in the GridRowReorderEvent
object to calculate the target position of the dragged row. <Grid onRowReorder={handleRowReorder}>
const handleRowReorder = (event: GridRowReorderEvent) => {
const reorderedData = [...data];
const droppedItemIndex = data.findIndex((item) => item === event.droppedDataItem);
event.draggedDataItems.forEach((draggedItem) => {
const draggedItemIndex = data.findIndex((item) => item === draggedItem);
const idxToAdd: number = calculateIndexToAdd(draggedItemIndex, droppedItemIndex, event.dropPosition)!;
reorderedData.splice(draggedItemIndex, 1);
reorderedData.splice(idxToAdd, 0, event.draggedDataItems[0]);
});
setData(reorderedData);
};
const calculateIndexToAdd = (dragIndex: number, dropIndex: number, dropPosition: GridReorderDropPosition) => {
const isDropPosition = dropPosition === 'after';
if (dragIndex > dropIndex) {
return isDropPosition ? dropIndex + 1 : dropIndex;
}
return isDropPosition ? dropIndex : dropIndex - 1;
};
The following example demonstrates this approach in action:
Built-in Browser APIsThe following example demonstrates one of the possible approaches which utilizes the HTML Drag and Drop API.
Suggested LinksRetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4