Articles in this section

How to Drag and Drop Elements From Containers in JavaScript Diagram?

This article explains how to drag and drop elements from containers in a 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, you can enable the node constraint `AllowDrop`. The drop event is raised to notify that an element is dropped over another one, and it is disabled by default. It can be enabled through 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's 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, the &~ symbol should be used instead of the pipeline symbol. By doing this, a node can be dropped onto another node or container, and the container can also be dropped onto another container.

In the following code sets the container as the canvas. It can also be set as a stack or grid by using the type of property.

//Sets the container as a canvas.
                container: {
                    type: "canvas"
                },
                fillColor: "white",
                borderColor: "black",
 
                paddingBottom: 30

 

The following code shows that 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);

 

Sample: https://jsplayground.syncfusion.com/uib4gnlu?_ga=2.32441786.548666981.1594612518-984831319.1592397309

 

Conclusion

We 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 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 forumsBoldDesk Support, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied