Drag and drop list items in listview in EJ2 TypeScript Listview control
8 Aug 20234 minutes to read
In ListView control, we don’t have drag and drop support. But we can achieve this requirement using TreeView
control with ListView appearance.
Drag and Drop in TreeView control was enabled by setting allowDragAndDrop
to true
.
let treeViewInstance: TreeView = new TreeView({
fields: { dataSource: data, id: 'id', text: 'text' },
allowDragAndDrop: true
});
The TreeView control is used to represent hierarchical data in a tree like structure. So, list items in TreeView can be dropped to child of target element. we can prevent this behaviour by cancelling the nodeDragStop
and nodeDragging
events.
let treeViewInstance: TreeView = new TreeView({
fields: { dataSource: data, id: 'id', text: 'text' },
allowDragAndDrop: true,
nodeDragging: onDragStop,
nodeDragStop: onDragStop
});
function onDragStop(args: DragAndDropEventArgs) {
//Block the Child Drop operation in TreeView
let draggingItem: HTMLCollection = document.getElementsByClassName("e-drop-in");
if (draggingItem.length == 1) {
draggingItem[0].classList.add('e-no-drop');
args.cancel = true;
}
}
In the below sample, we have rendered draggable list items.
import { TreeView, DragAndDropEventArgs } from '@syncfusion/ej2-navigations';
let data: { [key: string]: Object }[] = [
{ text: 'Hennessey Venom', id: 'list-01' },
{ text: 'Bugatti Chiron', id: 'list-02' },
{ text: 'Bugatti Veyron Super Sport', id: 'list-03' },
{ text: 'SSC Ultimate Aero', id: 'list-04' },
{ text: 'Koenigsegg CCR', id: 'list-05' },
{ text: 'McLaren F1', id: 'list-06' },
{ text: 'Aston Martin One- 77', id: 'list-07' },
{ text: 'Jaguar XJ220', id: 'list-08' },
{ text: 'McLaren P1', id: 'list-09' },
{ text: 'Ferrari LaFerrari', id: 'list-10' },
];
let treeViewInstance: TreeView = new TreeView({
fields: { dataSource: data, id: 'id', text: 'text' },
allowDragAndDrop: true,
nodeDragging: onDragStop,
nodeDragStop: onDragStop
});
treeViewInstance.appendTo('#element');
function onDragStop(args: DragAndDropEventArgs) {
//Block the Child Drop operation in TreeView
let draggingItem: HTMLCollection = document.getElementsByClassName("e-drop-in");
if (draggingItem.length == 1) {
draggingItem[0].classList.add('e-no-drop');
args.cancel = true;
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Essential JS 2 for ListView </title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Essential JS 2 for ListView UI Control" />
<meta name="author" content="Syncfusion" />
<link href="index.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/27.1.48/ej2-base/styles/material.css" rel="stylesheet" />
<link href="https://cdn.syncfusion.com/ej2/27.1.48/ej2-navigations/styles/material.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.38/system.js"></script>
<script src="systemjs.config.js"></script>
<script src="https://cdn.syncfusion.com/ej2/syncfusion-helper.js" type ="text/javascript"></script>
</head>
<body>
<div id='loader'>Loading....</div>
<div id='container'>
<div id='element'></div>
</div>
</body>
</html>