Having trouble getting help?
Contact Support
Contact Support
Drag and drop list items in Vue ListView component
19 Feb 20259 minutes to read
The ListView component doesn’t have built-in drag and drop support. However, we can achieve this functionality using the TreeView
component with ListView appearance.
Drag and Drop in the TreeView component is enabled by setting allowDragAndDrop
to true
.
<ejs-treeview id='element' :fields='fields' allowDragAndDrop='true'></ejs-treeview>
The TreeView component is used to represent hierarchical data in a tree-like structure. As a result, list items in TreeView can be dropped as children of the target element. We can prevent this behavior by cancelling thenodeDragStop
and nodeDragging
events.
<ejs-treeview id='element' :fields='fields' allowDragAndDrop='true' :nodeDragging='onDragStop' :nodeDragStop='onDragStop'></ejs-treeview>
fields: { dataSource: data, id: 'id', text: 'text' },
onDragStop: function(args) {
//Block the Child Drop operation in TreeView.
let draggingItem = document.getElementsByClassName("e-drop-in");
if (draggingItem.length == 1) {
draggingItem[0].classList.add('e-no-drop');
args.cancel = true;
}
}
In the sample below, we have rendered draggable list items.
<template>
<div id="sample">
<ejs-treeview id='element' :fields='fields' allowDragAndDrop='true' :nodeDragging='onDragStop'
:nodeDragStop='onDragStop'></ejs-treeview>
</div>
</template>
<script setup>
import { TreeViewComponent as EjsTreeview } from "@syncfusion/ej2-vue-navigations";
var data = [
{ 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' },
];
const fields = { dataSource: data, id: 'id', text: 'text' };
const onDragStop = (args) => {
//Block the Child Drop operation in TreeView
let draggingItem = document.getElementsByClassName("e-drop-in");
if (draggingItem.length == 1) {
draggingItem[0].classList.add('e-no-drop');
args.cancel = true;
}
};
</script>
<style>
#sample {
display: block;
max-width: 280px;
margin: auto;
border: 1px solid #dddddd;
border-radius: 3px;
}
#element.e-treeview .e-ul {
padding: 0;
}
#element.e-treeview .e-list-item {
padding: 0 16px;
}
#element.e-treeview .e-text-content {
padding: 0;
}
#element.e-treeview .e-fullrow {
height: 36px;
}
#element.e-treeview .e-list-text {
line-height: 34px;
}
#element.e-treeview .e-list-item:last-child {
margin-bottom: 9px;
}
#element.e-treeview .e-list-item:first-child {
margin-top: 9px;
}
</style>
<template>
<div id="sample">
<ejs-treeview id='element' :fields='fields' allowDragAndDrop='true' :nodeDragging='onDragStop'
:nodeDragStop='onDragStop'></ejs-treeview>
</div>
</template>
<script>
import { TreeViewComponent } from "@syncfusion/ej2-vue-navigations";
export default {
name: "App",
components: {
"ejs-treeview": TreeViewComponent
},
data: function () {
var data = [
{ 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' },
];
return {
fields: { dataSource: data, id: 'id', text: 'text' },
};
},
methods: {
onDragStop: function (args) {
//Block the Child Drop operation in TreeView
let draggingItem = document.getElementsByClassName("e-drop-in");
if (draggingItem.length == 1) {
draggingItem[0].classList.add('e-no-drop');
args.cancel = true;
}
}
}
}
</script>
<style>
#sample {
display: block;
max-width: 280px;
margin: auto;
border: 1px solid #dddddd;
border-radius: 3px;
}
#element.e-treeview .e-ul {
padding: 0;
}
#element.e-treeview .e-list-item {
padding: 0 16px;
}
#element.e-treeview .e-text-content {
padding: 0;
}
#element.e-treeview .e-fullrow {
height: 36px;
}
#element.e-treeview .e-list-text {
line-height: 34px;
}
#element.e-treeview .e-list-item:last-child {
margin-bottom: 9px;
}
#element.e-treeview .e-list-item:first-child {
margin-top: 9px;
}
</style>