This document describes dhtmlxMenu 2.0 component, defines its methods and global parameters. This documentation is created to give users full understanding of every feature of this component and also to enable users to implement this component quickly and easily.
Target readers are users (developers).
The following terms are connected with dhtmlxMenu 2.0 component:
The first steps that need to be taken for dhtmlxMenu's initialization are the following:
<head>From this point further steps the user should take are different for creating a usual menu and a contextual menu.
<script src="[full path to this file]/dhtmlxcommon.js"></script>
<script src="[full path to this file]/dhtmlxmenu.js"></script>
<link rel="stylesheet" type="text/css" href="[full path to this file]/dhtmlxmenu_dhx_blue.css">
</head>
<div id="menuObj"></div>The next step is to create a new dhtmlXMenuObject and place it after the <div> element (object) that we've just created:
<script>baseId defines an HTML object on page to which the menu is attached (the main menu container in the code mentioned above).
var menu = new dhtmlXMenuObject(baseId, skin);
</script>
The second argument is optional, and it indicates the name of the skin chosen for the Menu. If this argument is not indicated, the component will be created with the default skin (dhx_blue).
<script>
var menu = new dhtmlXMenuObject("menuObj");
</script>
It should be noted that with the way of menu creating described above, the system will first clear the menu container (<div>), and then create the menu.
<div id="contextArea"></div>For a contextual menu the user should:
<script>It should be noted that with the way of menu creating described above, the system will first clear the menu container (<div>), and then create the contextual menu. To prevent container data from clearing, the user should use the method described in Contextual menu zones section.
menu = new dhtmlXMenuObject("contextArea");
menu.renderAsContextMenu();
</script>
3.1.3 Contextual Menu Zones
A contextual zone can be easily removed with the help of the following method:menu = new dhtmlXMenuObject();
menu.renderAsContextMenu();
menu.addContextZone(contextZoneId);
</script>
<script>
menu.removeContextZone(contextZoneId);
</script>
There is also the possibility to check whether an object is a contextual menu zone:
<script>
var isZone = menu.isContextZone(objectId); // returns true|false
</script>
3.1.4 Initialization Recommendation
The recommended way of menu initialization is the following:
<html>
<head>
<script>
var menu;
function doOnLoad() {
menu = new dhtmlXMenuObject("parentId");
}
</script>
...
</head>
<body onload="doOnLoad();">
<div id="parentId"></div>
...
</body>
</html>
<script>
menu.setImagePath("[full path to this directory]/codebase/imgs/");
menu.setIconsPath("[full path to icons image files directory]");
</script>
<head>
<link rel="stylesheet" type="text/css" href="[full path to this file]/dhtmlxmenu_clear_blue.css">
</head>
<script>Note: there's no need to write the name of the skin as the second argument for the default menu skin (dhx_blue).
var menu = new dhtmlXMenuObject("menuObj", "clear_blue");
//or
menu = new dhtmlXMenuObject("contextArea", "clear_blue");
menu.renderAsContextMenu();
</script>

dhx_black


