Ztree介绍
zTree 是一个依靠 jQuery 实现的多功能 “树插件”,优异的性能、灵活的配置、多种功能的组合是 zTree 最大优点
在使用的时候,先导入jquery才能导入zTree
使用
1)引入Ztree到项目中
2):拷贝js/css导入到当前页面
3)页面定义显示树的区域
demo01_ztree
<%@ page contentType="text/html;charset=UTF-8" isELIgnored="false" language="java" %>
<html>
<head>
<title>Title</title>
<% pageContext.setAttribute("path",request.getContextPath()); %>
<%-- 第一步:创建页面jsp--%>
<%-- 第二步:拷贝js/css导入到当前页面 --%>
<script type="text/javascript" src="${path}/plugins/ztree/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="${path}/plugins/ztree/js/jquery.ztree.all-3.5.min.js"></script>
<link rel="stylesheet" type="text/css" href="${path}/plugins/ztree/css/zTreeStyle/zTreeStyle.css">
// 第三步:页面定义显示树的区域
<SCRIPT type="text/javascript">
var setting = {
//支持复选
check: {
enable: true
},
// 后台可以提交简单数据给ztree
data: {
simpleData: {
enable: true
}
}
};
var zNodes =[
{ id:1, pId:0, name:"Sass管理", open:true},
{ id:11, pId:1, name:"企业管理", open:true,checked:true},
{ id:111, pId:1, name:"模块管理"}
];
$(document).ready(function(){
$.fn.zTree.init($("#treeDemo"), setting, zNodes);
//参1 显示的标签
//参2 设置的参数 比如支持复选 check enable = true
//参3 数据
});
</SCRIPT>
</head>
<body>
<ul id="treeDemo" class="ztree"></ul>
</body>
</html>
测试后台传数据给树状菜单
传入的数据为List<Map<String,Object>>类型
ZTreeTest(测试传入数据类型)
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ZTreeTest {
@Test
public void test01(){
/* var zNodes =[
{ id:1, pId:0, name:"Sass管理", open:true},
{ id:11, pId:1, name:"企业管理", open:true,checked:true},
{ id:111, pId:1, name:"模块管理"}
];*/
// []表示list集合, List<元素类型>
// 但是由于[]内的元素的字段不一致,有的有4个或者5个字段,有的只有3个字段,所以只有Map才行 元素类型为 Map<String,Object>
List<Map<String,Object>> list=new ArrayList<Map<String, Object>>();
Map<String,Object> map01=new HashMap<String, Object>();
map01.put("id",1);
map01.put("pId",0);
map01.put("name","Sass管理");
map01.put("open",true);
Map<String,Object> map02=new HashMap<String, Object>();
map02.put("id",11);
map02.put("pId",0);
map02.put("name","企业管理");
map02.put("open",true);
map02.put("check",true);
Map<String,Object> map03=new HashMap<String, Object>();
map03.put("id",11);
map03.put("pId",0);
map03.put("name","模块管理");
list.add(map01);
list.add(map02);
list.add(map03);
String json = null;
try {
json = new ObjectMapper().writeValueAsString(list);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
System.out.println(json);
}
}
TestZtreeDataController(测试json是否传到页面)
@Controller
@RequestMapping("/test")
public class TestZtreeDataController extends BaseController {
@RequestMapping(path="/getZtreeData",method ={ RequestMethod.GET, RequestMethod.POST})
public @ResponseBody
Object getZtreeData(){
List<Map<String,Object>> list=new ArrayList<>();
Map<String,Object> node1=new HashMap<String,Object>();
node1.put("id",1);
node1.put("pId",0);
node1.put("name","Sass管理");
node1.put("open",true);
Map<String,Object> node2=new HashMap<String,Object>();
node2.put("id",11);
node2.put("pId",1);
node2.put("name","企业管理");
node2.put("open",true);
node2.put("checked",true);
Map<String,Object> node3=new HashMap<String,Object>();
node3.put("id",111);
node3.put("pId",1);
node3.put("name","模块管理");
//因为三个元素放在[]中,所以本质上放到集合中的
list.add(node1);
list.add(node2);
list.add(node3);
return list; //list虽然是对象,但会被 @ResponseBody转成json
}
}
demo04_ztree.jsp(测试后台传数据给树状菜单)
<script type="text/javascript">
//当前的配置信息
var setting = {
check: {
enable: true
},
data: {
simpleData: {
enable: true
}
}
};
//当前的数据
/*var zNodes =[
{ id:1, pId:0, name:"Sass管理", open:true},
{ id:11, pId:1, name:"企业管理", open:true,checked:true},
{ id:111, pId:1, name:"模块管理"}
];*/
$(document).ready(function(){
var fn =function(data){
//菜单的初始化
$.fn.zTree.init($("#treeDemo"), setting, data);
//参1 显示的标签
//参2 设置的参数 比如支持复选 check enable = true
//参3 数据
}
$.get('${path}/test/getZtreeData.do',fn,'json')
});
</script>