How to do drag and drop in OrgChart with JavaScript Diagram?
This article explains how to drag and drop in OrgChart with JavaScript Diagram. In the OrgChart sample, one can create a parent/child relationship by dragging and dropping a node onto another node using the drop event in JavaScript Diagram.
Additionally, the AllowDrop constraints need to be enabled to display a
highlighter when dragging the node and hovering the mouse over another node.
Please refer to the provided code example for more information.
//Initializes diagram control
var diagram = new ej.diagrams.Diagram({
//Sets the default values of a node
getNodeDefaults: function (obj) {
obj.style = { fill: '#659be5', strokeColor: 'none', color: 'white', strokeWidth: 2 };
//AllowDrop Constraints
obj.constraints = ej.diagrams.NodeConstraints.Default | ej.diagrams.NodeConstraints.AllowDrop;
return obj;
},
drop: function (args) {
setTimeout(() => {
var diagram = document.getElementById("diagram").ej2_instances[0];
var connector = {};
if (args.element && args.target) {
if (args.element) {
//get an connector object
connector = diagram.getObject(args.element.inEdges[0]);
}
// this block executed when we drag any node in diagram and drop it onto the another node
if (connector) {
//update connector source and target id
connector.sourceID = args.target.id;
connector.targetID = args.element.id;
diagram.dataBind();
} else {
// this block executed when we drag node in palette and drop it onto the node in diagram
if(args.target.id!==args.element.id){
diagram.add({ id: 'connector' + ej.diagrams.randomId(), sourceID: args.target.id, targetID: args.element.id });
}}
//To update an layout
diagram.doLayout();
}
}, 100);
},
});
diagram.appendTo('#diagram');Refer to the working sample for additional details and implementation: Sample
Conclusion
We hope you enjoyed learning about how to do drag and drop in OrgChart with JavaScript Diagram.
You can refer to our JavaScript Diagram feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our JavaScript Diagram example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, BoldDesk Support, or feedback portal. We are always happy to assist you!