The next step of initialization of dhtmlxMenu component is Data Loading. The user can choose one of 5 data loading possibilities:
3.4.1 Data Loading from XML File
loadXML() method loads menu data from an XML file. When the data is loaded into the object, a user-defined handler is called (onLoadFunction), if it was indicated by the user. All the data is loaded at once:
<script>
menu.loadXML("[path to this file]/file.xml", onLoadFunction);
onLoadFunction = function(){
// will be called if specified
}
</script>
The first parameter of loadXML() method is the path to the XML file, while the second parameter is an optional user-defined handler.
See here for XML Format Template.
3.4.2 Data Loading from XML String
loadXMLString() method loads menu data from the XML string. When the data is loaded into the object, a user-defined handler is called (onLoadFunction).<script>
menu.loadXMLString("<menu><item....>", onLoadFunction);
onLoadFunction = function(){
// will be called if specified
}
</script>
The first parameter for loadXMLString() method is the XML string from our html file, while the second parameter is an optional user-defined handler.
See here for XML Format Template.
3.4.3 Data Loading from HTML Object
loadFromHTML() method loads content from HTML object to the menu. First, the user should create this HTML object. For example:
<body>
<div id="menuData" style="display: none;">
<div id="m1" text="File"> // the first top menu item
<div id="m11" text="New" <hotkey>Ctrl+N</hotkey></div>// the first child item
<div id="ms1" type="separator"></div> // a separator
<div id="m12" text="Open"><hotkey>Ctrl+O</hotkey></div> // the second child item
</div>
</div>
</body>
Then loadFromHTML() should be applied with the following parameters:
<script>
menu.loadFromHTML(objectId, clear, function(){});
</script>
So, in our example this code will look like this (without any user-defined handler):
<script>
menu.loadFromHTML("menuData", true);
</script>
3.4.4 Data Loading from Script
Loading data from script means that the user should write a special method for adding every menu item. In our example to add a new sibling item, a child item, a new separator and set a hotkey, we should write the following:
<script>
menu.addNewSibling(null, "file", "File", false); // adding the first item to the menu, "nextToId" param is null
menu.addNewChild("file", 0, "file_new", "New", false); // adding a new child item
menu.setHotKey("file_new", "Ctrl+N"); // setting a hotkey to a button
menu.addNewSeparator("file_new"); // adding a separator
</script>
Refer to the section Items Settings Manipulations to learn about the methods used in the above mentioned snippet.
3.4.5 Dynamical Loading
Dynamical loading means loading data on request. The user can split data into parts by levels and decrease loading time this way.<script>
menu.enableDynamicLoading(url, icon);
</script>
So, in our case we should write this line of code (without an icon):
<script>
menu.enableDynamicLoading("[script url]");
</script>
3.4.6 onLoadFunction()
onLoadFunction() is a user-defined handler that is called after the data was loaded into the object:
menu.loadXML("file.xml", function(){
alert(the data is loaded); // will be invoked after XML file was loaded (after onXLE, if specified)
<script>Note: setting of the web mode from the script is not required because it's already set by default.
menu.setOpenMode("win");
</script>
3.5.2 Setting Timeout for Web Mode
When the Menu is set to the web mode, there is an opportunity to set the period of time, during which the menu will be held expanded, even if the user moves the mouse outside the menu area. By default this time is set to 400 msec.<script>setVisibleArea() method sets the rectangle area in which sub-level polygon items will be able to appear. If this area is not set, sub-level polygon items can occupy any available visible space.
menu.setWebModeTimeout(time);
</script>
<script>
menu.setVisibleArea(x1,x2,y1,y2);
</script>
When using dhtmlxGrid with Contextual menu you often meet some inconveniences. For example you need to read contextual menu of the record placed at the bottom of the grid, but the appearing of browser's scrollbars will disturb the impementation of this task. To avoid scrolling appearence set visible area and define menu position. (set grid's coordinates to acheive this).
There is the possibility to hide all open polygons in a menu from script with the help of hide() method:<script>By default there is no limits on the number of visible menu items in any sub-level polygon. However, the user can limit the number of visible items in a sub-level polygon using setOverflowHeight() method.
menu.hide();
</script>
<script>
menu.loadXML("dhtmlxMenu/codebase/dhtmlxmenu.xml", function () {
menu.setOverflowHeight(4);
});
</script>
The first menu item in the usual menu can be created with the help of addNewSibling() method. The parameters here are as follows:
<script>
...
menu.addNewSibling(null, ...);
</script>
The first menu item in the contextual menu can be created with the help of addNewChild() method. The parameters here are as follows:
<script>
...
menu.renderAsContextMenu();menu.addNewChild(menu.topId, ...);
</script>
<script>
menu.addNewSibling(nextToId, ItemId, itemText, disabled, img,imgDis);
</script>
There is the possibility to create a child item for any item in the menu. The child item will be located in a sub-level polygon and won't be visible until its parent item is hovered over.
addNewChild() method should be called and the following parameters are to be passed:
<script>
menu.addNewChild(parentId, position, itemId, itemText, disabled, img, imgDis);
</script>
Separators can be created with the help of addNewSeparator() method. A separator provides kind of a logical grouping of menu items.
addNewSeparator() method has the following parameters:
<script>
menu.addNewSeparator(nextToId, itemId);
</script>
<script>
menu.removeItem(Id);
</script>
<script>
var parentId = menu.getParentId(id);
</script>
<script>Also the user has the possibility to check whether an item is enabled. This can be done by calling the following method:
menu.setItemEnabled(id);
menu.setItemDisabled(id);
</script>
<script>The user should pass id of the item that will be checked. The method returns true if the item is enabled.
var isEnabled = menu.isItemEnabled(id); // returns true|false
</script>
<script>The user has the possibility to check whether an item is hidden. The method returns true if the item is hidden:
menu.showItem(id);
menu.hideItem(id);
</script>
<script>
var isHidden = menu.isItemHidden(id);
</script>
<script>The user can get item's text using getItemText() method. The method returns the current text of the item:
menu.setItemText(id, text);
</script>
<script>
var text = menu.getItemText(id); // returns item's text
</script>
<script>There is the possibility to get items' position. The method returns item's position number:
menu.setItemPosition(id,pos);
</script>
<script>
var pos = menu.getItemPosition(id); // returns item's position
</script>
The user has the possibility to set, store, and pass values set for a certain menu item. One menu item can have several different user data stored in it.
<script>An easy way to get user data is to use getUserData() method passing the item's id and data name:
menu.setUserData(id, name, value);
</script>
<script>
var value = menu.getUserData(id, name);
</script>
<script>getItemImage() method gets current item images for enabled and disabled states and returns array(img, imgDis) that will contain URLs to images:
menu.setItemImage(id,img,imgDis);
</script>
<script>Item's image can be easily removed/cleared by means of clearItemImage() method to which the user should pass item's id:
var imgs = menu.getItemImage(id); // returns array
</script>
</script>
menu.clearItemImage(id);
</script>
The user can specify the supplementary information regarding the item - tooltip. setTooltip() method takes the following parameters:
<script>The following method can return current item's tooltip text:
menu.setTooltip(id,tip);
</script>
<script>
var tip = menu.getTooltip(id);
</script>
<script>getHotKey() method returns current item's hotkey text:
menu.setHotKey(id, hkey);
</script>
<script>
var hkey = menu.getHotKey(id); // returns hotkey text
</script>
<script>New state (checked/unchecked) of the checkbox can be set by the following method:
menu.addCheckbox(mode, nextToId, pos, itemId, itemText, state, disabled);
</script>
<script>A simple way to get the current state of a checkbox is presented below:
menu.setCheckboxState(id,state);
</script>
<script>
var state = menu.getCheckboxState(id); // returns true|false
</script>
<script>
menu.addRadioButton(mode, nextToId, pos, itemId, itemText, group, state, disabled);
</script>
Any unchecked radio button can be made checked while currently checked radio button automatically changes its state to unchecked:
<script>An easy way to get id of the current checked radio button in a radio group is presented below:
menu.setRadioChecked(id,state);
</script>
<script>
var idChecked = menu.getRadioChecked(group);
</script>
<script>
menu.forEachItem(function(itemId){});
</script>
To do this, he can use attachEvent() method with the following parameters:
Note: the names of the events are case-insensitive.<script>
attachEvent(evName, evHandler);
</script>
Now let's attach an event handler for onClick event:<script>
menu.attachEvent("onClick", function(id, zoneId, casState){});
menu.attachEvent("onTouch", function(id){});
</script>
<script>
menu.attachEvent("onClick", function(id, zoneId, casState){
alert("element "+id+" was clicked");
// ctrl
if (casState["ctrl"] == true) {
// ctrl key was pressed with click
} else {
// ctrl key was not pressed with click
}
// alt
if (casState["alt"] == true) {
// alt key was pressed with click
} else {
// alt key was not pressed with click
}
// shift
if (casState["shift"] == true) {
// shift key was pressed with click
} else {
// shift key was not pressed with click
}
});
</script>
<script>When the user attaches a user-defined handler to this event, the state of the item can no longer be changed automatically. The user can control this by returning true|false from a user-defined handler. That's why, return true should be added after a user-defined handler in order to let the script know that the state of the item should be changed.
menu.attachEvent("onCheckboxClick", function(id, state, zoneId, casState){
... // user-defined handler
// ctrl
if (casState["ctrl"] == true) {
// ctrl key was pressed with click
} else {
// ctrl key was not pressed with click
}
// alt
if (casState["alt"] == true) {
// alt key was pressed with click
} else {
// alt key was not pressed with click
}
// shift
if (casState["shift"] == true) {
// shift key was pressed with click
} else {
// shift key was not pressed with click
}
return true;
});
</script>
<script>
menu.attachEvent("onRadioClick", function(group, idChecked, idClicked, zoneId, casState){
... // user-defined handler
// ctrl
if (casState["ctrl"] == true) {
// ctrl key was pressed with click
} else {
// ctrl key was not pressed with click
}
// alt
if (casState["alt"] == true) {
// alt key was pressed with click
} else {
// alt key was not pressed with click
}
// shift
if (casState["shift"] == true) {
// shift key was pressed with click
} else {
// shift key was not pressed with click
}
return true;
});
</script>
When the user attaches a user-defined handler to this event, the state of the item can no longer be changed automatically. The user can control this by returning true|false from a user-defined handler. That's why return true should be added after a user-defined handler in order to let the script know that the state of the item should be changed.
<script>In case of dynamic loading, onXLS event will be called every time before a system loads a certain part of data into the menu.
menu.attachEvent("onXLS", function() {});
</script>
<script>In case of dynamical loading, onXLE event will be called every time after a system loads a certain part of data into the menu.
menu.attachEvent("onXLE", function(){});
</script>
<script>When the user attaches a user-defined handler to this event, the state of the item can no longer be changed automatically. The user can control this by returning true|false from a user-defined handler. That's why return true should be added after user-defined handler, in order to let the script know that the state of the item should be changed.
menu.attachEvent("onBeforeContextMenu", function(zoneId){
... // user-defined handler
return true;
});
</script>
<script>
menu.attachEvent("onAfterContextMenu", function(zoneId){});
</script>
[BASIC]
<?xml version="1.0"?>
<menu> a top xml tag, mandatory
<item id="file" text="File"> a top-level item, each item must have a unique ID
<item id="new" text="New"/> a sub-level item
<item id="ms1" type="separator"/> a sub-level separator item
...
<item id="export" text="Export">
<item id="export_pdf" text="PDF"/> a sub-level item
...
</item>
...
</item>
</menu>
[ADVANCED]
<?xml version="1.0"?>
<menu>
<item id="file" text="File">
<item id="new" text="New" img="new.gif" imgdis="new_dis.gif"/> a sub-level item with images for enabled/disabled states
<item id="ms1" type="separator"/>
...
<item id="export" text="Export">
<item id="export_pdf" text="PDF" enabled="false"/> a sub-level item disabled by default
...
</item>
...
</item>
</menu>
[FULL]
<?xml version="1.0"?>
<menu>
<item id="file" text="File">
<item id="new" text="New" img="new.gif" imgdis="new_dis.gif">
<hotkey>Ctrl+N</hotkey> a hotkey (just a text) to a menu item
</item>
<item id="ms1" type="separator"/>
...
<item id="export" text="Export">
<item id="export_pdf" text="PDF" enabled="false"/>
...
</item>
...
<item id="eol" text="End Of Line">
<item id="eol_u" text="Unix (\n)" type="radio" group="eol" checked="true"/> a radiobutton, group "eol", checked by default
<item id="eol_w" text="DOS/Windows (\r\n)" type="radio" group="eol"/> a radiobutton, group "eol"
<item id="eol_m" text="MacOS (\r)" type="radio" group="eol"/> a radiobutton, group "eol"
<item id="eol_m" text="MacOS (\r)" type="radio" group="eol"/> a radiobutton, group "eol", disabled
we got a radiogroup "eol" with 4 buttons, 1 disabled
</item>
<item id="ms2" type="separator"/>
<item id="ignorecase" text="Ignore Case" type="checkbox" checked="true" enabled="false"/> a disabled checked checkbox
<item id="checkspelling" text="Check Spelling" type="checkbox"/> an unchecked checkbox
</item>
</menu>