If you have technical questions, please create a support ticket in the DevExpress Support Center.
<div class="form">
<div class="drive-panel">
<div class="drive-header dx-treeview-item"><div class="dx-treeview-item-content"><i class="dx-icon dx-icon-activefolder"></i><span>Drive C:</span></div></div>
Html.DevExtreme().Sortable() (
.Filter(".dx-treeview-item")
.Data("driveC")
.Group("shared")
.AllowReordering(true)
.AllowDropInsideItem(true)
.OnDragChange("onDragChange")
.OnDragEnd("onDragEnd")
.Content(
Html.DevExtreme().TreeView()
.ID("treeviewDriveC")
.ExpandNodesRecursive(false)
.DataStructure(TreeViewDataStructure.Plain)
.KeyExpr("Id")
.ParentIdExpr("ParentId")
.ExpandedExpr("IsExpanded")
.DisplayExpr("Name")
.DataSource(d => d.Mvc().LoadAction("GetPlainDataForDragAndDrop"))
.DataSourceOptions(o => o.Map("mapIcons"))
.Width(250)
.Height(380).ToString()
))
</div>
<div class="drive-panel">
<div class="drive-header dx-treeview-item"><div class="dx-treeview-item-content"><i class="dx-icon dx-icon-activefolder"></i><span>Drive D:</span></div></div>
Html.DevExtreme().Sortable() (
.Filter(".dx-treeview-item")
.Data("driveD")
.Group("shared")
.AllowReordering(true)
.AllowDropInsideItem(true)
.OnDragChange("onDragChange")
.OnDragEnd("onDragEnd")
.Content(
Html.DevExtreme().TreeView()
.ID("treeviewDriveD")
.ExpandNodesRecursive(false)
.DataStructure(TreeViewDataStructure.Plain)
.KeyExpr("Id")
.ParentIdExpr("ParentId")
.ExpandedExpr("IsExpanded")
.DisplayExpr("Name")
.Width(250)
.Height(380).ToString()
)
)
</div>
</div>
<script>
function onDragChange(e) {
if(e.fromComponent === e.toComponent) {
var $nodes = e.element.find(".dx-treeview-node");
var isDragIntoChild = $nodes.eq(e.fromIndex).find($nodes.eq(e.toIndex)).length > 0;
if(isDragIntoChild) {
e.cancel = true;
}
}
}
function onDragEnd(e) {
if(e.fromComponent === e.toComponent && e.fromIndex === e.toIndex) {
return;
}
var fromTreeView = getTreeView(e.fromData);
var toTreeView = getTreeView(e.toData);
var fromNode = findNode(fromTreeView, e.fromIndex);
var toNode = findNode(toTreeView, calculateToIndex(e));
if(e.dropInsideItem && toNode !== null && !toNode.itemData.IsDirectory) {
return;
}
var fromTopVisibleNode = getTopVisibleNode(fromTreeView);
var toTopVisibleNode = getTopVisibleNode(toTreeView);
var fromItems = fromTreeView.option('items');
var toItems = toTreeView.option('items');
moveNode(fromNode, toNode, fromItems, toItems, e.dropInsideItem);
fromTreeView.option("items", fromItems);
toTreeView.option("items", toItems);
fromTreeView.scrollToItem(fromTopVisibleNode);
toTreeView.scrollToItem(toTopVisibleNode);
}
function getTreeView(driveName) {
return driveName === 'driveC'
? $('#treeviewDriveC').dxTreeView('instance')
: $('#treeviewDriveD').dxTreeView('instance');
}
function calculateToIndex(e) {
if(e.fromComponent != e.toComponent || e.dropInsideItem) {
return e.toIndex;
}
return e.fromIndex >= e.toIndex
? e.toIndex
: e.toIndex + 1;
}
function findNode(treeView, index) {
var nodeElement = treeView.element().find('.dx-treeview-node')[index];
if(nodeElement) {
return findNodeById(treeView.getNodes(), nodeElement.getAttribute('data-item-id'));
}
return null;
}
function findNodeById(nodes, id) {
for(var i = 0; i < nodes.length; i++) {
if(nodes[i].itemData.Id == id) {
return nodes[i];
}
if(nodes[i].children) {
var node = findNodeById(nodes[i].children, id);
if(node != null) {
return node;
}
}
}
return null;
}
function moveNode(fromNode, toNode, fromItems, toItems, isDropInsideItem) {
var fromIndex = findIndex(fromItems, fromNode.itemData.Id);
fromItems.splice(fromIndex, 1);
var toIndex = toNode === null || isDropInsideItem
? toItems.length
: findIndex(toItems, toNode.itemData.Id);
toItems.splice(toIndex, 0, fromNode.itemData);
moveChildren(fromNode, fromItems, toItems);
if(isDropInsideItem) {
fromNode.itemData.ParentId = toNode.itemData.Id;
} else {
fromNode.itemData.ParentId = toNode != null
? toNode.itemData.ParentId
: undefined;
}
}
function moveChildren(node, fromItems, toItems) {
if(!node.itemData.IsDirectory) {
return;
}
node.children.forEach(function(child) {
if(child.itemData.IsDirectory) {
moveChildren(child, fromItems, toItems);
}
var fromIndex = findIndex(fromItems, child.itemData.Id);
fromItems.splice(fromIndex, 1);
toItems.splice(toItems.length, 0, child.itemData);
});
}
function findIndex(array, id) {
var idsArray = array.map(function(elem) { return elem.Id; });
return idsArray.indexOf(id);
}
function getTopVisibleNode(component) {
var treeViewElement = component.element().get(0);
var treeViewTopPosition = treeViewElement.getBoundingClientRect().top;
var nodes = treeViewElement.querySelectorAll(".dx-treeview-node");
for(var i = 0; i < nodes.length; i++) {
var nodeTopPosition = nodes[i].getBoundingClientRect().top;
if(nodeTopPosition >= treeViewTopPosition) {
return nodes[i];
}
}
return null;
}
function mapIcons(item) {
if (item.Icon) {
item.icon = item.Icon;
}
return item;
}
</script>
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxx
Use Sortable to implement the necessary drag and drop functionality within your web app. The following steps outline configuration requirements for our JavaScript TreeView:
-
Allow users to reorder nodes
Wrap the TreeView in a Sortable and enable the Sortable's allowReordering property. -
Allow users to change node hierarchy
Enable the allowDropInsideItem property so that users can drop one node onto another. This adds it as the target node's child. If this property is disabled, users can only drop nodes between other nodes. -
Allow users to drag only tree view nodes
To specify tree view nodes as drag targets, set the filter property to a class selector. Since all tree view nodes use thedx-treeview-node
class, you can use this class selector as needed. -
Prevent a node from being moved into its child node
When a user moves a parent node into its own child node, it breaks the hierarchy. To prevent this outcome, implement the onDragChange function and traverse up the node tree. If the target is a child of the dragged node,cancel
the ability to drop the node. -
Reorder nodes in code
Implement the onDragEnd function. In this function, you must gather information about nodes being moved. With this information, you can reorder the nodes in the data source (see themoveNode
function), and reassign the data source to the TreeView's items property. -
Specify tree view identifiers (for drag and drop between multiple tree views only)
Identifiers help distinguish between multiple tree views. Save them in the Sortable's data property. The tree views below include the following identifiers: "driveC" and "driveD". -
Combine tree views into one drag and drop group (for drag and drop between multiple tree views only)
Set the Sortable's group property to the same value for all tree views. This allows users to move nodes between the tree views.