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、最終就是剛開始看到的樹目錄: