欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jQuery zTree树插件简单使用教程

程序员文章站 2022-06-29 22:18:39
前言 ztree 是一个依靠 jquery 实现的多功能 “树插件”。优异的性能、灵活的配置、多种功能的组合是 ztree 最大优点。 支持静态 和 ajax 异步加...

前言

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

下载地址:ztree

静态ztree开发流程

引入资源

<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元素

<div>
 <ul id="treedemo" class="ztree"></ul>
</div>

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

<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数据格式

<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

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层

@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实体类

public class treeinfo {

  private string id;
  private string pid;
  private string name;
  private string isparent;
  }

说明

更多实例可以参看ztree中文文档
或参见ztree github 里面更多的示例和说明。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。