Skip to main content

Data Loading

There are several simple ways of loading data into dhtmlxToolbar:

  • 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 Toolbar.

Preparing data set#

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

var data = [    {        "id": "other",        "type": "button",        "view": "link",        "circle": true,        "color": "secondary",        "icon": "mdi mdi-menu"    },    {        "id": "add",        "icon": "mdi mdi-plus",        "value": "Add"    },    {        "type": "separator"    },    {        "id": "edit",        "value": "Edit"    },    {        "id": "search",        "type": "input",        "placeholder": "Search",        "icon": "mdi mdi-magnify"    }];

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

Loading from a local source#

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

toolbar.data.parse(data);

Related sample: Toolbar. Basic Initialization

Loading from an external file#

The load method loads the toolbar 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.

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

Related sample: Toolbar. 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:

toolbar.data.load("/some/data").then(function(){   // some logic 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 toolbar into an array of JSON objects. Each JSON object contains the configuration of a separate Toolbar control.

var state = toolbar1.data.serialize();

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

// creating a new toolbarvar toolbar2 = new dhx.Toolbar(document.body);// parsing the state of toolbar1 into toolbar2toolbar2.data.parse(state);

JSON format templates#

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

Common template#

// common[    {id: "item_a", type: "button", ...},    {id: "item_b", type: "input", ...},    {id: "item_c", type: "text", ...}]

Button template#

// button{    id: "add",                  type: "button",             icon: "dxi-plus",               value: "Add",                   count: 11,                                      tooltip: "Add a new user"   }
note

View the full list of properties of the button object in the related article.

Custom HTML template#

// custom HTML button{    type: "customHTML",    html: "<img src='../logo.svg' alt=''/>",    css: "logo-container"}
note

Check the full list of properties of the customHTML object in the related article.

ImageButton template#

// imageButton{    id:       "user",                   type:     "imageButton",            src:      "../img/avatar.png"               }
note

You can view the full list of properties of the imageButton object in the related article.

Input template#

// input{    id:          "lookup",                        type:        "input",                         value:       "",                             placeholder: "Type in to search...",          width:       100,                             label:       "Search"}
note

You can check the full list of properties of the input object in the related article.

MenuItem template#

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

You can find the full list of properties of the menuItem object in the related article.

NavItem template#

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

You can take a look at the full list of properties of the navItem object in the related article.

SelectButton template#

// selectButton{    id:"select",    type:"selectButton",    icon:"dxi-some",    items:[]}
note

View the full list of properties of the selectButton object in the related article.

Separator template#

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

Check the full list of properties of the separator object in the related article.

Spacer template#

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

You can find the full list of properties of the spacer object in the related article.

Title template#

// title{    id: "collection",           type: "title",                  value: "Music",                 tooltip: "Current collection"   }
note

Take a look at the full list of properties of the title object in the related article.