jquery zTree异步加载的例子

时间:2021-07-27 04:08:57

下面是使用zTree异步加载的一个例子:

1)初始化树的时候是ajax请求,返回nodes列表来初始化树的;如果一开始就异步的话,$.fn.zTree.init($("#zTree"),setting, data)第三个参数为null就行;

2)后面点击最末端的节点,比如点击单板的时候,开始异步加载;

我准备的nodes的数据结构:一会返回的node就是这样的格式,不过全部是string类型;

var nodes =
[
{
'id': 1,
'pid': 0,
'name': '硬件规格',
'open':false
},
{
'id': 10,
'pid': 1,
'name': '单板',
'open':false
},
//比如点击单板+,要异步加载的两个节点,模拟用
{
'id': 100,
'pid': 10,
'name': '单板_00',
'open':false
},
{
'id': 101,
'pid': 10,
'name': '单板_01',
'open':false
}, {
'id': 11,
'pid': 1,
'name': '子卡',
'open':false
},
{
'id': 12,
'pid': 1,
'name': '设备',
'open':false
},
{
'id': 2,
'pid': 0,
'name': '软件规格',
'open':false
},
{
'id': 20,
'pid': 2,
'name': 'java',
'open':false
},
{
'id': 21,
'pid': 2,
'name': 'jscript',
'open':false
},
{
'id': 22,
'pid': 2,
'name': 'php',
'open':false
}
]

index.jsp:

