木子Maple先森

博客园 首页 联系 订阅 管理

将同级别的数据转化为不同树级别的easyUI数据 1. serviceimp层业务的实现 2.controller控制转换 3.jsp通过json接收
1 serviceimp层业务的实现
@Override
public List<TreeNode> getTreeNodesFromMenuList (List<TPermission> menus) {
List<TPermission> menuOne = new ArrayList<TPermission>();
//处理一级菜单
for (TPermission tp : menus) {
if (tp.getPid() == 0) {
menuOne.add(tp);
}
}

//处理二级菜单
for (TPermission tp : menus) {
int pid = tp.getPid();
TPermission tpp = null;
if (pid != 0) {
for (TPermission tp1 : menuOne) {
if (tp1.getId() == pid) {
tpp = tp1;
break;
}
}
tpp.getChildren().add(tp);
}
}
Collections.sort(menuOne);
for (TPermission tp : menuOne) {
Collections.sort(tp.getChildren());
}

List<TreeNode> treeNodes = transfer(menuOne);

return treeNodes;
}

private List<TreeNode> transfer(List<TPermission> tp) {
List<TreeNode> treeNodes = new ArrayList<TreeNode>();
TreeNode tn = null;
for (TPermission tp1 : tp) {
tn = new TreeNode();
tn.setId(tp1.getId());
tn.setText(tp1.getName());
tn.getAttributes().put("url", tp1.getUrl());
if (tp1.getChildren().size() != 0) {
tn.setState("closed");
tn.setChildren(transfer(tp1.getChildren()));
} else {
tn.setState("open");
}
treeNodes.add(tn);
}
return treeNodes;
}

}

2 controller层控制转换和与jsp交互
@RequestMapping(value = "getMenu", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public List<TreeNode> getUserMenu() {
List<TreeNode> treeNodes = null;
// 获取到当前主体
Subject subject = SecurityUtils.getSubject();
// 由当前主体获取到其首要身份信息
SysUser user = (SysUser) subject.getPrincipals().getPrimaryPrincipal();
// 获取该用户的菜单
List<TPermission> menuList = user.getMenus();
// 将Tpermission的集合转换为TreeNode的集合,返回
treeNodes = menuService.getTreeNodesFromMenuList(menuList);
return treeNodes;
}

 

3 .前段接收时json
<script>
$(function(){
//当页面加载完毕之后,初始化树
$('#myTree').tree({
url:'<%=basePath%>getMenu.do',
onClick:function(node){
alert(node.attributes.url);
if ($('#myTree').tree('isLeaf',node.target)) {
$("#myTabs").tabs('add',{
title:node.text,
href:"<%=basePath%>" + node.attributes.url,
closable:true,
iconCls:'icon-ok'
});
}/* else {
alert("父节点:" + node.text);
} */
}
});
});
</script>

posted on 2016-11-18 16:42  木子Maple先森  阅读(99)  评论(0编辑  收藏  举报