后台数据组装
/**
* 查询权限列表
* @param categoryId
* @return
*/
@Override
public List<Map<String, Object>> getMenus(String categoryId) {
List<Map<String, Object>>menuallist= userDao.selectMenuAllList();
List<String> meunSelectedList = null;
if (StringUtils.isNotBlank(categoryId)) {
//查询已有权限
meunSelectedList = userDao.selectMenuSelectedList(categoryId);
}
//获取树状结构
List<Map<String, Object>>listTree=getTreeList(menuallist,meunSelectedList,"id","pre_id","children");
return listTree;
}
public List<Map<String,Object>> getTreeList(List<Map<String, Object>>menuaList,List<String> idlist,String id,String pre_id,String children) {
System.out.println("获取树状结构开始》》》》》》》》》》》》》》》》》》》");
/**
* 给获取数组的每一项添加一个子项
*/
for(int i=0;i<menuaList.size();i++) {//checked
menuaList.get(i).put("checked","");
menuaList.get(i).put(children, new ArrayList<Map<String, Object>>());
}
System.out.println("menuaList=="+menuaList);
List<Map<String,Object>>treeList=new ArrayList<Map<String,Object>>();
for(Map<String,Object>outMap:menuaList) {
for(Map<String,Object>inMap:menuaList) {
String outid=outMap.get(id).toString();
String inPre_id=inMap.get(pre_id).toString();
if(outid.equals(inPre_id)) {
List<Map<String,Object>> listMap=(ArrayList<Map<String,Object>>)outMap.get(children);
listMap.add(inMap);
String inId=inMap.get("id").toString();
//判断查询获取的id和内循环id是否相同 如果相同给checked赋值为true
if(null!=idlist) {
if(isFaseOrTrue(inId,idlist)) {
inMap.put("checked", "true");
}
}
}
}
String outPid=outMap.get(pre_id).toString();
if(StringUtils.isAllBlank(outPid)) {
treeList.add(outMap);
String outId=outMap.get(id).toString();
if(null!=idlist) {
if(isFaseOrTrue(outId,idlist)) {
outMap.put("checked", "true");
}
}
}
}
return treeList;
}
//判断表达式
private Boolean isFaseOrTrue(String id,List<String>list) {
Boolean flag=false;
for(String str:list) {
if(id.equals(str)) {
flag=true;
break;
}
}
return flag;
}
前端代码
<div id="container"
style="width: 12%; height: 800px;display:inline-block; float: left !important;">
<div class="easyui-panel" title="权限列表"
style="width: 100%; height: 99%; float: right !important; overflow-y: scroll; background-color: #fff">
<ul>
<input type="checkbox" id="tree_select" name="tree_select"
onclick="allselect()" />
<a>全选或者取消全选</a>
</ul>
<ul id="dgMenu" class="easyui-tree" style="width: 200px"></ul>
</div>
</div>
<script type="text/javascript">
//全选或者取消全选
function allselect(){
var tree_select = $(‘#tree_select‘).is(‘:checked‘);
var roots = $(‘#dgMenu‘).tree(‘getRoots‘);//返回tree的所有根节点数组
console.log(tree_select);
if (tree_select==true) {
for ( var i = 0; i < roots.length; i++) {
var node = $(‘#dgMenu‘).tree(‘find‘, roots[i].id);//查找节点
$(‘#dgMenu‘).tree(‘check‘, node.target);//将得到的节点选中
}
} else {
for ( var i = 0; i < roots.length; i++) {
var node = $(‘#dgMenu‘).tree(‘find‘, roots[i].id);
$(‘#dgMenu‘).tree(‘uncheck‘, node.target);
}
}
}
$(‘#dgMenu‘).tree({
checkbox: ‘checked‘,
cascadeCheck: true,
url: ‘<%=basePath%>user/getMenu.do‘,
loadFilter: function(data){
console.log(data);
return data;
},
/* onBeforeLoad:function(node,param){
param.categoryId = "13";
} */
});
$("#allotControltable").datagrid({
//var row = $(‘#xyData_userListlk‘).datagrid(‘getSelected‘);
onClickRow: function (index, row) {
var categoryId=row.tm_SMP;//用户类别ID
//alert("查询已经有的权限======"+categoryId);
$(‘#dgMenu‘).tree({
checkbox: true,
cascadeCheck: true,
url: ‘<%=basePath%>user/getMenu.do?categoryId=‘+categoryId,
loadFilter: function(data){
return data;
}
});
}
});
2.后台数据组装
/**
* 查询权限
*/
@Override
public List<Map<String, Object>> findMenuList(String remark) {
System.out.println("findMenuList-remark=="+remark);
List<Map<String, Object>>list =loadMenuDao.findMenuList(remark);
list=getTreeList("id","pid","children",list);
System.out.println("treelist======"+list);
return list;
}
//把带有父子机构的数据变成treelist形式
public static List<Map<String, Object>> getTreeList(String id,String pid,String children,List<Map<String, Object>>list) {
//List<Map<String, Object>>listmap=new ArrayList<Map<String, Object>>();
//给list的每一项加一个子项
for(int i=0;i<list.size();i++) {
list.get(i).put(children, new ArrayList<Map<String, Object>>());
}
List<Map<String, Object>>treelist=new ArrayList<Map<String, Object>>();
for(Map<String, Object> outmap:list) {
for(Map<String, Object> inmap:list) {
String outid=outmap.get(id).toString();
String inpid=inmap.get(pid).toString();
if(outid.equals(inpid)) {
List<Map<String, Object>>maplist=(ArrayList) outmap.get(children);
maplist.add(inmap);
}
}
String prid=outmap.get(pid).toString();
if(StringUtils.isBlank(prid)) {
treelist.add(outmap);
}
}
return treelist;
}
原文地址:https://www.cnblogs.com/xianz666/p/12004691.html