关闭

easyui 中 的同步树(1)

标签: easyuitreejsonhibernate
598人阅读 评论(0) 收藏 举报
分类:

1.先看一下easyUI,Tree组件要求的数据格式

[{
    "id":1,
    "text":"Folder1",
    "iconCls":"icon-save",
    "children":[{
        "text":"File1",
        "checked":true
    },{
        "text":"Books",
        "state":"open",
        "attributes":{
            "url":"/demo/book/abc",
            "price":100
        },
        "children":[{
            "text":"PhotoShop",
            "checked":true
        },{
            "id": 8,
            "text":"Sub Bookds",
            "state":"closed"
        }]
    }]
},{
    "text":"Languages",
    "state":"closed",
    "children":[{
        "text":"Java"
    },{
        "text":"C#"
    }]
}]

2.我以 学院,系,专业 的顺序在前台显示tree
下面是数据库表:
学院表:
这里写图片描述
系表:
这里写图片描述
专业表:
这里写图片描述

3.看下数据库中的实体

@Entity
@Table(name = "t_department", catalog = "xiaonei")
public class TDepartment implements java.io.Serializable {

    // Fields

    private Integer id;
    private String text;
    private Integer pid;
    private List<TMajor> children;

    public List<TMajor> getChildren() {
        return children;
    }

    public void setChildren(List<TMajor> children) {
        this.children = children;
    }
    // Constructors

    /** default constructor */
    public TDepartment() {
    }

    /** full constructor */
    public TDepartment(String text, Integer pid) {
        this.text = text;
        this.pid = pid;
    }

    // Property accessors
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Column(name = "text", length = 36)
    public String getText() {
        return this.text;
    }

    public void setText(String text) {
        this.text = text;
    }

    @Column(name = "pid")
    public Integer getPid() {
        return this.pid;
    }

    public void setPid(Integer pid) {
        this.pid = pid;
    }

}

@Entity
@Table(name = "t_major", catalog = "xiaonei")
public class TMajor implements java.io.Serializable {

    // Fields

    private Integer id;
    private String text;
    private Integer pid;

    // Constructors

    /** default constructor */
    public TMajor() {
    }

    /** full constructor */
    public TMajor(String text, Integer pid) {
        this.text = text;
        this.pid = pid;
    }

    // Property accessors
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Column(name = "text", length = 36)
    public String getText() {
        return this.text;
    }

    public void setText(String text) {
        this.text = text;
    }

    @Column(name = "pid")
    public Integer getPid() {
        return this.pid;
    }

    public void setPid(Integer pid) {
        this.pid = pid;
    }

}

@Entity
@Table(name = "t_academy", catalog = "xiaonei")
public class TAcademy implements java.io.Serializable {

    // Fields

    private Integer id;
    private String text;
    private List<TDepartment> children;


    // Constructors

    @Transient
    public List<TDepartment> getChildren() {
        return children;
    }

    public void setChildren(List<TDepartment> children) {
        this.children = children;
    }

    /** default constructor */
    public TAcademy() {
    }

    /** full constructor */
    public TAcademy(String text) {
        this.text = text;
    }

    // Property accessors
    @Id
    @GeneratedValue(strategy = IDENTITY)
    @Column(name = "id", unique = true, nullable = false)
    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Column(name = "text", length = 36)
    public String getText() {
        return this.text;
    }

    public void setText(String text) {
        this.text = text;
    }

}

4.看下后台组装easyui json格式(java),dao层和service层就不贴了

@RequestMapping("/getAllTree")
    public String getAllTree(HttpServletRequest request,HttpServletResponse response) throws Exception{
        List academyList = academyService.getAllAcademy();
        Iterator academyIt = academyList.iterator();
        /*
         * 这太复杂了,没法看啊---
         * */
        while(academyIt.hasNext()){

            TAcademy academy = (TAcademy) academyIt.next();

            List departmentList = departmentService.getAllDepartmentByAcademyId(academy.getId());

            Iterator departmentIt = departmentList.iterator();

            List addDepartmentList = new ArrayList();

            while(departmentIt.hasNext()){

                TDepartment department = (TDepartment) departmentIt.next();

                addDepartmentList.add(department);

                List majorList = majorService.getAllMajorByDepartmentId(department.getId());

                Iterator majorIt = majorList.iterator();

                List addMajorList = new ArrayList();

                while(majorIt.hasNext()){

                    TMajor major = (TMajor) majorIt.next();

                    addMajorList.add(major);
                }
                department.setChildren(addMajorList);
            }
            academy.setChildren(addDepartmentList);
        }
        JSONArray jn = JSONArray.fromObject(academyList);
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().println(jn.toString());
        return null;
    }
}

5.看下前台

<body style="margin:15px;">
<script>
    $("#majorTree").tree({    
        url:"/xiaoneinew/tree/getAllTree"
    }); 
</script>
<ul id="majorTree"></ul>
</body>

6.效果图
这里写图片描述
这里写图片描述
7.注意

  • 在实体department和academy中,有关children字段我用了@Transient,就是不同数据库中同步,是hibernate中三种状态的一种。
  • 这是我第一次写easyui的tree,写的很不好,以后我会优化的。
0
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:103584次
    • 积分:2544
    • 等级:
    • 排名:第16668名
    • 原创:145篇
    • 转载:53篇
    • 译文:0篇
    • 评论:13条
    博主
    博客专栏