Set the allowDragging property to true to allow users to drag nodes to new positions within the TreeView.
When dragging is allowed, users may drag any node to any position within the tree. Specifically, nodes can be dragged to a position above, below, or into (as a child of) other nodes.
You can customize this behavior by handling the TreeView drag/drop events:
For more information on usage of these events, see the Drag and Drop demo.
<div id="theTree"></div>
onload = function() {
// create the tree
var tree = new wjNav.TreeView('#theTree', {
itemsSource: getData(),
displayMemberPath: 'header',
childItemsPath: 'items',
showCheckboxes: true,
imageMemberPath: 'img',
allowDragging: true
});
// get the tree data
function getData() {
var imgUrl = 'https://demos.wijmo.com/5/PureJS/TreeViewIntro/TreeViewIntro/resources/';
return [
{ header: 'Electronics', img: imgUrl + 'electronics.png', items: [
{ header: 'Trimmers/Shavers' },
{ header: 'Tablets' },
{ header: 'Phones', img: imgUrl + 'phones.png', items: [
{ header: 'Apple' },
{ header: 'Motorola', newItem: true },
{ header: 'Nokia' },
{ header: 'Samsung' }]
},
{ header: 'Speakers', newItem: true },
{ header: 'Monitors' }]
},
{ header: 'Toys', img: imgUrl + '/toys.png', items: [
{ header: 'Shopkins' },
{ header: 'Train Sets' },
{ header: 'Science Kit', newItem: true },
{ header: 'Play-Doh' },
{ header: 'Crayola' } ]
},
{ header: 'Home', img: imgUrl + 'home.png', items: [
{ header: 'Coffeee Maker' },
{ header: 'Breadmaker', newItem: true },
{ header: 'Solar Panel', newItem: true },
{ header: 'Work Table' },
{ header: 'Propane Grill' }]
}
];
}
}
Setting the allowDrag property to true allows users to drag and drop nodes within the same TreeView.
To allow dragging and dropping nodes between different TreeView controls, you must handle the dragOver event and set the cancel parameter to true if the operation is invalid, or to false if it is valid.
.wj-treeview {
height: 250px;
font-size: 120%;
margin-top: 8px;
margin-bottom: 8px;
padding: 3px;
background: #f0f0f0;
box-shadow: 0 3px 6px rgba(0,0,0,0.16), 0 3px 6px rgba(0,0,0,0.23);
}
body
{
margin-bottom: 24pt;
}
<div class="container">
<div class="row">
<div class="col-xs-6">
<div id="firstTree" class="short-tree"></div>
</div>
<div class="col-xs-6">
<div id="secondTree" class="short-tree"></div>
</div>
</div>
</div>
onload = function() {
// create the trees
var firstTree = new wjNav.TreeView('#firstTree', {
itemsSource: [
{ header: 'root 1', items: [
{ header: 'Item 1.1' },
{ header: 'Item 1.2' },
{ header: 'Item 1.3' }]
}
],
displayMemberPath: 'header',
childItemsPath: 'items',
allowDragging: true,
dragOver: dragOverBetweenTrees
});
var secondTree = new wjNav.TreeView('#secondTree', {
itemsSource: [
{ header: 'root 2', items: [
{ header: 'Item 2.1' },
{ header: 'Item 2.2' },
{ header: 'Item 2.3' }]
}
],
displayMemberPath: 'header',
childItemsPath: 'items',
allowDragging: true,
dragOver: dragOverBetweenTrees
});
// handle drag-drop within or between trees
function dragOverBetweenTrees(s, e) {
var t1 = e.dragSource.treeView;
var t2 = e.dropTarget.treeView;
// prevent dragging within trees
if (t1 == t2) {
e.cancel = true;
}
// allow dragging between trees
if (t1 != t2) {
e.cancel = false;
}
}
}