There are several simple ways of loading data into DHTMLX Tree:
First, you need to prepare a data set that will be loaded into Tree.
Preparing data setâDHTMLX Tree 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 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 contains configuration of a tree item. The structure of an item is rather flexible. It may include:
value (string) the name of an item id (string | number) the id of an item opened (boolean) optional, defines whether an item is opened by default checkbox (boolean) optional, enables/disables displaying a checkbox for a tree item checked (boolean) optional, checks/unchecks a checkbox of a tree item, works when the checkbox property is set to true items (array) an array of nested items icon (object) allows adding custom icons for a tree item Loading data on initializationâYou can load a predefined data set into Tree on the initialization stage. Use the data configuration property, as in:
const tree = new dhx.Tree("tree_container", {
data: dataset
});
Related sample: Tree. Initialization with config.data
Loading data after initializationâThere are two ways to load data into Sidebar after its initialization:
Loading from local sourceâTo load data from a local data source, use the parse method of Tree Collection. Pass a predefined data set as a parameter of this method:
const tree = new dhx.Tree("tree_container");
tree.data.parse(dataset);
Related sample: Tree. Initialization with data.parse()
External data loadingâTo load data from an external file, make use of the load
method of Tree Collection. It takes the URL of the file with data 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 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:
tree.data.load("/some/data").then(function(){
});
Saving and restoring stateâ
To save the current state of a tree, use the serialize
method of Tree Collection. It converts the data of a tree into an array of JSON objects. Each JSON object contains the configuration of a separate row.
const state = tree1.data.serialize();
Then you can parse the data stored in the saved state array to a different tree. For example:
const tree2 = new dhx.Tree("tree_container2");
tree2.data.parse(state);
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4