Skip to main content

Data loading

There are several simple ways of loading data into DHTMLX Ribbon:

  • on initialization of Ribbon
  • after initialization of Ribbon

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

Preparing data set

DHTMLX Ribbon expects loaded data in the JSON format.

info

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 data = [
{
type: "block",
items: [
{
value: "New",
icon: "dxi dxi-file-outline",
size: "small"
}
]
},
{
type: "block",
direction: "col",
items: [
{
value: "Add",
icon: "dxi dxi-plus",
size: "small"
},
{
value: "Remove",
icon: "dxi dxi-delete",
size: "small"
}
]
}
];

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

Loading data on initialization

You can load a predefined data set into Ribbon on the initialization stage. Use the data configuration property, as in:

const ribbon = new dhx.Ribbon("ribbon_container", {
css: "dhx_widget--bordered dhx_widget--bg_white",
data: data
});

Related sample: Ribbon. Initialization with config.data

Loading data after initialization

There are two ways to load data into Ribbon after its initialization:

Loading from a local source

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

ribbon.data.parse(data);

Related sample: Ribbon. Initialization with data.parse()

Loading from an external file

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

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

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

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

Saving and restoring state

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

const state = ribbon1.data.serialize();

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

// creating a new ribbon
const ribbon2 = new dhx.Ribbon("ribbon_container2");
// parsing the state of ribbon1 into ribbon2
ribbon2.data.parse(state);

JSON format templates

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

Common template

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

Related sample: Ribbon. Initialization with config.data

Block template

// block
{
type: "block",
title: "Action",
items: [
{ id: "copy", icon: "mdi mdi-content-copy", value: "Copy" },
{ id: "cut", icon: "mdi mdi-content-cut", value: "Cut" }
]
}
note

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

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
{
id: "custom_html",
type: "customHTML",
html: "<div style='height:30px; border: 2px solid'>My HTML button</div>"
}
note

You can view 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 find 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

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

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

Check 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

You will find the full list of properties of the selectButton object in the related article.

Separator template

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

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

Spacer template

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

View 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

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