Articles in this section
Category / Section

How to delete a diagram elements using JavaScript Diagram context menu item?

2 mins read

The JavaScript Diagram provides built-in context menu items and allows you to define custom menu items through the contextMenuSettings property. The show property enables you to toggle the visibility of the context menu. The Diagram also offers default context menu items for frequently used commands.

Here is an example of enabling the context menu:

var diagram = new ej.diagrams.Diagram({
   contextMenuSettings: {
       //Enables the context menu
       show: true,
   },
});
diagram.appendTo('#diagram');

To add a custom item for deleting diagram elements via the context menu and display only the custom context menu, set showCustomMenuOnly to true:

   contextMenuSettings: {
       //Enables the context menu
       show: true,
       items: [{
           text: 'delete',
           id: 'delete'
       }],
       // Hides the default context menu items
       showCustomMenuOnly: true,
   }

When a user clicks on a context menu item associated with a diagram node or connector, the “contextMenuClick” event is triggered. Define a function to perform the delete operation:

var diagram = new ej.diagrams.Diagram({
   width: '100%', height: '350px',

   contextMenuSettings: {
       //Enables the context menu
       show: true,
       items: [{
           text: 'delete',
           id: 'delete'
       }],
       // Hides the default context menu items
       showCustomMenuOnly: true,
   },
   contextMenuClick: function(args) {
       //do your custom action here.
       if (args.item.id === 'delete') {
           if ((diagram.selectedItems.nodes.length + diagram.selectedItems.connectors.length) > 0) {
               diagram.remove();
           }
       }
   }

});
diagram.appendTo('#diagram');

You can find a working example of this implementation in the provided sample on StackBlitz.
Sample

Conclusion
I hope you enjoyed learning about how to delete a diagram elements using JavaScript Diagram context menu item.

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!

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