使用zTree进行数据动态显示
由于公司项目的须要。现学了一下zTree的使用。
以下是我项目的结构图:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYmVuamFtaW5fd2h4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
baseDao类
package com.wiseweb.zTree; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement; public class BaseDao {
static Connection conn ;
PreparedStatement pstm ;
ResultSet rs ; public static Connection getConnection(){
try {
Class.forName("com.mysql.jdbc.Driver") ;
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/crud", "root", "root") ;
System.out.println("------------打开连接---------------");
} catch (SQLException e) {
System.out.println("------------连接失败---------------");
e.printStackTrace();
}
} catch (ClassNotFoundException e) {
System.out.println("-----------------驱动载入失败---------------");
e.printStackTrace();
}
return conn ;
}
public static void closeConnection(ResultSet rs,Statement st,Connection conn){
try {
if(rs != null){
rs.close() ;
}
if(st != null){
st.close() ;
}
if(conn != null){
conn.close() ;
}
System.out.println("----------------关闭连接----------------");
} catch (SQLException e) {
System.out.println("----------------关闭连接失败----------------");
e.printStackTrace();
}
}
}
实体Competence类
package com.wiseweb.zTree; public class Competence {
private int id ;
private int pId ;
private int isParent ;
private String name ;
private int open ;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getpId() {
return pId;
}
public void setpId(int pId) {
this.pId = pId;
}
public int getIsParent() {
return isParent;
}
public void setIsParent(int isParent) {
this.isParent = isParent;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getOpen() {
return open;
}
public void setOpen(int open) {
this.open = open;
} }
Test类
package com.wiseweb.zTree; import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; public class Test extends BaseDao{
public List<Competence> getAllAuthorize(){
List<Competence> authorizes = new ArrayList<Competence>() ;
Connection conn = getConnection() ;
try {
pstm = conn.prepareStatement("select * from competence") ;
rs = pstm.executeQuery() ;
while(rs.next()){
Competence authorize = new Competence() ;
authorize.setId(rs.getInt("id")) ;
authorize.setName(rs.getString("name")) ;
authorize.setIsParent(rs.getInt("isParent")) ;
authorize.setOpen(rs.getInt("open")) ;
authorize.setpId(rs.getInt("pId")) ;
authorizes.add(authorize) ;
}
} catch (SQLException e) {
System.out.println("-----------查询competence失败-------------");
e.printStackTrace();
}finally{
closeConnection(rs,pstm,conn) ;
}
return authorizes ;
}
public String getJsonData(){
List<Competence> list = getAllAuthorize() ;
StringBuffer json = new StringBuffer("[") ;
String data = "" ;
int length = list.size() ;
for(int i=0;i<length;i++){
json.append("{id:" + list.get(i).getId() + ",") ;
json.append("pId:" + list.get(i).getpId() + ",") ;
json.append("name:\"" + list.get(i).getName() + "\",") ;
if(list.get(i).getIsParent() != 0){
json.append("isParent:" + list.get(i).getIsParent() + ",") ;
}
if(list.get(i).getOpen() != 0){
json.append("open:" + list.get(i).getOpen() + ",") ;
}
data = json.substring(0,json.lastIndexOf(",")) + "}," ;
json = new StringBuffer(data) ;
}
data = json.substring(0,json.length()-1) + "]" ;
System.out.println(data);
return data ;
}
public static void main(String[] args) {
new Test().getJsonData() ;
}
}
asynload.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>asynload.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="css/demo.css" type="text/css">
<link rel="stylesheet" href="css/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-3.5.js"></script> <script type="text/javascript">
var setting = {
async:{
enable:true,
url:"loadData.jsp",
autoParam:["id"],
},
data:{
simpleData:{
enable:true,
idKey:"id",
pIdKey:"pId",
rootPId:0
}
},
callback:{
onClick:function(event,treeId,treeNode,clickFlag){
if(!treeNode.isParent){
alert("treeId自己主动编号:" + treeNode.tId + ",节点id是:" + treeNode.id + ",节点文本是:" + treeNode.name) ;
}
},
onAsyncError:zTreeOnAsyncError,
onAsyncSuccess:function(event,treeId,treeNode,msg){ }
}
};
function filter(treeId, parentNode, childNodes) {
if (!childNodes)
return null;
for ( var i = 0, l = childNodes.length; i < l; i++) {
childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
}
return childNodes;
}
function zTreeOnAsyncError(event,treeId,treeNode,XMLHttpRequest,textStatus,errorThrown){
alert("载入错误:" + XMLHttpRequest) ;
}
$(document).ready(function(){
$.fn.zTree.init($("#treeDemo"),setting) ;
}) ;
</script>
</head> <body>
<div>
<ul id="treeDemo" class="zTree"></ul>
</div>
</body>
</html>
index.jsp主程序
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
//String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
request.setAttribute("ctx",request.getContextPath());
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>zTree</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" href="${ctx }/css/demo.css" type="text/css">
<link rel="stylesheet" href="${ctx }/css/zTreeStyle.css" type="text/css">
<script type="text/javascript" src="${ctx }/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="${ctx }/js/jquery.ztree.core-3.5.js"></script> <script type="text/javascript">
var setting = {
async:{
enable:true,
url:"loadData.jsp",
autoParam:["id"],
},
data:{
simpleData:{
enable:true,
idKey:"id",
pIdKey:"pId",
rootPId:0
}
},
callback:{
onClick:function(event,treeId,treeNode,clickFlag){
if(!treeNode.isParent){
alert("treeId自己主动编号:" + treeNode.tId + ",节点id是:" + treeNode.id + ",节点文本是:" + treeNode.name) ;
}
},
onAsyncError:zTreeOnAsyncError,
onAsyncSuccess:function(event,treeId,treeNode,msg){ }
}
};
function filter(treeId, parentNode, childNodes) {
if (!childNodes)
return null;
for ( var i = 0, l = childNodes.length; i < l; i++) {
childNodes[i].name = childNodes[i].name.replace(/\.n/g, '.');
}
return childNodes;
}
function zTreeOnAsyncError(event,treeId,treeNode,XMLHttpRequest,textStatus,errorThrown){
alert("载入错误:" + XMLHttpRequest) ;
}
$(document).ready(function(){
$.fn.zTree.init($("#treeDemo"),setting) ;
}) ;
</script>
</head> <body>
<div>
<ul id="treeDemo" class="zTree"></ul>
</div>
</body>
</html>
loadData.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="com.wiseweb.zTree.*" %>
<%
String id = request.getParameter("id") ;
System.out.println("得到的节点id: " + id) ;
Test demo = new Test() ;
String json = demo.getJsonData() ;
out.print(json) ;
%>
zTree.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>zTree.html</title> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="css/demo.css" type="text/css">
<link rel="stylesheet" href="css/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-3.5.js"></script>
<!--<link rel="stylesheet" type="text/css" href="./styles.css">--> <script type="text/javascript">
function setFontCss(treeId,treeNode){
return treeNode.level==0?{color:"red"}:{};
}
var setting = {
data:{
simpleData: {
enable: true,
idKey: "id",
pIdKey: "pId",
rootPId:0,
}
},
view:{
showLine: false,//是否显示连接线
//showIcon: false,//是否显示节点图标
//showTitle: false,//是否显示节点的title提示信息
fontCss: setFontCss,//改变字体颜色和样式
}
};
var zNodes = [
{id:1, pId:0, name:"父节点1-展开", open:true, iconOpen:"WEB-INF/image/1_open.png", iconClose:"WEB-INF/image/1_close.png"},
{id:11, pId:1, name:"父节点11-折叠", icon:"WEB-INF/image/2.png"},
{id:111, pId:11, name:"叶子节点111",url:"http://www.baidu.com"},//超链接
{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>
</head> <body>
<h1>最简单的树--简单的json数据</h1>
<ul id="treeDemo" class="ztree"></ul>
</body>
</html>
数据库图片:
浏览器输入http://localhost:8080/testZtree进行显示
结果为:
使用zTree进行数据动态显示的更多相关文章
- 爱上MVC3~MVC+ZTree大数据异步树加载
回到目录 理论部分: MVC+ZTree:指在.net MVC环境下进行开发,ZTree是一个jquery的树插件 大数据:一般我们系统中,有一些表结构属于树型的,如分类,地域,菜单,网站导航等等,而 ...
- MVC小系列(十四)【MVC+ZTree大数据异步树加载】
ZTree是一个jquery的树插件可以异步加载 第一步定义一个标准的接口(指的是与ztree默认的数据元素保持一致) /// <summary> /// ZTree数据结构 /// &l ...
- MVC+ZTree大数据异步树加载
实例部分: 首先是为ZTree提供的数据规范,定义一个标准的接口,这样对于前台调用是清楚的,简单的,因为它返回的JSON数据将与ZTree默认的数据元素保持一致 /// <summary> ...
- ztree多种数据包装以及相关设置
首先来一个完整的ztree代码 html代码 <form id="addTreeDataFrm" method="post" class="fo ...
- 项目一:项目第二天 Jquery ztree使用展示菜单数据 2、 基础设置需求分析 3、 搭建项目框架环境--ssh(复习) 4、 SpringData-JPA持久层入门案例(重点) 5、 Easyui menubutton菜单按钮使用 6、 Easyui messager消息框使用
1. Jquery ztree使用展示菜单数据 2. 基础设置需求分析 3. 搭建项目框架环境--ssh(复习) 4. SpringData-JPA持久层入门案例(重点) 5. Easyui menu ...
- jQuery Ztree基本用法
1.首先在页面上有<ul/>标签 <ul id="tree" class="ztree"></ul> 2.定义ztree的配 ...
- jq生成目录文件树jQuery Ztree基本用法
转自:http://www.cnblogs.com/linjiqin/p/4547452.html 1.首先在页面上有<ul/>标签 ? 1 <ul id="tree&qu ...
- 转:zTree树控件扩展篇:巧用zTree控件实现文本框输入关键词自动模糊查找zTree树节点实现模糊匹配下拉选择效果
是否可以借助于zTree实现文本框输入关键词自动模糊匹配zTree下拉树,然后选择下拉树内节点显示在文本框内且隐藏下拉树. 看到这个需求脑子里头大致已经想到了要如何实现这样一个需求,当时是限于时间问题 ...
- 转:zTree树控件key配置之title:zTree树节点名称过长如何省略显示且鼠标移入节点上能够显示全称
当树节点的名称有些很长时,全部显示出来显得很拥挤的情况下,我们会想到用省略节点名称来代替,当鼠标移入节点时能够显示该节点的全称.这样我们应该如何做呢? 首先,我们要在树的节点内多增加一个属性用于设置该 ...
随机推荐
- LeetCode(103) Binary Tree Zigzag Level Order Traversal
题目 Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left ...
- LeetCode(118) Pascal's Triangle
题目 Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, R ...
- 哈夫曼树:HDU5884-Sort(队列、哈夫曼树)
Sort Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) 题目链接:http://ac ...
- Knockout v3.4.0 中文版教程-11-控制文本内容和外观-text绑定
2. text绑定 目的 text绑定把传入的参数通过关联的DOM元素来显示文本值. 通常这对像<span>或<em>标签等使用,但技术上你可以对任何元素使用该绑定. 例子 T ...
- 面试准备——Zookeeper
转自https://www.cnblogs.com/shan1393/p/9479109.html 1. Zookeeper是什么框架 分布式的.开源的分布式应用程序协调服务,原本是Hadoop.HB ...
- mac osx下apache下的坑: you don’t have permission to access / on this server
在Mac下Apache修改默认站点的目录时,遇到403错误, you don’t have permission to access / on this server 首先按照google到教程: 修 ...
- Laya List翻页滚动方案 & List滚动源码解析
Laya List翻页滚动方案 & List滚动源码解析 @author ixenos 2019-03-29 1.List翻页滚动方案 /** * 计算下一页的起始索引, 不足时补足 * @p ...
- apr的使用
APR(Apache Portable Runtime),即Apache可移植运行库,正如官网所言,APR的使命是创建和维护一套软件库,以便在不同操作系统(Windows.Linux等)底层实现的基础 ...
- bzoj1584 [Usaco2009 Mar]Cleaning Up 打扫卫生 动态规划+思维
Description 有N头奶牛,每头那牛都有一个标号Pi,1 <= Pi <= M <= N <= 40000.现在Farmer John要把这些奶牛分成若干段,定义每段的 ...
- bzoj [Scoi2016]美味
[Scoi2016]美味 Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 721 Solved: 391[Submit][Status][Discus ...