Programming is an art form.

Good at java web & Interested in deep Learning…(https://javaduqing.github.io)

easyui 中 的同步树(1)

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,写的很不好,以后我会优化的。
阅读更多
版权声明:本文为博主原创文章,转载注明出处。 https://blog.csdn.net/disiwei1012/article/details/51539149
个人分类: js/jquery/easyui
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

easyui 中 的同步树(1)

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