从数据库读取数据并动态生成easyui tree构结
一、 数据库表结构

二、从后台读取数据库生成easyui tree结构的树
1、TreeNode树结点类(每个结点都包含easyui tree 的基本属性信息)
import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; public class TreeNode implements Serializable { private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
private String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
private String parentid;
public String getParentid() {
return parentid;
}
public void setParentid(String parentid) {
this.parentid = parentid;
} private List<TreeNode> children = new ArrayList<TreeNode>();
public List<TreeNode> getChildren() {
return children;
}
public void setChildren(List<TreeNode> children) {
this.children = children;
} private String iconCls;
public String getIconCls() {
return iconCls;
}
public void setIconCls(String iconCls) {
this.iconCls = iconCls;
} private String state;//节点状态,'open' 或 'closed',默认:'open'。
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
} private boolean checked;
public boolean isChecked() {
return checked;
}
public void setChecked(boolean checked) {
this.checked = checked;
} private Map<String, Object> attributes = new HashMap<String, Object>();
public Map<String, Object> getAttributes() {
return attributes;
}
public void setAttributes(Map<String, Object> attributes) {
this.attributes = attributes;
} }
2、用递归方法生成easyui tree 格式的json字符串
public static TreeNode recursiveTree(String id) {
// 根据id获取节点对象(SELECT * FROM ttree t WHERE t.id=?)
TreeNode node = getTreeNode(id);
// 查询id下的所有子节点(SELECT * FROM ttree t WHERE t.parentid=?)
List<TreeNode> childTreeNodes = queryTreeNode(id);
// 遍历子节点
for (TreeNode child :childTreeNodes) {
TreeNode n = recursiveTree(child.getId()); // 递归
node.getChildren().add(n);
}
return node;
}
private static TreeNode getTreeNode(String id){
TreeNode treeNode = null;
Connection dbConn = getDbConnection();
if(dbConn != null){
try {
String strSQL = "select * from ttree where id="+id;
PreparedStatement pstmt = null;
ResultSet rs = null;
pstmt = dbConn.prepareStatement(strSQL);
rs = pstmt.executeQuery();
while (rs.next()) {
treeNode = new TreeNode();
treeNode.setId(rs.getString("id"));
treeNode.setText(rs.getString("text"));
treeNode.setParentid(rs.getString("parentid"));
Map<String, Object> attributes = new HashMap<String, Object>();
attributes.put("url",rs.getString("url"));
treeNode.setAttributes(attributes);
}
rs.close();
pstmt.close();
dbConn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return treeNode;
}
private static List<TreeNode> queryTreeNode(String id){
List<TreeNode> arrList = new ArrayList<TreeNode>();
Connection dbConn = getDbConnection();
TreeNode treeNode = null;
if(dbConn != null){
try {
String strSQL = "select * from ttree where parentid="+id;
PreparedStatement pstmt = null;
ResultSet rs = null;
pstmt = dbConn.prepareStatement(strSQL);
rs = pstmt.executeQuery();
while (rs.next()) {
treeNode = new TreeNode();
treeNode.setId(rs.getString("id"));
treeNode.setText(rs.getString("text"));
treeNode.setParentid(rs.getString("parentid"));
Map<String, Object> attributes = new HashMap<String, Object>();
attributes.put("url",rs.getString("url"));
treeNode.setAttributes(attributes);
arrList.add(treeNode);
}
rs.close();
pstmt.close();
dbConn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return arrList;
}
3、测试调用(JSON.toJSONString方法是使用的fastjson.jar包中的方法功能很强大强烈推荐,可以在网上下载)
public static void main(String[] args) {
TreeNode node = recursiveTree("1");
String jsonText = JSON.toJSONString(node, true);
System.out.println(jsonText);
}
4、输出结果
{
"attributes":{
"url":"1"
},
"checked":false,
"children":[{
"attributes":{
"url":"2"
},
"checked":false,
"children":[
{
"attributes":{
"url":"3"
},
"checked":false,
"children":[],
"id":"3",
"parentid":"2",
"text":"长沙"
},
{
"attributes":{
"url":"4"
},
"checked":false,
"children":[],
"id":"4",
"parentid":"2",
"text":"株洲"
},
{
"attributes":{
"url":"5"
},
"checked":false,
"children":[],
"id":"5",
"parentid":"2",
"text":"湘潭"
},
{
"attributes":{
"url":"6"
},
"checked":false,
"children":[
{
"attributes":{
"url":"7"
},
"checked":false,
"children":[],
"id":"7",
"parentid":"6",
"text":"岳阳县"
},
{
"attributes":{
"url":"http://192.168.1.1"
},
"checked":false,
"children":[],
"id":"8",
"parentid":"6",
"text":"华容县"
},
{
"attributes":{
"url":"9"
},
"checked":false,
"children":[],
"id":"9",
"parentid":"6",
"text":"湘阴县"
}
],
"id":"6",
"parentid":"2",
"text":"岳阳"
}
],
"id":"2",
"parentid":"1",
"text":"湖南"
}],
"id":"1",
"parentid":"0",
"text":"中国"
}

写得很简单代码不是很规范,只是个测试demo。但功能很实用,希望对做web开发同学有些帮助。
从数据库读取数据并动态生成easyui tree构结的更多相关文章
- .Net Mvc 返回Json,动态生成EasyUI Tree
最近做一个项目,开始接触EasyUI,感觉很强大,很适合我这种对前台不是很感冒的人.在学习Tree的过程中,感觉网上的资料挺乱的,很多只是把EasyUI API 抄了一遍.现在把最近这段时间的学到的, ...
- 10天学会phpWeChat——第三天:从数据库读取数据到视图
在第二天,我们创建了我们的第一个phpWeChat功能模块,但是比较简单.实际生产环境中,我们不可能有如此简单的需求.更多的情况是数据存储在MySql数据库中,我们开发功能模块的作用就是将这些数据从M ...
- echarts通过ajax向服务器发送post请求,servlet从数据库读取数据并返回前端
1.echarts的官网上的demo,都是直接写死的随机数据,没有和数据库的交互,所以就自己写了一下,ok,我们开始一步一步走一遍整个流程吧. 就以官网最简单的那个小demo来做修改吧.官网上的小de ...
- C#实现从数据库读取数据到Excel
用第三方组件:NPOI来实现 先去官网:http://npoi.codeplex.com/下载需要引入dll(可以选择.net2.0或者.net4.0的dll),然后在网站中添加引用.使用 NPOI ...
- JMeter 参数化之利用JDBCConnectionConfiguration从数据库读取数据并关联变量
参数化之利用DBC Connection Configuration从数据库读取数据并关联变量 by:授客 QQ:1033553122 1. 下载mysql jar包 下载mysql jar包 ...
- Android打开数据库读取数据
打开数据库读取数据 private MyDatabaseHelper dbHelper; dbHelper=new MyDatabaseHelper(this,"List.db", ...
- .Net MVC 动态生成LayUI tree
.Net MVC 动态生成LayUI tree 最近在做项目的过程中需要用到Tree插件,所以找了一堆Tree发现LayUI的Tree样式比较好看,所以开始搞! 1.Layui部分 1.1 首先引用文 ...
- silverlight 从数据库获取到数据,动态生成XMLWEN文件,并获取文件进行操作
// Silverlight中的独立存储是其内部的可信任的可访问文件空间,在这里你可以使用Silverlight随意的创建.读取.写入.删除目录和文件,它有一些类似于Cookie,但是它可以在客户端保 ...
- Hibernate上传数据到数据库,从数据库读取数据到本地模板代码
1.Hibernate上传数据到数据库: //创建一个session对象 Session session1=HibernateTools.getSession(); //Fenciresult数据库表 ...
随机推荐
- OJ题:句子逆转
将一个英文语句以单词为单位逆序排放.例如"I am a boy",逆序排放后为"boy a am I"所有单词之间用一个空格隔开,语句中除了英文字母外,不再包含 ...
- Leetcode_8_String to Integer
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41521063 Implement atoi to conv ...
- 【一天一道LeetCode】#55. Jump Game
一天一道LeetCode系列 (一)题目 Given an array of non-negative integers, you are initially positioned at the fi ...
- jsp中的路径与跳转
在jsp(serlvet)中,页面的"变换"有两种方式,第一重定向,第二转发: 先说重定向,它的调用方式是这样的 response.sendredirect("uri&q ...
- Leetcode_136_Single Number
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42713315 Given an array of inte ...
- SpriteBuilder添加的TrueType字体未显示在log中的原因分析
按照书上的说法,在SpriteBuilder中添加的TrueType字体名称会在枚举字体方法显示的log中出现.但是运行程序后没有在log中发现对应的字体名称. 因为该字体是例子中作者制作的,所以字体 ...
- centos 下安装mysql-5.6.11
这次是在centos6.4下安装mysql,在安装之前,你要先确定你的linux已经安装了这些包: wget, gcc-c++, ncurses-devel ,cmake, make ,perl 如果 ...
- PS图层混合算法之二(线性加深,线性减淡,变亮,变暗)
线性加深模式: 查看每个通道的颜色信息,通过降低"亮度"使底色的颜色变暗来反映绘图色,和白色混合没变化. Linear Burn 线形加深 C=A+B-1 如果上下层的像素值之和小 ...
- ActiveX数据对象之事务控制在VB和DELPHI中的应用
本文发表在中国人民解放军"信息工程大学"学报 2001年第3期. ActiveX数据对象之事务控制在VB和DELPHI中的应用 ...
- HI258摄像头旋转配置问题
{0x28, 0x04}, //Full row start y-flip {0x29, 0x01}, //Pre1 row start no-flip {0x2a, 0x02}, //Pre1 r ...