zoukankan      html  css  js  c++  java
  • ztree

    jQuery zTree树插件简单使用教程

     转载:https://www.jb51.net/article/102584.htm
     

    前言

    zTree 是一个依靠 jQuery 实现的多功能 “树插件”。优异的性能、灵活的配置、多种功能的组合是 zTree 最大优点。 
    支持静态 和 Ajax 异步加载节点数据. 
    在开发中我们常需要用到树状的展示.

    下载地址:zTree

    静态zTree开发流程

    引入资源

    1
    2
    3
    <link rel="stylesheet" href="../../../css/zTreeStyle/zTreeStyle.css" type="text/css">
    <script type="text/javascript" src="../../../js/jquery-1.4.4.min.js"></script>
    <script type="text/javascript" src="../../../js/jquery.ztree.core.js"></script>

    html元素

    1
    2
    3
    <div>
     <ul id="treeDemo" class="ztree"></ul>
    </div>

    JS方式一-简单json数据格式

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    <SCRIPT type="text/javascript">
      <!--
      var setting = {
       data: {
        simpleData: {
         enable: true
        }
       }
      };
     
      var zNodes =[
       { id:1, pId:0, name:"父节点1 - 展开", open:true},
       { id:11, pId:1, name:"父节点11 - 折叠"},
       { id:111, pId:11, name:"叶子节点111"},
       { id:112, pId:11, name:"叶子节点112"},
       { id:113, pId:11, name:"叶子节点113"},
       { id:114, pId:11, name:"叶子节点114"},
       { id:12, pId:1, name:"父节点12 - 折叠"},
       { id:121, pId:12, name:"叶子节点121"},
       { id:122, pId:12, name:"叶子节点122"},
       { id:123, pId:12, name:"叶子节点123"},
       { id:124, pId:12, name:"叶子节点124"},
       { id:13, pId:1, name:"父节点13 - 没有子节点", isParent:true},
       { id:2, pId:0, name:"父节点2 - 折叠"},
       { id:21, pId:2, name:"父节点21 - 展开", open:true},
       { id:211, pId:21, name:"叶子节点211"},
       { id:212, pId:21, name:"叶子节点212"},
       { id:213, pId:21, name:"叶子节点213"},
       { id:214, pId:21, name:"叶子节点214"},
       { id:22, pId:2, name:"父节点22 - 折叠"},
       { id:221, pId:22, name:"叶子节点221"},
       { id:222, pId:22, name:"叶子节点222"},
       { id:223, pId:22, name:"叶子节点223"},
       { id:224, pId:22, name:"叶子节点224"},
       { id:23, pId:2, name:"父节点23 - 折叠"},
       { id:231, pId:23, name:"叶子节点231"},
       { id:232, pId:23, name:"叶子节点232"},
       { id:233, pId:23, name:"叶子节点233"},
       { id:234, pId:23, name:"叶子节点234"},
       { id:3, pId:0, name:"父节点3 - 没有子节点", isParent:true}
      ];
     
      $(document).ready(function(){
       $.fn.zTree.init($("#treeDemo"), setting, zNodes);
      });
      //-->
     </SCRIPT>

    JS方式二-标准json数据格式

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    <SCRIPT type="text/javascript">
      <!--
      var setting = { };
     
      var zNodes =[
       { name:"父节点1 - 展开", open:true,
        children: [
         { name:"父节点11 - 折叠",
          children: [
           { name:"叶子节点111"},
           { name:"叶子节点112"},
           { name:"叶子节点113"},
           { name:"叶子节点114"}
          ]},
         { name:"父节点12 - 折叠",
          children: [
           { name:"叶子节点121"},
           { name:"叶子节点122"},
           { name:"叶子节点123"},
           { name:"叶子节点124"}
          ]},
         { name:"父节点13 - 没有子节点", isParent:true}
        ]},
       { name:"父节点2 - 折叠",
        children: [
         { name:"父节点21 - 展开", open:true,
          children: [
           { name:"叶子节点211"},
           { name:"叶子节点212"},
           { name:"叶子节点213"},
           { name:"叶子节点214"}
          ]},
         { name:"父节点22 - 折叠",
          children: [
           { name:"叶子节点221"},
           { name:"叶子节点222"},
           { name:"叶子节点223"},
           { name:"叶子节点224"}
          ]},
         { name:"父节点23 - 折叠",
          children: [
           { name:"叶子节点231"},
           { name:"叶子节点232"},
           { name:"叶子节点233"},
           { name:"叶子节点234"}
          ]}
        ]},
       { name:"父节点3 - 没有子节点", isParent:true}
     
      ];
     
      $(document).ready(function(){
       $.fn.zTree.init($("#treeDemo"), setting, zNodes);
      });
      //-->
     </SCRIPT>

    异步zTree加载

    前面的配置相同,在此不再阐述.主要是js不同.

    异步JS

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    var setting = {
     //可勾选
     check: {
       enable: true
      },
     data : {
      simpleData : {
       enable : true
      }
     }
    };
     
     function initTree(){
      var payFreq = $("#payFreq").val();
      var fyType = $('#fyType').val();
      var setHzType = $('#setHzType').val();
     
      $.ajax({
       url : "/demo/initTree",
       data : {payFreq:payFreq,
         fyType:fyType,
         setHzType:setHzType
       },
       success: function(object){
        var nodes = "";
        //拼接simple格式的json字符串
        $.each(object.data, function(i,item) {
         nodes+="{id:'"+item.id+"', pId:'"+item.pid+"', name:'"+item.name+"', isParent:'"+item.isParent+"'},";
        });
        var zNodes = "["+nodes+"]";
        var json = eval('(' + zNodes + ')');
        //console.log(json);
        zTreeInit(json);
       }
      });
     
     }
     /* 初始化树 */
     function zTreeInit(json) {
      $.fn.zTree.init($("#treeDemo"), setting, json);
      var zTree = $.fn.zTree.getZTreeObj("treeDemo");
      //全部展开
      zTree.expandAll(true);
      //Y代表勾选时,N代表取消勾选 p代表父节点,s代表字节点
      setting.check.chkboxType = { "Y" : "ps", "N" : "ps" };
     
     }

    java代码

    Controller层

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    @RequestMapping("initTree")
     @ResponseBody
     public DataMessage initTree(String setHzType,String payFreq,String fyType){
      params.put("setHzType", setHzType);
      params.put("fyType", fyType);
      params.put("fkmattr_xt", fkmattr_xt);
      //获取treeinfo列表
      List<TreeInfo> treeInfos = feeTransferService.initTree(params);
      return DataMessage.successData(treeInfos);
     }

    tree实体类

    1
    2
    3
    4
    5
    6
    7
    public class TreeInfo {
     
     private String id;
     private String pid;
     private String name;
     private String isParent;
     }
  • 相关阅读:
    字符串转换为整数
    解决.Net 3.1 alpine镜像无法连接Sql server的问题
    微信小程序之wepy自动化架构搭建(fly+wepy-plugin-replace)
    node+express+socket.io+mysql=通讯服务器搭建(一)
    vuejs学习——vue+vuex+vue-router项目搭建(二)
    vuejs学习——vue+vuex+vue-router项目搭建(一)
    vuejs学习——vue+vuex+vue-router项目搭建(三)
    vuejs学习--递归组件(树型表格分享)
    Simditor学习--vuejs集成simditor
    前端学习——实现事件代理
  • 原文地址:https://www.cnblogs.com/luckyuns/p/13230366.html
Copyright © 2011-2022 走看看