ExtJS--根据用户权限动态创建菜单树

服务器端方法

public File writeJason(){
final List<ADAuthority> allMenus = securityManager.getUserMenuTree(); //根据用户ID从数据库读出用户的所有权限
final List<ADAuthority> leafs = new LinkedList<ADAuthority>(allMenus);//有顺序的list
List<ADAuthority> roots = new LinkedList<ADAuthority>();//把所有根节点取出,这里的根不是Ext.tree.TreePanel里的root而是它的一级子节点
for (ADAuthority menu : allMenus) {
if (menu.getLevel() == 1) {
roots.add(menu);
leafs.remove(menu);
}
}
List<Map<ADAuthority, List>> results = new LinkedList();
for(ADAuthority root : roots){
Map<ADAuthority,List> m = getDeepChildren(root, leafs);
results.add(m);
}
String s = Map2Json(results);
//将s写入服务器端.json文件
}


/*
* 递归方法
* 传一个根节点和其他所有节点的集合
* 根据根结点遍历其下的所有子结点
*/
public static Map<ADAuthority,List> getDeepChildren(ADAuthority root, List<ADAuthority> leafs){
Map<ADAuthority,List> singleTree = new HashMap<ADAuthority,List>();//用来存放根和其下所有的子
List<Map<ADAuthority,List>> singleLeafs = new LinkedList<Map<ADAuthority,List>>();
List<ADAuthority> copyLeafs = new LinkedList<ADAuthority>(leafs);
for(ADAuthority leaf : leafs){
if (leaf.getParentRrn() == null){
copyLeafs.remove(leaf);
continue;
} else if(leaf.getParentRrn().equals(root.getObjectRrn())){
copyLeafs.remove(leaf);
singleLeafs.add(getDeepChildren(leaf,copyLeafs));
}
}
singleTree.put(root, singleLeafs);
return singleTree;
}


/*
* 递归方法
* 将生成的特定List转成json格式数据
*/
public static String Map2Json(List<Map<ADAuthority, List>> source){
StringBuffer sb = new StringBuffer();
int time = 0;
for(Map<ADAuthority, List> tree : source){
for(ADAuthority key : tree.keySet()){
List<Map<ADAuthority,List>> l = tree.get(key);
if(time++ > 0) sb.append(",");//json的格式,用","分隔每一组{},最后面的一组没有","
sb.append("{'text': '" + key.getLabel_zh() + "',");
sb.append("'cls': 'floder'");
if(l.size() != 0){
sb.append(",'children': [" + Map2Json(l) + "]");
}else{
sb.append(",'leaf': true");
}
sb.append("}");
}
}
return sb.toString();
}


menutree.html内容

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!--引入ext样式表-->
<link rel="stylesheet" type="text/css" href="../extjs/resources/css/ext-all.css" />
<!--引入ext适配器脚本-->
<script type="text/javascript" src="../extjs/adapter/ext/ext-base.js"></script>
<!--引入ext框架脚本-->
<script type="text/javascript" src="../extjs/ext-all-debug.js"></script>
<script type="text/javascript" src="../js/tree.js"></script>
<title>Tree of ExtJS</title>
</head>
<body>
<div id="menu"></div>
</body>
</html>


menutree.js内容

Ext.onReady(function(){
var tree = new Ext.tree.TreePanel({
renderTo: 'menu',
useArrows: true,
autoScroll: true,
animate: true,
enableDD: true,
containerScroll: true,
border: false,
root: {
text:'Function List',
nodeType: 'async',
draggable: false,
id: 'source'
},
rootVisible:true,

// auto create TreeLoader
dataUrl: '../js/nodes.json'
});

tree.getRootNode().expand(true);
});

效果图:
[img]http://dl.iteye.com/upload/picture/pic/51746/fc28707f-2979-33cb-a297-5b98b6698cc5.gif[/img]


[b][size=medium][i][align=center][color=red][img]http://img.tongji.linezing.com/729225/tongji.gif[/img]希望踩的朋友也能留下宝贵批评意见,也能使我从中有所收获不断进步,不甚感激![/color][/align][/i][/size][/b]

[table]|[url="http://s.click.taobao.com/t_1?i=qX1eVuxw6cVJpA%3D%3D&p=mm_14390488_0_0&n=11"][img]http://image.taobao.com/bao/uploaded/http://img05.taobaocdn.com/bao/uploaded/i5/T1q4BpXl8gXXaNzWM3_045807.jpg_sum.jpg[/img]震撼手感 镭射防伪 Levi's钱包 李维斯钱包 软牛皮长款钱包[/url][/table][table]|[url="http://s.click.taobao.com/t_1?i=qX98H4zYjXIhAQ%3D%3D&p=mm_14390488_0_0&n=11"][img]http://image.taobao.com/bao/uploaded/http://img08.taobaocdn.com/bao/uploaded/i8/T1SylrXi4bXXaax._a_092611.jpg_sum.jpg[/img]09新款¥Levi's李维斯牛皮钱包/卡包/男士钱包 情侣款 新年礼物[/url][/table][table]|[url="http://s.click.taobao.com/t_1?i=qz75YSmrgO376A%3D%3D&p=mm_14390488_0_0&n=12"][img]http://image.taobao.com/bao/uploaded/http://img06.taobaocdn.com/bao/uploaded/i6/T1S3tkXi37zJPz3QzX_085801.jpg_sum.jpg[/img][/url][url="http://s.click.taobao.com/t_1?i=qz75YSmrgO376A%3D%3D&p=mm_14390488_0_0&n=12"]双冠信誉-诺基亚5800手机-触摸屏手机-大陆行货-全国联保[/url][b]2299.0元[/b][url="http://s.click.taobao.com/t_1?i=qz75YSmrgO376A%3D%3D&p=mm_14390488_0_0&n=12"][img]http://img.alimama.cn/images/tbk/cps/fgetccode_btn.gif[/img][/url][/table]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值