Data loading
You can load data into DHTMLX Tree in two ways:
- During Tree initialization
- After Tree initialization
Both ways start from a prepared data set.
Preparing data set
DHTMLX Tree expects loaded data in the JSON format.
If you specify id fields in the tree collection, their values must be unique. You can also omit these fields. In this case Tree generates the values automatically.
Here is an example of an appropriate data set:
const dataset = [
{
"value": "Books",
"id": "Books",
"opened": true,
"items": [
{
"value": "Thrillers",
"id": "Thrillers",
"opened": true,
"items": [
{
"value": "Bestsellers",
"id": "Bestsellers",
"items": [
{
"value": "Lawrence Block",
"id": "Lawrence Block",
}
]
},
{
"value": "Robert Crais",
"id": "Robert Crais",
},
{
"value": "Ian Rankin",
"id": "Ian Rankin",
},
{
"value": "James Johns",
"id": "James Johns",
"checked": true,
},
{
"value": "Nancy Atherton",
"id": "Nancy Atherton",
"checked": true
}
]
},
{
"value": "Fiction & Fantasy",
"id": "Fiction & Fantasy",
"items": [
{
"value": "Audrey Niffenegger",
"id": "Audrey Niffenegger"
},
{
"value": "Philip Roth",
"id": "Philip Roth"
}
]
},
]
},
];
const tree = new dhx.Tree("tree", {
checkbox: true,
});
tree.data.parse(dataset);
Each object in the data set configures a tree item. An item object can include the following properties:
| value | (string) the name of an item |
| id | (string | number) the id of an item |
| opened | (boolean) optional, defines whether an item is initially open |
| checkbox | (boolean) optional, shows or hides the checkbox of a tree item |
| checked | (boolean) optional, checks or unchecks the checkbox of a tree item; works when the checkbox property is true |
| items | (array) an array of nested items |
| icon | (object) adds custom icons to a tree item |
Loading data on initialization
You can load a predefined data set into Tree during initialization. Use the data configuration property:
const tree = new dhx.Tree("tree_container", {
data: dataset
});
Related sample: Tree. Initialization with config.data
Loading data after initialization
You can load data into Tree after initialization in two ways:
Loading from a local source
The parse() method of TreeCollection loads data into a tree from an array. Pass a predefined data set to this method:
const tree = new dhx.Tree("tree_container");
tree.data.parse(dataset);
Related sample: Tree. Initialization with data.parse()
Loading from an external file
The load() method of TreeCollection loads tree data from an external file. The method takes the URL of the data file as a parameter:
const tree = new dhx.Tree("tree_container");
tree.data.load("../common/dataset.json");
Related sample: Tree. Initialization with data.load()
The component makes an AJAX call and expects the remote URL to return valid JSON data.
Tree loads data asynchronously, so place the code that depends on the loaded data into the then() callback:
tree.data.load("/some/data").then(function(){
// some logic here
});
Saving and restoring state
The serialize() method of TreeCollection saves the current state of a tree. The method converts tree data into an array of JSON objects. Each JSON object contains the configuration of a tree item.
const state = tree1.data.serialize();
Then you can parse the data stored in the saved state array to a different tree. For example:
// creating a new Tree
const tree2 = new dhx.Tree("tree_container2");
// parsing the state of tree1 into tree2
tree2.data.parse(state);