The use of Jquery-ztree

Source: Internet
Author: User

  • "section Functions and properties Introduction"

  • Core: Ztree (setting, [Ztreenodes])

    This function takes a JSON-formatted data object setting and a JSON-formatted data object Ztreenodes to build the Tree.

  • Core parameter: Setting

    Ztree parameter configuration is done here, simply say: Tree style, events, access path, etc. are configured here

    setting Example:

    JS Code
      1. var setting = {
      2. ShowLine: True,
      3. Checkable: True
      4. };

    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
    1. var ztreenodes = [
    2. {"id": 1, "name":"Test1", "nodes": [
    3. {"id": one, "name":"test11", "nodes": [
    4. {"id": 111, "name":"test111"}
    5. ]},
    6. {"id": " Name": "test12"}
    7. ]},
    8. ......
    9. ];

    ② Ztreenodes Example of a simple Array format (Issimpledata) with a parent-child relationship:

    JS Code
    1. var treenodes = [
    2. {"id": 1, "PId": 0, "name":"Test1"},
    3. {"id": One, "pId": 1, "name":"test11"},
    4. {"id": " pId": 1, "name":"test12"},
    5. {"id": 111, "PId": One, "name":"test111"},
    6. ......
    7. ];

    ① in the page reference Ztree JS and CSS:

    HTML code
    1. <!--ztree tree-shaped plug-in
    2. <link rel= "stylesheet" href= "<%=root%>/web/common/css/ztreestyle/ztreestyle.css" type="Text/css">
    3. <!--<link rel= "stylesheet" href= "<%=root%>/web/common/css/ztreestyle/ztreeicons.css" type= "Text/css" >
    4. <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
    1. var setting = {
    2. Issimpledata: True, //data is in simple Array format, default false
    3. Treenodekey: "id", //In issimpledata format, current Node ID property
    4. Treenodeparentkey: "PId", //In issimpledata format, the parent Node ID property of the current node
    5. ShowLine: True, //whether the connection between nodes is displayed
    6. Checkable: true //CheckBox is displayed on each node
    7. };
    8. var treenodes = [
    9. {"id":1, "pId":0, "name":"Test1"},
    10. {"id": one, "pId":1, "name":"test11"},
    11. {"id": " pId":1, "name":"test12"},
    12. {"id":111, "PId": one, "name":"test111"},
    13. ];

    ③ the tree structure when entering the page:

    JS Code
      1. var Ztree;
    JS Code
      1. $ (function () {
      2. Ztree = $ ("#tree"). Ztree (setting, treenodes);
      3. });

    ④ Last View effect:

    "Instance two" (Get the simple format JSON data from the background)

    ① background code encapsulates simple format JSON data:

    Java code
    1. Public void Dogetprivilegetree () throws ioexception{
    2. String S1 = "{id:1, pid:0, name:\" test1\ ", open:true}";
    3. String s2 = "{id:2, pid:1, name:\" test2\ ", open:true}";
    4. String s3 = "{id:3, pid:1, name:\" test3\ ", open:true}";
    5. String S4 = "{id:4, Pid:2, name:\" test4\ ", open:true}";
    6. list<string> lsttree = new arraylist<string> ();
    7. Lsttree.add (S1);
    8. Lsttree.add (S2);
    9. Lsttree.add (S3);
    10. Lsttree.add (S4);
    11. //Use JSON plugin to convert array to JSON format
    12. Response.getwriter (). Print (Jsonarray.fromobject (lsttree). toString ());
    13. }

    ② page using Ajax to get Ztreenodes data regenerated into a tree

    JS Code
    1. var setting = {
    2. Issimpledata: True, //data is in simple Array format, default false
    3. Treenodekey: "id", //In issimpledata format, current Node ID property
    4. Treenodeparentkey: "PId", //In issimpledata format, the parent Node ID property of the current node
    5. ShowLine: True, //whether the connection between nodes is displayed
    6. Checkable: true //CheckBox is displayed on each node
    7. };
    8. var Ztree;
    9. var treenodes;
    10. $ (function () {
    11. $.ajax ({
    12. Async: false,
    13. Cache:false,
    14. Type: ' POST ',
    15. DataType: "JSON",
    16. url:root+"/ospm/logininfo/dogetprivilegetree.action",//Requested action path
    17. Error: function () {//request failed handler
    18. Alert (' request failed ');
    19. },
    20. Success:function (data) { //request succeeded after processing.
    21. alert (data);
    22. TreeNodes = data; //Give TreeNodes a simple JSON format packaged in the background
    23. }
    24. });
    25. Ztree = $ ("#tree"). Ztree (setting, treenodes);
    26. });

    ③ Final Display effect

The use of Jquery-ztree

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.