Work with Tree
Work with checkboxes
You can enable checkboxes for tree items with the checkbox configuration option. Then you can manage the checkboxes with the Tree API.
Check and uncheck checkboxes
The checkItem() method checks the checkbox of a tree item. The method takes the id of the item as a parameter:
tree.checkItem("history");
The uncheckItem() method unchecks a tree item and its sub-items by the item id:
tree.uncheckItem("history");
Related sample: Tree. Check / uncheck Item
Get checked items
The getChecked() method returns an array with the ids of all checked items in a tree, for example:
tree.getChecked(); // -> ["jmf", "rle", "sk", "km", "af", "jw"]
Related sample: Tree. Get checked items
Expand and collapse items
Expand and collapse an item
The expand() method expands a folder by its id:
tree.expand("history");
The collapse() method collapses a tree item:
tree.collapse("history");
Related sample: Tree. Expand item
The toggle() method expands a collapsed folder and collapses an expanded one:
tree.toggle("history");
Related sample: Tree. Toggle item
Expand and collapse all items
The expandAll() and collapseAll() methods expand and collapse all Tree items:
// expand all tree items
tree.expandAll();
// collapse all tree items
tree.collapseAll();
Related sample: Tree. Expand all items
Edit an item
The editItem() method starts editing a tree item. Pass the id of the item to the method:
tree.editItem(id);
The method can also take a second parameter that configures the editor. This parameter is an object that can include two properties:
| mode | (string) the type of an editor:
|
| options | (array) optional, an array of additional options This property works only with mode: "select" |
For instance:
tree.events.on("itemDblClick", function (id) {
tree.editItem(id,{ mode: "select", options: [1, 2, 3, 4, 5] });
});
Set and get Tree state
The getState() method returns an object with the state of a tree. The setState() method takes this object as a parameter and restores the tree state:
// getting the state of a tree
const treeState = tree.getState();
// restoring the state of a tree
tree.setState(treeState);
Related sample: Tree. Getting Tree state
Related sample: Tree. Setting Tree state
The treeState object contains key:value pairs, where key is the id of a tree item and value is its state. The state object of a tree item includes two properties:
| selected | (number) the status of a checkbox of an item:
|
| open | (boolean) defines whether a tree item is open (for folders with items) |
Here is an example of a treeState object:
{
"books": {
"open": true,
"selected": 2
},
"mystery": {
"open": true,
"selected": 2
},
"bsthrillers": {
"selected": 1
},
"rc": {
"selected": 0
},
"ir": {
"selected": 1
},
"history": {
"selected": 0
},
"jmf": {
"selected": 0
},
"jd": {
"selected": 0
}
}
Use the TreeCollection API
You can manipulate Tree items with the TreeCollection API.
Add items to Tree
Use the add() method of TreeCollection to add items to an initialized tree. The method takes three parameters:
| config | (object) the configuration object of the added item |
| index | (number) optional, the position to add an item at |
| parent | (string) the ID of the future parent item |
tree.data.add({"value": "Life"}, -1, "Magazines");
Related sample: Tree. Adding and removing items
Update Tree items
The update() method of TreeCollection changes the configuration options of an item. The method takes two parameters:
| id | the id of the item |
| config | an object with the new configuration of the item |
For example, you can change the value of an item:
tree.data.update("item_id", {value: "New value"});
You can also hide the checkbox of a tree item:
tree.data.update("Books", {checkbox:false});
Get the parent of an item
The getItem() method of TreeCollection returns an item object. Its parent property (string) contains the id of the parent item:
tree.data.getItem("Thrillers").parent
// "Books"
tree.data.getItem("Books").parent
// "_ROOT_u1574768464563"
Related sample: Tree. Data update
Remove items from Tree
The remove() method of TreeCollection removes an item. Pass the id of the item to the method:
tree.data.remove("id");
Related sample: Tree. Adding and removing items
Check the full list of TreeCollection API methods.