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
- EasyUI树目录结构
- easyUI的目录结构
- 以目录形式存树结构,用json lib拼接,用easyui显示(例子)
- easyui前台树结构展示
- linux目录树结构
- Linux目录树结构
- Ubuntu_目录树结构
- 修改EasyUI的目录树的图标
- easyui更换结构树的默认图标
- easyui 生成 菜单结构树 02
- 打印 目录树,获知源码目录结构
- 基于easyui开发Web版Activiti流程定制器详解(一)——目录结构
- EasyUi---tree 导航目录
- Linux源代码目录树结构
- Linux源代码目录树结构
- Linux源代码目录树结构
- Linux源代码目录树结构
- Linux源代码目录树结构
- 自定义seekbar 背景色不显示/进度条不在中间的问题
- My97DatePicker时间控件使用
- HDU 5619 Jam's store(最小费用最大流-mcmf)
- java中的IO操作总结(一)
- SlidingMenu 源码解析
- EasyUI树目录结构
- [leetcode] 94. Binary Tree Inorder Traversal
- leetcode[8] String to Integer (atoi)
- 勾股定理一日一证连载61
- 设计模式-建造者模式
- jqGrid分页按钮不能用解决方法
- Android简单实现侧滑功能
- 窗体调用kindeditor,使用WebBrowser控件、把Kindeditor烦在bin/DeBug目录中
- ZooKeeper的安装与配置