$(() => {
createTreeView('#treeviewDriveC', itemsDriveC);
createTreeView('#treeviewDriveD', itemsDriveD);
createSortable('#treeviewDriveC', 'driveC');
createSortable('#treeviewDriveD', 'driveD');
});
function createTreeView(selector, items) {
$(selector).dxTreeView({
items,
expandNodesRecursive: false,
dataStructure: 'plain',
width: 250,
height: 380,
displayExpr: 'name',
});
}
function createSortable(selector, driveName) {
$(selector).dxSortable({
filter: '.dx-treeview-item',
group: 'shared',
data: driveName,
allowDropInsideItem: true,
allowReordering: true,
onDragChange(e) {
if (e.fromComponent === e.toComponent) {
const $nodes = e.element.find('.dx-treeview-node');
const isDragIntoChild = $nodes.eq(e.fromIndex).find($nodes.eq(e.toIndex)).length > 0;
if (isDragIntoChild) {
e.cancel = true;
}
}
},
onDragEnd(e) {
if (e.fromComponent === e.toComponent && e.fromIndex === e.toIndex) {
return;
}
const fromTreeView = getTreeView(e.fromData);
const toTreeView = getTreeView(e.toData);
const fromNode = findNode(fromTreeView, e.fromIndex);
const toNode = findNode(toTreeView, calculateToIndex(e));
if (e.dropInsideItem && toNode !== null && !toNode.itemData.isDirectory) {
return;
}
const fromTopVisibleNode = getTopVisibleNode(fromTreeView);
const toTopVisibleNode = getTopVisibleNode(toTreeView);
const fromItems = fromTreeView.option('items');
const 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) {
const 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 (let i = 0; i < nodes.length; i += 1) {
if (nodes[i].itemData.id === id) {
return nodes[i];
}
if (nodes[i].children) {
const node = findNodeById(nodes[i].children, id);
if (node != null) {
return node;
}
}
}
return null;
}
function moveNode(fromNode, toNode, fromItems, toItems, isDropInsideItem) {
const fromIndex = findIndex(fromItems, fromNode.itemData.id);
fromItems.splice(fromIndex, 1);
const 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((child) => {
if (child.itemData.isDirectory) {
moveChildren(child, fromItems, toItems);
}
const fromIndex = findIndex(fromItems, child.itemData.id);
fromItems.splice(fromIndex, 1);
toItems.splice(toItems.length, 0, child.itemData);
});
}
function findIndex(array, id) {
const idsArray = array.map((elem) => elem.id);
return idsArray.indexOf(id);
}
function getTopVisibleNode(component) {
const treeViewElement = component.element().get(0);
const treeViewTopPosition = treeViewElement.getBoundingClientRect().top;
const nodes = treeViewElement.querySelectorAll('.dx-treeview-node');
for (let i = 0; i < nodes.length; i += 1) {
const nodeTopPosition = nodes[i].getBoundingClientRect().top;
if (nodeTopPosition >= treeViewTopPosition) {
return nodes[i];
}
}
return null;
}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title>DevExtreme Demo</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>window.jQuery || document.write(decodeURIComponent('%3Cscript src="js/jquery.min.js"%3E%3C/script%3E'))</script>
<link rel="stylesheet" type="text/css" href="https://cdn3.devexpress.com/jslib/24.1.7/css/dx.light.css" />
<script src="js/dx.all.js"></script>
<script src="data.js"></script>
<link rel="stylesheet" type="text/css" href="styles.css" />
<script src="index.js"></script>
</head>
<body class="dx-viewport">
<div class="demo-container">
<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
>
<div id="treeviewDriveC"></div>
</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
>
<div id="treeviewDriveD"></div>
</div>
</div>
</div>
</body>
</html>
.form {
display: flex;
}
.form > div {
display: inline-block;
vertical-align: top;
}
.dx-treeview-item {
box-sizing: border-box;
}
.drive-header {
min-height: auto;
padding: 0;
cursor: default;
margin-bottom: 10px;
}
.drive-panel {
padding: 20px 30px;
font-size: 115%;
font-weight: bold;
border-right: 1px solid rgba(165, 165, 165, 0.4);
height: 100%;
}
.drive-panel:last-of-type {
border-right: none;
}
const itemsDriveD = [];
const itemsDriveC = [{
id: '1',
name: 'Documents',
icon: 'activefolder',
isDirectory: true,
expanded: true,
}, {
id: '2',
parentId: '1',
name: 'Projects',
icon: 'activefolder',
isDirectory: true,
expanded: true,
}, {
id: '3',
parentId: '2',
name: 'About.rtf',
icon: 'file',
isDirectory: false,
expanded: true,
}, {
id: '4',
parentId: '2',
name: 'Passwords.rtf',
icon: 'file',
isDirectory: false,
expanded: true,
}, {
id: '5',
parentId: '2',
name: 'About.xml',
icon: 'file',
isDirectory: false,
expanded: true,
}, {
id: '6',
parentId: '2',
name: 'Managers.rtf',
icon: 'file',
isDirectory: false,
expanded: true,
}, {
id: '7',
parentId: '2',
name: 'ToDo.txt',
icon: 'file',
isDirectory: false,
expanded: true,
}, {
id: '8',
name: 'Images',
icon: 'activefolder',
isDirectory: true,
expanded: true,
}, {
id: '9',
parentId: '8',
name: 'logo.png',
icon: 'file',
isDirectory: false,
expanded: true,
}, {
id: '10',
parentId: '8',
name: 'banner.gif',
icon: 'file',
isDirectory: false,
expanded: true,
}, {
id: '11',
name: 'System',
icon: 'activefolder',
isDirectory: true,
expanded: true,
}, {
id: '12',
parentId: '11',
name: 'Employees.txt',
icon: 'file',
isDirectory: false,
expanded: true,
}, {
id: '13',
parentId: '11',
name: 'PasswordList.txt',
icon: 'file',
isDirectory: false,
expanded: true,
}, {
id: '14',
name: 'Description.rtf',
icon: 'file',
isDirectory: false,
expanded: true,
}, {
id: '15',
icon: 'file',
name: 'Description.txt',
isDirectory: false,
expanded: true,
}];