Skip to main content

Drag-n-Drop

DHTMLX Tree supports drag-n-drop. Users can reorder items within a tree and drag items between trees. To enable drag-n-drop, set the dragMode property:

const source = new dhx.Tree("tree-source", { dragMode: "source", dropBehaviour: "complex" });
const target = new dhx.Tree("tree-target", { dragMode: "target", dropBehaviour: "complex" });

Drag mode​

The dragMode property defines one of three drag-n-drop modes:

  • target — a tree takes items from other trees but doesn't let you drag its items out
  • source — a tree lets you drag its items out but can't take items from other trees
  • both — a tree takes items from other trees and lets you drag its items out

For example, the following tree works in the source mode:

const tree = new dhx.Tree("tree_container", { 
dragMode: "source"
});

Related sample: Tree. Drag modes

note

Drag-n-drop within a tree works only if the tree has the dragMode:"both" setting in its configuration object.

Drop behaviour​

You can specify the drag-n-drop behaviour of tree items with the dropBehaviour property in the configuration object of a tree.

The property supports three modes:

  • child — a dragged item becomes a child of the target item

Tree drag-and-drop dropping an item as a child of the target folder in DHTMLX Suite

  • sibling — a dragged item becomes a sibling of the target item

Tree drag-and-drop dropping an item as a sibling of the target item in DHTMLX Suite

  • complex — a dragged item becomes either a child or a sibling of the target item, depending on the drop position that the highlighting shows (see the images above)

Copying dragged item​

Instead of moving a dragged item to a new position in the same or a different tree, you can copy it. Set the dragCopy option to true in the configuration object of a tree:

const treeSource = new dhx.Tree("tree-source", { dragMode: "source", dragCopy: true });
const treeTarget = new dhx.Tree("tree-target", { dragMode: "target", dragCopy: true });

Related sample: Tree. Copy dragged item