EasyUI树目录结构

来源:互联网 发布:mysql 查询 锁定的表 编辑:程序博客网 时间:2024/06/13 05:18

EasyUI树目录结构,通过后台返回的json数据resualt,通过eval()解析成对象给treeData
从而实现动态数据:下面是treeData 数据默认的数据格式:
//动态菜单数据

var treeData = [{                text :  "菜单",                children :  [{                            text :  "一级菜单1",                            attributes :  {                                    url :  ""                            }                    },     {                            text :  "一级菜单2",                            attributes :  {                                    url :  ""                            }                    },     {                            text :  "一级菜单3",                            state :  "closed",                            children :  [{                                        text :  "二级菜单1",                                        attributes :  {                                                url :  ""                                        }                                },         {                                        text :  "二级菜单2",                                        attributes :  {                                                url :  ""                                        }                                },         {                                        text :  "二级菜单3",                                        attributes :  {                                                url :  ""                                        }                                }                    ]                    }            ]        }    ];

1、效果:
这里写图片描述
2、Js实现代码:

$.post(url,function(resualt){    //动态菜单数据    var treeData =eval("("+resualt+")");    //实例化树形菜单    $("#tree").tree({        data : treeData,        lines : true,        onClick : function (node) {            if (node.attributes) {                Open(node.text, node.attributes.url);            }        }    });  //在右边center区域打开菜单,新增tab    function Open(text, url) {    //这里将url存放来问类型代码        getFwInfo(text,url);    }//-----post end     //通过传递的参数访和Url访问后台并返回json数据    function getFwInfo(year,lwlxmc){}

3、Html代码:
在页面中添加ul标签, id=”tree”

<ul id="tree"></ul>

4、java实现
思路分析:从treeData 数据

顶级目录以及子目录格式为:

 {                            text :  "一级菜单3",                            state :  "closed",                            children :  [{                                        text :  "二级菜单1",                                        attributes :  {                                                url :  ""                                        }                                },         {                                        text :  "二级菜单2",                                        attributes :  {                                                url :  ""                                        }                                },         {                                        text :  "二级菜单3",                                        attributes :  {                                                url :  ""                                        }                                }                    ]                    } 

从上面可以看出:顶级目录的属性:text 、state 、children 。其中children 是设置子目录的,子目录包含text 、attributes 、url 其中url是attributes 属性节点,所以根据这个格式编写java:
首先创建bean
创建顶级目录bean– Parent.java

package com.bean.guiGangMenu;import java.util.ArrayList;public class Parent {    private String text;    private ArrayList<Children> children;    public String getText() {        return text;    }    public void setText(String text) {        this.text = text;    }    public ArrayList<Children> getChildren() {        return children;    }    public void setChildren(ArrayList<Children> children) {        this.children = children;    }}

创建子目录bean:

package com.bean.guiGangMenu;public class Children {    private Url attributes;    private String text;    public String getText() {        return text;    }    public void setText(String text) {        this.text = text;    }    public Url getAttributes() {        return attributes;    }    public void setAttributes(Url attributes) {        this.attributes = attributes;    }}

创建attributes属性bean

package com.bean.guiGangMenu;public class Url {    private String url;    public String getUrl() {        return url;    }    public void setUrl(String url) {        this.url = url;    }}

测试数据集合

String y="2014年";        SimpleDateFormat format=new SimpleDateFormat("yyyy年");        Date d=new Date();        System.out.println(format.format(d));        //设置菜单集合父菜单        ArrayList<Parent> parent=new ArrayList<Parent>();        Parent prnt=new Parent();        prnt.setText("父菜单");        //设置子菜单集合        ArrayList<Children> children=new ArrayList<Children>();        Children child=new Children();        //设置子菜单名称        child.setText("子菜单一");        Url rl=new Url();        rl.setUrl("http://www.baidu.com");        //设置子菜单Url        child.setAttributes(rl);        //添加到子菜单集合中        children.add(child);        Children child2=new Children();        child2.setText("子菜单二");        child2.setAttributes(rl);        children.add(child2);        //父菜单添加子菜单        prnt.setChildren(children);        //添加到父菜单集合中        parent.add(prnt);

输出数据:

[{"text":"父菜单","children":[{"text":"子菜单一","attributes":{"url":"http://www.baidu.com"}},{"text":"子菜单二","attributes":{"url":"http://www.baidu.com"}}]}]

正式调用:

@RequestMapping("/getMenuClassifyInfo_sw")    public void getMenuClassifyInfo_sw(RcbgSwdjZbModel model,String type, String number,            HttpServletRequest request, HttpServletResponse response) throws Exception{        SimpleDateFormat format=new SimpleDateFormat("yyyy年");        response.setCharacterEncoding("utf-8");        PrintWriter writer = response.getWriter();        List<RcbgSwdjZb> getFwcx = get_swcx(model,response,request);        //设置菜单集合父菜单        ArrayList<Parent> parent=new ArrayList<Parent>();        //国家局菜单集合        HashSet countrySet=new HashSet();        //省部委        HashSet provinceSet=new HashSet();        //无文号        HashSet noNumSet=new HashSet();        for(RcbgSwdjZb fwzb:getFwcx){            if(fwzb.getLwlx()!=null){                Parent prnt=new Parent();                if(fwzb.getLwlx().equals("0001")){// prnt.setText("收文库(国家局)");                    if(fwzb.getSwrq()!=null){                        String year=format.format(fwzb.getSwrq());                        countrySet.add(year);                    }                }                if(fwzb.getLwlx().equals("0002")){ // prnt.setText("收文库(省、部、委)");                    if(fwzb.getSwrq()!=null){                        String year=format.format(fwzb.getSwrq());                        provinceSet.add(year);                    }                }                if(fwzb.getLwlx().equals("0003")){ //prnt.setText("无文号登记");                    if(fwzb.getSwrq()!=null){                        String year=format.format(fwzb.getSwrq());                        noNumSet.add(year);                    }                }            }        }        //HashSet set=new HashSet();        for(int i=0;i<3;i++){            Parent prnt=new Parent();            Iterator<String> itor=null;            if(i==0){                prnt.setText("收文库(国家局)");                itor=countrySet.iterator();            }            if(i==1){                prnt.setText("收文库(省、部、委)");                itor=provinceSet.iterator();            }            if(i==2){                prnt.setText("无文号登记");                itor=noNumSet.iterator();            }            if(itor.hasNext()==false){                ArrayList<Children> children=new ArrayList<Children>();                    Children child=new Children();                    //设置子菜单名称                    child.setText("无");                    Url rl=new Url();                    rl.setUrl("http://www.baidu.com");                    //设置子菜单Url                    child.setAttributes(rl);                    //添加到子菜单集合中                    children.add(child);                //父菜单添加子菜单                prnt.setChildren(children);                //添加到父菜单集合中                parent.add(prnt);            }else{            ArrayList<Children> children=new ArrayList<Children>();            for(Iterator<String> it=itor;it.hasNext();){                String menu=it.next();                Children child=new Children();                //设置子菜单名称                child.setText(menu);                Url rl=new Url();                if(i==0)                    rl.setUrl("0001");                if(i==1)                    rl.setUrl("0002");                if(i==2)                    rl.setUrl("0003");                //设置子菜单Url                child.setAttributes(rl);                //添加到子菜单集合中                children.add(child);            }            //父菜单添加子菜单            prnt.setChildren(children);            //添加到父菜单集合中            parent.add(prnt);            }        }       String menu=JSONUtil.toJSONString(parent);       writer.write(menu);        writer.close();5、  }

6、最终就是刚开始看到的树目录:
这里写图片描述

0 0
原创粉丝点击