Skip to main content

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);

Related sample: Menu. Basic Initialization

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");

Related sample: Menu. Basic Initialization

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 template#

// 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"        }    ]}
note

You can find the full list of properties of the menuItem object here.

NavItem template#

// navItem{    type:  "navItem",     value: "Some",    icon:  "dxi-check",    count: 10}
note

You will find the full list of properties of the navItem object here.

Custom HTML template#

// customHTML{    id: "custom",    parent: "edit",    type: "customHTML",    html: "<img src='../logo.svg' alt=''/>",    css: "custom-image"}
note

You will find the full list of properties of the customHTML object here.

Separator template#

You can add separators that will draw horizontal lines between menu options or vertical lines between menu items.

// separator{    id:   "s_id",            type: "separator"   }
note

You will find the full list of properties of the separator object here.

Spacer template#

// spacer{    id:     "spacerId",         type:   "spacer"        }
note

You will find the full list of properties of the spacer object here.

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 menuvar menu2 = new dhx.Menu(document.body);// parsing the state of menu1 into menu2menu2.data.parse(state);