最近需要用到EASYUI中的TREE功能,以前我是直接拼接成<UL><LI>发现这样拼完之后在更改树后对树的刷新不是很理想,现改用JSON格式,首先分析TREE中JOSN格式如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[{
    "id":1,
    "text":"流程分类",
    "children":[{
        "id":11,
        "text":"门禁流程分类",
        "checked":true
    },{
        "id":113,
        "text":"子门禁流程分类",
        "children":[{
            "id":1131,
            "text":"子子门禁流程分类"
        },{
            "id": 8,
            "text":"Async Folder",
            "state":"closed"
        }]
    }]
},{
    "id":3
    "text":"行政",
    "children":[{
        "id":"31",
        "text":"加班"
    },{
        "id":"33",
        "text":"请假"
    }]
}]

可以看出这种模式是由三个属性所组成,ID TEXT 集合,根据分析 我们需要对此模式建立一个BEAN的结构模型,建立TREENODE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
packagecom.odbpo.beans;
 
importjava.util.List;
 
publicclassTreeNode {
 
    privateintid;
 
    privateString text;
     
    privateintpid;
 
    privateList<TreeNode> children;
 
    publicintgetPid() {
        returnpid;
    }
 
    publicvoidsetPid(intpid) {
        this.pid = pid;
    }
 
    publicintgetId() {
        returnid;
    }
 
    publicvoidsetId(intid) {
        this.id = id;
    }
 
    publicString getText() {
        returntext;
    }
 
    publicvoidsetText(String text) {
        this.text = text;
    }
 
    publicList<TreeNode> getChildren() {
        returnchildren;
    }
 
    publicvoidsetChildren(List<TreeNode> children) {
        this.children = children;
    }
}

BEAN构建完成,那么接下来分析如何往BEAN里传数据,首先分析 数据库表中结构

1
2
3
4
5
createtabledepatment(
id,--当前ID
pid,--父ID
name--显示名称
)

接下来我们要建立一个COM.UTIL包,所递归方法放置在这个包下,以便后续多次调用方便

建立类名为:JSONFACTORY

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
     * 以对象形式传回前台
     */
    publicstaticList<TreeNode> buildtree(List<TreeNode> nodes,intid){
        List<TreeNode> treeNodes=newArrayList<TreeNode>();
        for(TreeNode treeNode : nodes) {
            TreeNode node=newTreeNode();
            node.setId(treeNode.getId());
            node.setText(treeNode.getText());
            if(id==treeNode.getPid()){
                node.setChildren(buildtree(nodes, node.getId()));
                treeNodes.add(node);
            }
             
        }
        returntreeNodes;
    }
    

完成以上工作后我们就要在控制器中使用在DAO中建立好的查询方法,这里DAO中写法就不细说了;

控制器写法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@RequestMapping("/flow_tree")
@ResponseBody
publicList<TreeNode> getTree(){
    List<TreeNode> nodes=newArrayList<TreeNode>();
    List<FlowSortTable> list_all=flowSortTableServiceImpl.findAll();
    for(FlowSortTable flowSortTable : list_all) {
        TreeNode treeNode=newTreeNode();
        treeNode.setId(flowSortTable.getSortId());
        treeNode.setPid(flowSortTable.getSortPartmentId());
        treeNode.setText(flowSortTable.getSortName());
        nodes.add(treeNode);
    }
    List<TreeNode> treeNodes=JsonTreeFactory.buildtree(nodes,0);
    returntreeNodes;
}

以上工作结束,我们就可以在前台使用EASYUITREE模式了

将此代码 放在$(document).ready(function(){})中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$("#tt1").tree({
            url:'${contextPath}/main/flow/flow_tree.html',
            onClick:function(node){
                $("#sort").css("display","block");
                $("#save").hide();
                $("#update").show();
                odbpo_combobox("#flowType",'${contextPath}/main/flow/flowSelect.html',"flowId","flowName");
                varpnode=$(this).tree('getParent',node.target);
                $("#flowType").combobox('setValue', pnode.id);
                $("#node_id").val(node.id);
                $("#node_text").val(node.text);
                console.debug(node.id);
                console.debug(node.text);          
            }
        })

HTML构建:

1
2
3
<ulid="tt1">
             
        </ul>

启动TOMCAT预览就可以看到一个树形图的效果了!

