Data loading
There are several simple ways of loading data into DHTMLX Toolbar:
- on initialization of Toolbar
- after initialization of Toolbar
First, you need to prepare a data set that will be loaded into Toolbar.
Preparing data set
DHTMLX Toolbar expects loaded data in the JSON format.
Please note that if you specify the id
fields in the tree collection, their values should be unique. You can also omit the id
fields in the tree collection. In this case they will be generated automatically.
Here is an example of an appropriate data set:
const dataset = [
{
"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 data on initialization
You can load a predefined data set into Toolbar on the initialization stage. Use the data configuration property, as in:
const toolbar = new dhx.Toolbar("toolbar_container", {
css: "dhx_widget--bordered",
data: dataset
});
Related sample: Toolbar. Initialization with config.data
Loading data after initialization
There are two ways to load data into Sidebar after its initialization:
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(dataset);
Related sample: Toolbar. Initialization with data.parse()
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. Initialization with data.load()
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.
const 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 toolbar
const toolbar2 = new dhx.Toolbar("toolbar_container2");
// parsing the state of toolbar1 into toolbar2
toolbar2.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"
}
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"
}
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"
}
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"
}
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
}
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"
}
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: []
}
View the full list of properties of the selectButton object in the related article.
Separator template
// separator
{
id: "sepId",
type: "separator"
}
Check the full list of properties of the separator object in the related article.
Spacer template
// spacer
{
id: "spacerId",
type: "spacer"
}
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"
}
Take a look at the full list of properties of the title object in the related article.