参考博客:https://www.cnblogs.com/henuyuxiang/p/6677397.html

前台代码

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="resources/zTreeStyle/zTreeStyle.css" type="text/css">
<link rel="stylesheet" type="text/css" href="resources/bootstrap/bootstrap.min.css">
<title>index</title>
</head>
<body>
    <div class="container">
        <h4>Ztree异步加载使用例子</h4>
        <input type="text" />
        <ul ></ul>
    </div>
</body>
<script src="resources/js/jquery.min.js"></script>
<script type="text/javascript" src="resources/js/jquery.ztree.all.min.js"></script>
<script type="text/javascript" src="resources/js/jquery.ztree.exhide.min.js"></script>
<script type="text/javascript">
var setting = {
        async: {
            enable: true,
            url:"asyncGetNodes",
            autoParam:["id", "pid", "name"],
            dataFilter: filter
        },
        data:{
            simpleData:{
                enable: true,
                idKey:'id',
                pIdKey:'pid',
                rootPId: 0
            }
        },
        view:{
            showIcon: false
        },
        callback: {
            onNodeCreated: zTreeOnNodeCreated
          }
};
$(document).ready(function(){
    initZTree();
    
});

/**
 * 搜索
 */
function search(){
    var param = $.trim($("#search").val());
    var treeObj = $.fn.zTree.getZTreeObj("zTree");
    if(param != ""){
        param = encodeURI(encodeURI(param));
        treeObj.setting.async.otherParam=["param", param];
      }else {
        //搜索参数为空时必须将参数数组设为空
        treeObj.setting.async.otherParam=[];
      }
    
      treeObj.reAsyncChildNodes(null, "refresh");
}

function zTreeOnNodeCreated(event, treeId, treeNode){
    var param = $.trim($("#search").val());
    var treeObj = $.fn.zTree.getZTreeObj(treeId);
    if(param!="" && treeNode.isParent){
        treeObj.reAsyncChildNodes(treeNode, "refresh",false);        
    }
}

function filter(treeId, parentNode, childNodes) {
    return childNodes;
}
//初始化树
function initZTree(){
    $.ajax({
          url:"asyncGetNodes",
          type:"post",
          dataType: "json",
          success: function(data){
              console.log(data);
              var zTreeObj = $.fn.zTree.init($("#zTree"),setting, data); 
              //让第一个父节点展开
              var rootNode_0 = zTreeObj.getNodeByParam('pid',0,null);
              zTreeObj.expandNode(rootNode_0, true, false, false, false);
          },
          error: function(){
              
          }
      });
}

</script>
</html>

Node.java:

package com.test.model;

public class Node {
    private String id;
    private String pid;
    private String name;
    private String open;
    private String isParent;
    
    public Node(String id, String pid, String name, String open, String isParent) {
        super();
        this.id = id;
        this.pid = pid;
        this.name = name;
        this.open = open;
        this.isParent = isParent;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getPid() {
        return pid;
    }
    public void setPid(String pid) {
        this.pid = pid;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getOpen() {
        return open;
    }
    public void setOpen(String open) {
        this.open = open;
    }
    public String getIsParent() {
        return isParent;
    }
    public void setIsParent(String isParent) {
        this.isParent = isParent;
    }
    
    
}
View Code

相关文章:

  • 2022-12-23
  • 2021-05-18
  • 2022-01-21
  • 2022-12-23
  • 2021-12-27
  • 2022-12-23
  • 2021-11-14
  • 2022-12-23
猜你喜欢
  • 2021-08-19
  • 2021-05-19
  • 2021-11-01
  • 2021-06-27
  • 2022-12-23
  • 2021-09-02
相关资源
相似解决方案