Articles in this section
Category / Section

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

2 mins read

The Angular 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:

this.contextMenuSettings = {
   //Enables the context menu
   show: true,
} as ContextMenuSettingsModel;

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

this.contextMenuSettings = {
   //Enables the context menu
   show: true,
   items: [{
       text: 'delete',
       id: 'delete',
   }],
   // Hides the default context menu items
   showCustomMenuOnly: true,
} as ContextMenuSettingsModel;

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:


<ejs-diagram #diagram id="diagram" width="100%" height="580px" [contextMenuSettings]="contextMenuSettings"  (contextMenuClick)="contextMenuClick($event)"></ejs-diagram>

public contextMenuClick(args: MenuEventArgs): void {
       if (args.item.id === 'delete') {
           if (((this.diagram as any).selectedItems.nodes.length + (this.diagram as any).selectedItems.connectors.length) > 0) {
               (this.diagram as any).remove();
           }
       }
   }

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

Sample

Conclusion:

Hope you enjoyed learning about how to delete a diagram elements using context menu item.

You can refer to our Angular Diagram feature tour page to learn about its other groundbreaking feature representations. You can explore our Angular Diagram documentation to understand how to present and manipulate data.

For current customers, you can check out our Angular 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 Angular Diagram and other Angular components.

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