Drag and drop in React List box component
2 Feb 202311 minutes to read
The ListBox has support to drag an item or a group of selected items and drop it within the same list box or into another list box.
The elements can be customized on drag and drop by using the following events,
Events | Description |
---|---|
dragStart |
Triggers when the selected element is being dragged. |
drag |
Triggers when the selected element is being dragged. |
drop |
Triggers when the selected element is being dropped. |
Single listbox
To drag and drop an item or group of item within the list box can be achieved by setting allowDragAndDrop
property as true
.
The following sample illustrates how to drag and drop an item within the same list box by enabling allowDragAndDrop
property.
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { ListBoxComponent } from '@syncfusion/ej2-react-dropdowns';
function App() {
let data = [
{ "Name": "Australia", "Code": "AU" },
{ "Name": "Bermuda", "Code": "BM" },
{ "Name": "Canada", "Code": "CA" },
{ "Name": "Cameroon", "Code": "CM" },
{ "Name": "Denmark", "Code": "DK" },
{ "Name": "France", "Code": "FR" },
{ "Name": "Finland", "Code": "FI" },
{ "Name": "Germany", "Code": "DE" },
{ "Name": "Hong Kong", "Code": "HK" }
];
let fields = { text: "Name" };
return (<ListBoxComponent dataSource={data} allowDragAndDrop="true" fields={fields}/>);
}
export default App;
ReactDOM.render(<App />, document.getElementById('sample'));
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { ListBoxComponent } from '@syncfusion/ej2-react-dropdowns';
function App() {
let data: { [key: string]: Object }[] = [
{ "Name": "Australia", "Code": "AU" },
{ "Name": "Bermuda", "Code": "BM" },
{ "Name": "Canada", "Code": "CA" },
{ "Name": "Cameroon", "Code": "CM" },
{ "Name": "Denmark", "Code": "DK" },
{ "Name": "France", "Code": "FR" },
{ "Name": "Finland", "Code": "FI" },
{ "Name": "Germany", "Code": "DE" },
{ "Name": "Hong Kong", "Code": "HK" }
];
let fields:object = { text:"Name"}
return (
<ListBoxComponent dataSource={data} allowDragAndDrop="true" fields={fields} />
);
}
export default App;
ReactDOM.render(<App />, document.getElementById('sample'));
Multiple listbox
To drag and drop an item or group of item between two list boxes can be achieved by setting allowDragAndDrop
property as true
and scope
property should be set to both the list boxes.
In the following sample, the allowDragAndDrop
property is set as true
and scope
is set as combined-list
to enable drop and drop in both list boxes.
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { ListBoxComponent } from '@syncfusion/ej2-react-dropdowns';
function App() {
let groupA: { [key: string]: Object }[] = [
{ "Name": "Australia", "Code": "AU" },
{ "Name": "Bermuda", "Code": "BM" },
{ "Name": "Canada", "Code": "CA" },
{ "Name": "Cameroon", "Code": "CM" },
{ "Name": "Denmark", "Code": "DK" },
{ "Name": "France", "Code": "FR" },
{ "Name": "Finland", "Code": "FI" },
{ "Name": "Germany", "Code": "DE" },
{ "Name": "Hong Kong", "Code": "HK" }
];
let groupB: { [key: string]: Object }[] = [
{ "Name": "India", "Code": "IN" },
{ "Name": "Italy", "Code": "IT" },
{ "Name": "Japan", "Code": "JP" },
{ "Name": "Mexico", "Code": "MX" },
{ "Name": "Norway", "Code": "NO" },
{ "Name": "Poland", "Code": "PL" },
{ "Name": "Switzerland", "Code": "CH" },
{ "Name": "United Kingdom", "Code": "GB" },
{ "Name": "United States", "Code": "US" }
];
let fields:object = { text:"Name"};
return (
<div className="wrapper">
<div className="listbox1">
<h4>Group A</h4>
<ListBoxComponent dataSource={groupA} allowDragAndDrop="true" height="330px" scope="combined-list" fields={fields}/></div>
<div className="listbox2">
<h4>Group B</h4>
<ListBoxComponent dataSource={groupB} allowDragAndDrop="true" height="330px" scope="combined-list" fields={fields} /></div>
</div>
);
}
export default App;
ReactDOM.render(<App />, document.getElementById('sample'));