Advice for Context Menu to dhtmlxTree nodes by Node Type

I am dynamically generating a dhtmlxTree. I have 3 different node types. Depending on which kind of node you click, I want the context menu to reflect the appropriate menu items.

Right now, I attach to the onBeforeContextMenu event and check the prefix of the node ID for the node type. Then I load the appropriate menu xml file.

dhxTree.attachEvent("onBeforeContextMenu", getMenu);

The following is an example of menu_application.xml:

[code]<?xml version="1.0" encoding="UTF-8"?>

[/code]

Here is menu_host.xml:

[code]<?xml version="1.0" encoding="UTF-8"?>

[/code]

This is the function to choose the menu_*.xml:

function getMenu(id) { dhxMenu.clearAll(); dhxMenu.loadXML("xml/menu_" + id.split(":")[0] + ".xml"); return true; }

As you can imagine, although this works, it is not very responsive. I would prefer a way to preload each menu_*.xml file once and dynamically attach them to the appropriate nodes.

Does anyone have any advice for how to do this better?

Using this extention “dhtmlxtree_attrs.js” you can add a type to the node. I.e.:

<item id="file_2" type="app"/>

Then you can use getAttribute() method to get this type.

att = dhxTree.getAttribute(id,"type");

And you can use check:

if (type == "app"){ dhxMenu.loadXML("xml/menu_application.xml" } if (type == "host"){ dhxMenu.loadXML("xml/menu_host.xml" }
and so on…

That is much more elegant. I also realized I could force a pre-load of the XML files by associating them all during the initialization.

dhxMenu = new dhtmlXMenuObject(); dhxMenu.renderAsContextMenu(); dhxMenu.loadXML("xml/menu_vip.xml"); dhxMenu.loadXML("xml/menu_host.xml"); dhxMenu.loadXML("xml/menu_application.xml");

It is much more responsive now.

Ok
You are welcome!