Tree assembly

 

 

 

 

Assemble background data

/**
* 查询权限列表
* @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 = inMap.get INID ( "id") toString ();.
// determine the query to get the id and the inner loop is the same as if the same id assigned to checked to true
IF (! = null idlist) {
IF (isFaseOrTrue (INID, IDList)) {
inMap.put ( "the checked", "to true");
}
}
}
}
String outPid = outMap.get (pre_id) .toString ();
IF (StringUtils.isAllBlank (outPid) ) {
treeList.add (outMap);
String outId = outMap.get (ID) .toString ();
! IF (IDList = null) {
IF (isFaseOrTrue (outId, IDList)) {
outMap.put ( "the checked", " to true ");
}
}
}
}
return treeList;

}
// Analyzing expression
private Boolean isFaseOrTrue (String id, List <String> list) {
Boolean flag=false;
for(String str:list) {
if(id.equals(str)) {
flag=true;
break;
}
}
return flag;
}

Front-end code

<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">var Roots = $ ( '# dgMenu') Tree ( 'getRoots');. // returns an array of all the root of the treevar tree_select = $ ( '# tree_select') IS. ( ': The checked');function allselect () {
// Select or deselect



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. assemble background data

/**
* 查询权限
*/
@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;
}

Guess you like

Origin www.cnblogs.com/xianz666/p/12004691.html