How to Drag and Drop Elements From Containers in JavaScript Diagram?
There are several interactions in the JavaScript Diagram such as Select, Drag, Drop, Resize, rotate, etc., To drop the node, connector, or container can be done by enabling the node constraint AllowDrop. The drop event is raised to notify that the element is dropped over another one and it is disabled, by default. It can be enabled by the constraint property, but the drag option is enabled, by default. The following code shows how to create a group node.
var nodes = [
{
type: "group",
name: "canvas1",
offsetX: 200,
offsetY: 250,
height: 300,
width: 300,
constraints: ej.datavisualization.Diagram.NodeConstraints.AllowDrop | ej.datavisualization.Diagram.NodeConstraints.Default,
children: [{
type: "node",
name: "node11",
fillColor: "lightGoldenRodYellow",
marginTop: 100,
marginLeft: 20,
width: 100,
height: 50
}, {
type: "node",
name: "node21",
// Sets the margin to define the space around the child node.
marginTop: 100,
marginLeft: 150,
marginRight: 20,
fillColor: "lightGoldenRodYellow",
width: 50,
height: 50,
},
{
type: "node",
name: "node31",
// Sets the margin to define the space around the child node.
fillColor: "orange",
width: 250,
height: 30,
labels: [{ "text": "Header" }],
constraints: ej.datavisualization.Diagram.NodeConstraints.Select,
horizontalAlignment: "stretch"
}],
In the above code snippet constraints property is used to enable the drop action. It is done by using the pipeline symbol for enabling the respective behavior of the node and for disabling the respective behavior &~ symbol should be used instead of pipeline symbol. By this, a node can be dropped to another node or container and the container can also drop to another container
In the following code, the container is set as the canvas. It can also be set as a stack or grid by using the type property.
//Sets the container as a canvas.
container: {
type: "canvas"
},
fillColor: "white",
borderColor: "black",
paddingBottom: 30
The following code shows, if the container is resized, it will update the size of the header or footer node.
if (args.resizeState === 'completed') {
node = diagram.findNode(args.element.name);
setTimeout(function () {
if (node.children) {
node.children[0].width = node.children[0].name == 'footer' ? node.width : node.children[0].width;
for (var i = 1; i < node.children.length; i++) {
if (node.children[i] && (node.children[i].labels.length ? node.children[i].labels[0].text == 'Header' : undefined)) {
node.children[i].width = node.width;
}
}
diagram.refresh();
}
}, 100);
Conclusion
I hope you enjoyed learning about how to drag and drop elements from one container to another in JavaScript Diagram.
You can refer to our JavaScript Diagram feature tour page to know 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, Direct-Trac, or feedback portal. We are always happy to assist you!