Data Loading

There are several simple ways of loading options into dhtmlxMenu:

  • load data from an external file
  • load data from a local data source

First, you need to prepare a data set that will be loaded into Menu.

Preparing data set

dhtmlxMenu expects loaded data in the JSON format. Here is an example of an appropriate data set:

var data = [
    { value: "File", 
        items: [
            {
                value: "New File",
                icon: "dxi dxi-file-outline",
            },                      
            {
                value: "Remove File",
                icon: "dxi dxi-delete",
            }        
        ]
    },
    {
        type: "separator"
    },
    { value: "Edit",
        items: [
            {
                value: "Undo",
                icon: "dxi dxi-undo",
            },
            {
                value:  "Redo",
                icon: "dxi dxi-redo",
            }
         ]
    },
    {
        type: "spacer"
    }
]

A data set consists of objects with configurations of menu controls. Templates for Menu controls in JSON format are given below.

Loading from a local source

You can load data to a menu from an array with the parse() method of TreeCollection. Pass a predefined data set as a parameter of this method:

menu.data.parse(data);

Loading from an external file

The load method of Tree Collection loads menu data from an external JSON file. All the data are loaded at once. The parameter of the method is the path to the JSON file.

menu.data.load("[path to this file]/file.json");

The component will make an AJAX call and expect the remote URL to provide valid JSON data.

Data loading is asynchronous, so you need to wrap any after-loading code into a promise:

menu.data.load("/some/data").then(function(){
   // some logic here
});

JSON format templates

This section will give you the idea of JSON format templates for separate Menu controls.

MenuItem

The menuItem object has the following properties:

  • type - (string) required, the type of a control, set it to "menuItem"
  • id - (string) the id of a control, auto-generated if not set
  • icon - (string) optional, the name of an icon from the used icon font
  • items - (array) optional, an array of children controls (note that all the children should have the type menuItem)
  • value - (string) optional, the value of a menu item
  • hotkey - (string) optional, the name of a keyboard shortcut for a menu item
// menuItem
{
    id:         "print",             
    type:       "menuItem",          
    value:      "Print",             
    icon:       "dxi-printer",      
    hotkey:     "Alt+P",                
    items:[                     
        {
            type:   "menuItem",
            value:  "Print"
        },
        {
            type:   "menuItem",
            value:  "Preview and print",
            icon:   "dxi-magnify"
        }
    ]
}

Separator

You can add separators that will draw horizontal lines between menu options or vertical lines between menu items. The separator object has the following properties:

  • type - (string) required, the type of a control, set it to "separator"
  • id - (string) the id of a control, auto-generated if not set
{
    id:   "s_id",        
    type: "separator"   
}

Spacer

The spacer object has the following properties:

  • type - (string) required, the type of a control, set it to "spacer"
  • id - (string) the id of a control, auto-generated if not set
// spacer
{
    id:     "spacerId",     
    type:   "spacer"        
}

Saving and restoring state

To save the current state of a toolbar, use the serialize() method of Tree Collection. It converts the data of a menu into an array of JSON objects. Each JSON object contains the configuration of a separate Menu item.

var state = menu1.data.serialize();

Then you can parse the data stored in the saved state array to a different menu. For example:

// creating a new menu
var menu2 = new dhx.Menu(document.body);
// parsing the state of menu1 into menu2
menu2.data.parse(state);
Back to top