First, when you are adapting a directory tree, use the checkbox form, which is configured as follows:
Look at the effect:
Each node at this point is in an optional state (but not selected), but we may encounter two other scenarios in the actual project:
1, according to the JSON returned in the background, select the specified node (at this time all the nodes are still in the editable state);
The code is as follows:
var treeobj = $.fn.ztree.getztreeobj ("CategoryTree");
for (var j = 0; J < Znodes.length; J + +) {
Treeobj.checknode (Treeobj.getnodebyparam ("id", znodes[j].id), true);
}
Znodes is the JSON data retrieved in the background, which includes ID, ParentID, name, and deep, in the following form:
2, according to JSON, select the specified node, all nodes are not operational state.
The code is as follows:
/** Get Tree Object * /
var treeobj = $.fn.ztree.getztreeobj ("CategoryTree");
/** Get all nodes * /
var nodes = Treeobj.transformtoarray (Treeobj.getnodes ());
For (var i = 0; i < nodes.length; i++) {
For (var j = 0; J < Znodes.length; J + +) {
if (nodes[i] = = = Treeobj.getnodebyparam ("id", Znodes[j].dataauthorityid)) {
Treeobj.checknode (Treeobj.getnodebyparam ("id", Znodes[j].dataauthorityid), true);
}
}
nodes[i].chkdisabled = true;
}
Effect:
Tips:
In the actual project, sometimes it is necessary to pass the selected node in JSON to the background, since to get the node, it is of course to start from the node:
var json = [];
var treeobj=$.fn.ztree.getztreeobj ("CategoryTree");
var nodes=treeobj.getcheckednodes (true);
for (var i = 0; i < nodes.length; i++) {
var obj = {};
Obj.dataauthorityid = nodes[i].id;
Obj.parentid = Nodes[i].pid;
Obj.name = Nodes[i].name;
Obj.deep = Nodes[i].deep;
Json.push (obj);
}
JSON = JSON.STRINGFY (JSON); To pass as a parameter to the background, don't forget to serialize
See what we Got:
[{"Dataauthorityid": "XX", "ParentID": null, "name": "Capital Directory System", "Deep": "0"},{"Dataauthorityid": "Gaozhong", "ParentID": "XX", "name": "High School", "Deep": "1"},{"Dataauthorityid": "Gaozhong-shuxue", "ParentID": "Gaozhong", "name": "Math", "Deep": "2" },{"Dataauthorityid": "Xiaoxue", "ParentID": "XX", "name": "Primary School", "Deep": "1"},{"Dataauthorityid": "Xiaoxue-shuxue", " ParentID ":" Xiaoxue "," name ":" Math "," Deep ":" 2 "},{" Dataauthorityid ":" Xiaoxue-yingyu "," ParentID ":" Xiaoxue "," Name ":" English "," Deep ":" 2 "}]
The level is limited, welcome to criticize correct!
Ztree Select nodes based on JSON and set other nodes not selectable