Prevent the Dragging of Nodes to the Root TreeView Level
Environment
Product | Progress® Kendo UI® TreeView for jQuery |
Operating System | Windows 10 64bit |
Visual Studio Version | Visual Studio 2017 |
Preferred Language | JavaScript |
Description
How can I prevent the nodes from being dragged to the root tree level of the Kendo UI for jQuery TreeView?
Solution
The following example demonstrates how to handle the drag
event to achieve this behavior.
<div id="tree"></div>
<script>
$("#tree").kendoTreeView({
dragAndDrop: true,
dataSource: [
{ text: "root 1", expanded: true, items: [
{ text: "bar", expanded: true, items: [
{ text: "baz" },
{ text: "foo" }
] }
] },
{ text: "root 2" }
],
drag: function(e) {
if (e.statusClass == "denied") {
// The TreeView already denies this operation.
return;
} else {
// Whether the action is related to a root node.
var targetsRoot = $(e.dropTarget).parentsUntil(".k-treeview", ".k-item").length == 1;
// If targeting a root node and the operation is not add.
// This means that the operation is to insert before or after the root
// which will create another root.
if (targetsRoot && e.statusClass != "add") {
e.setStatusClass("k-denied");
}
}
}
});
</script>