<%@ 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>
<h4>Ztree异步加载使用例子</h4>
<ul id="zTree" class="ztree"></ul>
</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">
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
}
};
$(document).ready(function(){
initZTree();
}); function filter(treeId, parentNode, childNodes) {
return childNodes;
}
//初始化树
function initZTree(){
$.ajax({
url:"getNodes",
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;
} }

Controller:

package com.cy.controller;

import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.test.model.Node; @Controller
public class ZtreeController { @RequestMapping("/getNodes")
@ResponseBody
public List<Node> getNodes() throws Exception{
List<Node> nodeList = new ArrayList<Node>();
nodeList.add(new Node("1","0","硬件规格","false","true"));
nodeList.add(new Node("10","1","单板","false","true"));
nodeList.add(new Node("11","1","子卡","false","true"));
nodeList.add(new Node("12","1","设备","false","true"));
nodeList.add(new Node("2","0","软件规格","false","true"));
nodeList.add(new Node("20","2","java","false","true"));
nodeList.add(new Node("21","2","jscript","false","true"));
nodeList.add(new Node("22","2","php","false","true"));
return nodeList;
} @RequestMapping("/asyncGetNodes")
@ResponseBody
public List<Node> asyncGetNodes(String id, String pid, String name) throws Exception{
List<Node> nodeList = new ArrayList<Node>();
if(id.equals("10")){
nodeList.add(new Node("100",id,"单板_00","false","false"));
nodeList.add(new Node("101",id,"单板_01","false","false"));
}
Thread.sleep(3000);
return nodeList;
}
}

Thread.sleep(3000);是模拟异步加载的时间;

效果:
一开始初始化完tree时候,让第一个父节点展开了;

jquery zTree异步加载的例子

异步加载过程中....

jquery zTree异步加载的例子

异步加载完成:

jquery zTree异步加载的例子

------------------------------------------------------------------------------------------------------------------------------------------------------------------

jquery zTree异步加载的例子的更多相关文章

  1. JQuery ztree 异步加载实践

    本来要做一个文件目录浏览界面,需要遍历所有的文件和目录,很显然一次性读取时很费时费力的一件事情. 因此就需要做异步加载.... 不过网上的几篇帖子还挺坑的!原始参考:JQuery异步加载实例,相对来说 ...

  2. Jquery Ztree异步加载树

    1. 下载jquery的JS文件/ztree的CSS文件和JS文件 https://jquery.com/download/ https://gitee.com/zTree/zTree_v3/tree ...

  3. zTree 异步加载

    zTree异步加载数据的简单实现,更为详细的Api请参考 zTree官网   http://www.treejs.cn/ <link href="~/Content/ztree/css ...

  4. ztree异步加载

    Ztree异步加载的意思就是: 当点击展开树节点时,才去请求后台action返回点击节点的子节点数据并加载. 直接贴代码(SpringMvc+Mybatis): 前台页面ztreeList.jsp: ...

  5. ztree插件的使用及列表项拖拽的实现(jQuery)&plus;异步加载节点数据

    为了实现如图所示的树状结构图,并使列表项可拖动到盒子里,研究了ztree这个插件的使用,并仔细研究了列表项的拖动事件.完成了预期需求,对jQuery的运用得到了提高.这个插件的功能非常强大,除了基本的 ...

  6. Jquery树控件ZTree异步加载

    异步加载的意思就是: 当点击展开树节点时,才去请求后台action返回点击节点的子节点数据并加载. 这里面主要设计ztree的setting变量的async属性设置: var setting = { ...

  7. Ztree异步加载自动展开节点

    在Ztree的官网Demo中,有自动展开的例子,是通过设置节点属性open:true来实现自动展开的,但是在异步加载中,这个属性设置为true也不会自动展开,因为open:true是指在有子节点的情况 ...

  8. zTree异步加载并初始化树时全部展开(贴主要代码)

    <%@page pageEncoding="UTF-8"%> <%@include file="/commons/include/html_doctyp ...

  9. 插件使用一树形插件---zTree一zTree异步加载

    zTree 可以实现异步加载.异步加载可以让初次加载速度快,带来好的用户体验. 异步加载 官方源码中的demo提示了例子.例子是采用php语言. 在java语言中,zTree如何与Servlet结合呢 ...

随机推荐

  1. c&num;设计模式之简单工厂

    1.面向对象的3大属性,封装.继承.多态,以一个加单的计算机为例: 创建一个父类Operation 有两个属性 和一个计算方法(虚方法),便于子类重写: public class Operation ...

  2. Castle IOC FOR MVC 使用方法

    Castle Web.API 使用方法 一.创建 WindsorActivator 继承 IHttpControllerActivator public class WindsorActivator ...

  3. xib添加手势后报错&colon;-&lbrack;UITapGestureRecognizer setFrame&colon;&rsqb;&colon; unrecognized selector sent to instance xxx

    主要原因如下: + (instancetype)mineHeaderView { return [[NSBundle mainBundle] loadNibNamed:@"DDMineHea ...

  4. bash&comma;bg&comma;bind&comma;break&comma;builtin&comma;caller&comma;compgen&comma; complete&comma;compopt&comma;continue&comma;declare&comma;dirs&comma;disown&comma;enable&comma;eval&comma;exec&comma;expo

    bash,bg,bind,break,builtin,caller,compgen, complete,compopt,continue,declare,dirs,disown,enable,eval ...

  5. 字符编码笔记:ASCII,Unicode和UTF-8&lpar;转)

    字符编码笔记:ASCII,Unicode和UTF-8 作者: 阮一峰 日期: 2007年10月28日 今天中午,我突然想搞清楚Unicode和UTF-8之间的关系,于是就开始在网上查资料. 结果,这个 ...

  6. Github和Github for windows的使用简介

    很多程序员都把自己开发的代码放到Github上,方便自己管理也有利于别人查阅.所以这两天我也捣鼓了一下这个东西,现在把怎么使用Github和Github for windows简单的总结一下. 1.现 ...

  7. FastJson 支持配置的PropertyNamingStrategy四种策略

    摘要: FastJson默认使用CamelCase,在1.2.15版本之后,FastJson支持配置PropertyNamingStrategy,支持四种策略: CamelCase.PascalCas ...

  8. 【BZOJ】1443&colon; &lbrack;JSOI2009&rsqb;游戏Game

    [算法]博弈论+二分图匹配(最大流) [题解]方格图黑白染色得到二分图, 二分图博弈:当起点不属于某个最大匹配时,后手必胜. 问题转化为那些点不属于某个最大匹配. 先找到一个最大匹配,非匹配点加入答案 ...

  9. Linux之常用命令【service】

    补充说明 service命令 是Redhat Linux兼容的发行版中用来控制系统服务的实用工具,它以启动.停止.重新启动和关闭系统服务,还可以显示所有系统服务的当前状态. 语法 service(选项 ...

  10. ROS routeros mikrotik路由器CVE-2018-14847漏洞

    原文: https://securitynews.sonicwall.com/xmlpost/massive-cryptojacking-campaign/ SonicWall is observin ...