A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.telerik.com/kendo-react-ui/components/grid/rows/row-reordering below:

React Data Grid Rows Reordering of Rows

New to KendoReact? Learn about KendoReact Free. Reordering of Rows

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&Drop

Implementing row reordering requires the following steps:

  1. Enable the rowReorderable prop of the Grid.
<Grid rowReorderable={{ enabled: true }}>
  1. Add Grid column with reorder columnType to render the built-in drag handles to every row.
<Column columnType="reorder" width={60} />
  1. Handle the 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 APIs

The following example demonstrates one of the possible approaches which utilizes the HTML Drag and Drop API.

Suggested Links

RetroSearch 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