Change context menu items dynamically

Hi Syncfusion.

I'm interested, how can I change menu items of context menu dynamically, according to condition?

I'm trying to modify contextMenuSettings.items array, but that do not work.


1 Reply

AR Aravind Ravi Syncfusion Team December 10, 2021 12:09 PM UTC

Hi Alex, 

By Default, In diagram ContextMenu, we can`t able to add items dynamically. Instead, we can able to show or hide the items in contextMenuOpen event. Initially you can add all the menu items in the context menu. Before the context menu gets opened, contextMenuOpen event gets fired. In that event check if any node is selected or not through the diagram selectedItems property. We can able to hide the context menu items through hiddenItems property. In that based on the node’s id you can hide the other menu items. If node-1 gets selected means then push other two items in hiddenItems collection. So that when you select the node-1 open context menu means Item-1 only gets shown, other two items gets hidden. Similary, you can hide the menu items based on your operation. Please refer to the below code snippet . please refer to the below code to show the context menu based on specific condition. 

function contextMenuOpen(args) { 

  for (let item of args.items) { 

    if ( 

      diagram.selectedItems.nodes.length && 

      diagram.selectedItems.nodes[0].id === 'sdlc' && 

      item.text !== 'Item-1' 

    ) { 

      args.hiddenItems.push(item.id); 

    } else if ( 

      diagram.selectedItems.nodes.length && 

      diagram.selectedItems.nodes[0].id === 'support' && 

      item.text !== 'Item-2' 

    ) { 

      args.hiddenItems.push(item.id); 

    } else if ( 

      diagram.selectedItems.nodes.length && 

      diagram.selectedItems.nodes[0].id === 'analysis' && 

      item.text !== 'Item-3' 

    ) { 

      args.hiddenItems.push(item.id); 

    } if ( 

      diagram.selectedItems.nodes.length && 

      diagram.selectedItems.nodes[0].id === 'analysis' 

    ) { 

      args.hiddenItems.push("item4"); 

   

 




Regards, 

Aravind Ravi 


Loader.
Up arrow icon