User Interaction
If you want to allow the user to reorder items on the List, define the itemDragging object and set the allowReordering property within it to true. This setting supplies each List item with a button that enables the user to move the item with drag and drop on mouse-equipped platforms or with touch-and-drag on touch-enabled devices.
- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import List, {
- ItemDragging
- } from 'devextreme-react/list';
- export default function App() {
- return (
- <List ... >
- <ItemDragging
- allowReordering={true}
- />
- </List>
- );
- }
See Also
API
You can reorder List items from code in the following ways.
- By index
Pass the index to the reorderItem(itemIndex, toItemIndex) method. If the List is grouped, this method should be given two objects with the indexes of the groups and the items.
- import React, { useRef } from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import List from 'devextreme-react/list';
- // ...
- export default function App() {
- const list = useRef(null);
- const reorderItems = (index1, index2) => {
- // Place an item with `index1` after an item with `index2`
- list.current.instance().reorderItem(index1, index2);
- };
- const reorderItemsInGroups = (groupIndex1, itemIndex1, groupIndex2, itemIndex2) => {
- // Take an item with index `itemIndex1` from a group with `groupIndex1`
- // and place it to a group with `groupIndex2` after an item with `itemIndex2`
- list.current.instance().reorderItem(
- { group: groupIndex1, item: itemIndex1 },
- { group: groupIndex2, item: itemIndex2 }
- );
- }
- return (
- <List ...
- ref={list}
- />
- );
- }
See Also
Events
To execute certain commands when an item changes its position, handle the itemReordered event. If the event handling function is not going to be changed during the lifetime of the UI component, assign it to the onItemReordered property when you configure the UI component.
- import React from 'react';
- import 'devextreme/dist/css/dx.light.css';
- import List from 'devextreme-react/list';
- const onItemReordered = (e) => {
- const itemData = e.itemData;
- const itemDomNode = e.itemElement;
- const from = e.fromIndex;
- const to = e.toIndex;
- // Handler of the "itemReordered" event
- };
- export default function App() {
- return (
- <List ...
- onItemReordered={onItemReordered}>
- {/* ... */}
- </List>
- );
- }
See Also
If you have technical questions, please create a support ticket in the DevExpress Support Center.