关注公众号,分享干货,讨论技术

EasyUI Tree递归方式获取JSON的更多相关文章

  1. MVC4中EasyUI Tree异步加载JSON数据生成树

      1,首先构造tree接受的格式化数据结构MODEL /// <summary> /// 定义EasyUI树的相关数据,方便控制器生成Json数据进行传递 /// </summar ...

  2. 用递归方式在JSON中查找对象

    Json文件例子: { "type": "Update", "data": { "temp": "v" ...

  3. 树状sql--采用递归方式获取节点

    创建数据库 create table City(id varchar(3) primary key , pid varchar(3) , name varchar(10)) 插入数据 insert i ...

  4. jQuery AJAX获取JSON数据解析多种方式示例

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  5. J2EE Web开发入门—通过action是以传统方式返回JSON数据

    关键字:maven.m2eclipse.JSON.Struts2.Log4j2.tomcat.jdk7.Config Browser Plugin Created by Bob 20131031 l ...

  6. AngularJS学习笔记(3)——通过Ajax获取JSON数据

    通过Ajax获取JSON数据 以我之前写的与用户交互的动态清单列表为例,使用JSON前todo.html代码如下: <!DOCTYPE html> <html ng-app=&quo ...

  7. C++通过HTTP请求Get或Post方式请求Json数据(转)

    原文网址:https://www.cnblogs.com/shike8080/articles/6549339.html #pragma once#include <iostream>#i ...

  8. ajax获取json数据为undefined--原因解析

    解决办法:var dataObj=eval("("+data+")");//转换为json对象 问题: 1. 碰到一个问题ajax成功获取json数据后,取值显 ...

  9. [转]easyui tree 模仿ztree 使用扁平化加载json

    原文地址:http://my.oschina.net/acitiviti/blog/349377 参考文章:http://www.jeasyuicn.com/demo/treeloadfilter.h ...

随机推荐

  1. CP文件覆盖问题

    # \cp -r -a aaa/* /bbb[这次是完美的,没有提示按Y.传递了目录属性.没有略过目录]

  2. Thrift IDL使用方式

    I.背景 众所周知,Thrift是一个RPC的框架,其可用于不同语言之间的服务相互调用.比如最近接触到的一个运用环境: *前端使用Node.Js重构了部分我们的老旧代码(前后端未分离的SpringBo ...

  3. TCP系列31—窗口管理&流控—5、TCP流控与滑窗

    一.TCP流控 之前我们介绍过TCP是基于窗口的流量控制,在TCP的发送端会维持一个发送窗口,我们假设发送窗口的大小为N比特,网络环回时延为RTT,那么在网络状况良好没有发生拥塞的情况下,发送端每个R ...

  4. 原生javascript自定义input[type=checkbox]效果

    2018年6月27日  更新 能用css3,就不用js 用纯css3实现样式重写 <!DOCTYPE html> <html lang="en"> < ...

  5. python爬虫-使用xpath方法

    #coding=utf-8 import re from lxml import etree import requests response = requests.get("http:// ...

  6. tc:逼良为娼

    tc的学习原来是想着直接从用户态学习的,但是万万没想到哇,qdisc class两个概念直接把我给搞晕了,直接看代码吧 调用:tc qdisc add dev tap0 root handle 1: ...

  7. 【Maven】Snapshot和Release版本的区别

    Snapshot版本代表不稳定.尚处于开发中的版本,快照版本. Release版本则代表稳定的版本,发行版本. 什么时候用Snapshot版本? 依赖库中的jar正处于开发的阶段,会被经常被更新,这种 ...

  8. ASP.Net MVC+Ibaties架构

    1.配置Ibaties首先在DLL引用中添加Ibaties相关引用:IBatisNet.Common.dll;IBatisNet.Common.Logging.Log4Net.dll;IBatisNe ...

  9. RT-thread内核之内核对象模型

    RT-Thread的内核对象模型是一种非常有趣的面向对象实现方式.由于C语言更为面向系统底层,操作系统核心通常都是采用C语言和汇编语言混合编写而成.C语言作为一门高级计算机编程语言,一般被认为是一种面 ...

  10. vscode Variables Reference

    vscode Variables Reference 您可以在以下链接中找到该列表:https://code.visualstudio.com/docs/editor/variables-refere ...