Work with Menu

Hiding/showing menu options

You can hide and show menu items (one or several at once) with the help of the hide() / show() methods:

menu.hide(id);
menu.show(id);

Both methods take one parameter - the ID of a menu item or an array with IDs of menu items.

Disabling/enabling menu options

You can disable one menu option or several options at a time with the disable() method:

menu.disable("1");
menu.enable(["2","3"]);

To enable one menu option or several options at a time, use the enable() method:

menu.enable("1");
menu.enable(["1","3"]);

Both methods take one parameter: the ID of a menu item or an array of IDs.

Using Tree collection API

You can manipulate the controls of Menu with the help of the Tree collection API.

Adding menu items

You can add menu items with the add() method of tree collection:

menu.data.add({
    type:"separator"
});
 
menu.data.add({
    value:"Open", items:[
        { value:"File", hotkey:"Ctrl+F" },
        { value:"Folder" }
    ]
});

Rearranging menu items

You can move menu items to different positions with the move() method. For example, this is how you can move an item with ID "2" to the left edge of the menu:

menu.data.move("2",0);

The ID should always be a string, even if in menu item configuration you set it as a number.

To get the current position, use the getIndex() method of TreeCollection:

menu.data.getIndex("id");

Indexes are counted from 0.

Iterating menu items

You can work with all (or some) menu items with the help of the map() method of TreeCollection:

// remove all icons from the menu
menu.data.map(function(item){
    item.icon = "";
});
menu.paint();

Setting item text

You can set text labels for any menu item. Access the needed item with the getItem() method of the TreeCollection:

menu.data.getItem("id").value = "Open";
menu.paint();

Setting item hotkey

You can add a shortcut to a menu option by accessing it with the help of the getItem() method. After you add a hotkey, a label with the keys will be added to the option.

menu.data.getItem("id").hotkey = "Ctrl+N";
menu.paint();

Removing menu items

You can remove any item from a menu with the remove() method. The item will be removed with all its sub-items.

menu.data.remove("id");

To remove all items from Menu, call the removeAll() method. Afterwards, you can load other options.

menu.data.removeAll();
menu.data.parse(new_options);

Check the full list of Tree collection API.

Back to top