Core parameter: SettingZtree parameter configuration is done here, simply say: Tree style, events, access path, etc. are configured here
setting Example:
JS Code
- var setting = {
- ShowLine: True,
- Checkable: True
- };
Core parameter: Ztreenodes
Ztree is a collection of all node data, using a data structure composed of JSON objects, which simply says: This saves all of the tree's information in JSON format.
The format of the ztreenodes is divided into two types: nested in JSON format to reflect parent-child relationships and array simple formatting
① Standard Ztreenodes Examples with parent-child relationships:
JS Code
- var ztreenodes = [
- {"id": 1, "name":"Test1", "nodes": [
- {"id": one, "name":"test11", "nodes": [
- {"id": 111, "name":"test111"}
- ]},
- {"id": " Name": "test12"}
- ]},
- ......
- ];
② Ztreenodes Example of a simple Array format (Issimpledata) with a parent-child relationship:
JS Code
- var treenodes = [
- {"id": 1, "PId": 0, "name":"Test1"},
- {"id": One, "pId": 1, "name":"test11"},
- {"id": " pId": 1, "name":"test12"},
- {"id": 111, "PId": One, "name":"test111"},
- ......
- ];
① in the page reference Ztree JS and CSS:
HTML code
- <!--ztree tree-shaped plug-in
- <link rel= "stylesheet" href= "<%=root%>/web/common/css/ztreestyle/ztreestyle.css" type="Text/css">
- <!--<link rel= "stylesheet" href= "<%=root%>/web/common/css/ztreestyle/ztreeicons.css" type= "Text/css" >
- <script type="Text/javascript" src= "<%=root%>/web/common/js/jquery-ztree-2.5.min.js "></script>
② defining setting and Ztreenodes in the script script
Java code
- var setting = {
- Issimpledata: True, //data is in simple Array format, default false
- Treenodekey: "id", //In issimpledata format, current Node ID property
- Treenodeparentkey: "PId", //In issimpledata format, the parent Node ID property of the current node
- ShowLine: True, //whether the connection between nodes is displayed
- Checkable: true //CheckBox is displayed on each node
- };
- var treenodes = [
- {"id":1, "pId":0, "name":"Test1"},
- {"id": one, "pId":1, "name":"test11"},
- {"id": " pId":1, "name":"test12"},
- {"id":111, "PId": one, "name":"test111"},
- ];
③ the tree structure when entering the page:
JS Code
- var Ztree;
JS Code
- $ (function () {
- Ztree = $ ("#tree"). Ztree (setting, treenodes);
- });
④ Last View effect:
"Instance two" (Get the simple format JSON data from the background)
① background code encapsulates simple format JSON data:
Java code
- Public void Dogetprivilegetree () throws ioexception{
- String S1 = "{id:1, pid:0, name:\" test1\ ", open:true}";
- String s2 = "{id:2, pid:1, name:\" test2\ ", open:true}";
- String s3 = "{id:3, pid:1, name:\" test3\ ", open:true}";
- String S4 = "{id:4, Pid:2, name:\" test4\ ", open:true}";
- list<string> lsttree = new arraylist<string> ();
- Lsttree.add (S1);
- Lsttree.add (S2);
- Lsttree.add (S3);
- Lsttree.add (S4);
- //Use JSON plugin to convert array to JSON format
- Response.getwriter (). Print (Jsonarray.fromobject (lsttree). toString ());
- }
② page using Ajax to get Ztreenodes data regenerated into a tree
JS Code
- var setting = {
- Issimpledata: True, //data is in simple Array format, default false
- Treenodekey: "id", //In issimpledata format, current Node ID property
- Treenodeparentkey: "PId", //In issimpledata format, the parent Node ID property of the current node
- ShowLine: True, //whether the connection between nodes is displayed
- Checkable: true //CheckBox is displayed on each node
- };
- var Ztree;
- var treenodes;
- $ (function () {
- $.ajax ({
- Async: false,
- Cache:false,
- Type: ' POST ',
- DataType: "JSON",
- url:root+"/ospm/logininfo/dogetprivilegetree.action",//Requested action path
- Error: function () {//request failed handler
- Alert (' request failed ');
- },
- Success:function (data) { //request succeeded after processing.
- alert (data);
- TreeNodes = data; //Give TreeNodes a simple JSON format packaged in the background
- }
- });
- Ztree = $ ("#tree"). Ztree (setting, treenodes);
- });
③ Final Display effect