Articles in this section
Category / Section

How to Perform Drag and Drop Operation in OrgChart React Diagram?

4 mins read

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 React 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.

<DiagramComponent
     id="diagram"
     ref={(diagram) => (diagramInstance = diagram)}
     width={'100%'}
     height={'700px'}
     // Sets the default values of a node
     getNodeDefaults={(node) => {
      node.constraints =
         NodeConstraints.Default | NodeConstraints.AllowDrop;
       return node;
     }} 
     dataSourceSettings={{
       // Sets the fields to bind
       id: 'Name',
       parentId: 'Category',
       dataSource: new DataManager(hierarchicalTree),
       doBinding: (nodeModel, data, diagram) => {
         nodeModel.shape = {
           type: 'Text',
           content: data.Name,
         };
       },
     }}
     layout={{
       type: 'HierarchicalTree',
       verticalSpacing: 30,
       horizontalSpacing: 40,
       enableAnimation: true,
     }}
     drop={(args) => {
       setTimeout(() => {
         var connector = {};
         if (args.element && args.target) {
           if (args.element) {
             // Get a connector object
             connector = diagramInstance.getObject(
               args.element.inEdges[0]
             );
           }
           // This block executes when we drag any node in the diagram and drop it onto another node
           if (connector) {
             // Update connector source and target ID
             connector.sourceID = args.target.id;
             connector.targetID = args.element.id;
             diagramInstance.dataBind();
           } else {
             // This block executes when we drag a node from the palette and drop it onto a node in the diagram
             if (args.target.id !== args.element.id) {
               diagramInstance.add({
                 id: 'connector' + randomId(),
                 sourceID: args.target.id,
                 targetID: args.element.id,
               });
             }
           }

           // To update the layout
           diagramInstance.doLayout();
         }
       }, 100);
       diagramInstance.fitToPage();
     }}
   >
 <Inject services={[UndoRedo, DataBinding, HierarchicalTree]} />
</DiagramComponent>

The following sample illustrate the working of the how drag and drop in OrgChart works

Sample

Conclusion
I hope you enjoyed about how to perform drag and drop operation in OrgChart React Diagram.
You can refer to our React Diagram feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our React 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!

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