Data Loading

There are several simple ways of loading data into dhtmlxSidebar:

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

Preparing data set

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

var data = [
    {
        "id": "dashboard",
        "value": "Dashboard",
        "icon": "mdi mdi-view-dashboard"
    },
    {
        "id": "statistics",
        "value": "Statistics",
        "icon": "mdi mdi-chart-line"
    },
    {
        "id": "reports",
        "value": "Reports",
        "icon": "mdi mdi-file-chart"
    }
];

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

Loading from a local source

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

sidebar.data.parse(data);

Related sample:  Sidebar. Basic Initialization

Loading from an external file

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

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

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

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

Saving and restoring state

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

var state = sidebar1.data.serialize();

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

// creating a new sidebar
var sidebar2 = new dhx.Sidebar(document.body);
// parsing the state of sidebar1 into sidebar2
sidebar2.data.parse(state);

JSON format templates

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

Common template

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

Custom HTML

The customHTML object has the following properties:

// custom HTML button
{
    id: "custom_html",
    type: "customHTML",
    html:"<div style='height:30px; border: 2px solid'>My HTML button</div>"
}

MenuItem

The menuItem object has the following properties:

// menuItem
{
    type:"menuItem", 
    value:"Some",
    icon:"dxi dxi-menu-right"
}

NavItem

The navItem object has the following properties:

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

Separator

The separator object has the following properties:

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

Spacer

The spacer object has the following properties:

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

Title

The title object has the following properties:

// title
{
    id:         "collection",       
    type:       "title",                
    value:      "Music",                
    tooltip:    "Current collection"    
}
Back to top