Ztree Select nodes based on JSON and set other nodes not selectable

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.