获取角色下的员工两张表递归生成树
/**
* 员工树结构
*/
@RequestMapping(value="/getZzjgTree")
public @ResponseBody List<Map<String, Object>> getZzjgTree(Zzjg zzjg){
List<Zzjg> zzjgList = zzjgService.getZzjgByYh();
Integer fbh = 0;
List<Zzjg> digui = digui(zzjgList, fbh);
return digui2(digui);
}
public List<Map<String, Object>> digui2(List<Zzjg> zzjgList){
List<Map<String, Object>> list = new ArrayList<>();
if (null !=zzjgList &&zzjgList.size()>0) {
for (Zzjg zzjg : zzjgList) {
Map<String, Object> map = new HashMap<>();
map.put("id", zzjg.getBh());
map.put("text", zzjg.getMc());
List<Map<String, Object>> digui2 = digui2(zzjg.getZzjgs());
List<Map<String, Object>> yh = yh(zzjg.getYh());
digui2.addAll(yh);
map.put("children", digui2);
list.add(map);
}
}
return list;
}
public List<Map<String, Object>> yh(List<Yh> yhList){
List<Map<String, Object>> list = new ArrayList<>();
if (null !=yhList &&yhList.size()>0) {
for (Yh yh : yhList) {
Map<String, Object> map = new HashMap<>();
map.put("id", yh.getBh());
map.put("text", yh.getXm());
list.add(map);
}
}
return list;
}
public List<Zzjg> digui(List<Zzjg> zzjgList,Integer fbh){
List<Zzjg> list = new ArrayList<>();
for (Zzjg zzjg : zzjgList) {
if (zzjg.getFbh().equals(fbh)) {
List<Zzjg> digui = digui(zzjgList, zzjg.getBh());
zzjg.setZzjgs(digui);
list.add(zzjg);
}
}
return list;
}
前端操作
$(function() {
getTreeData();
})
function getChildren(bh, o) {
var r = [];
var i = 0, ic = o.length, j;
for (; i < ic; i++) {
if (o[i].parent_index == bh) {
j = {
"id":o[i].bh,
"text" : o[i].index_name
};
if(o[i].lx==0)j["state"]="closed";
j.children = getChildren(o[i].bh, o);
j.attributes = o[i];
r.push(j);
}
}
return r;
}
/**
* 加载数据
*
*/
function getTreeData() {
$.get("assess_index/aiTreeData?template_code="+"${template_code}", function(d) {
var rootData = [ {
"id" : 0,
"text" : "${template_name}",
children : getChildren(0, d)//子数据d
} ];
$("#tt").tree({
data : rootData,
cascadeCheck : true
,onClick:function(node){
if(node.id==0 || node.attributes.parent_index==0)
window.gnanList.location.href="assess_index/index_admin?parent_index="+node.id+"&template_code="+"${template_code}";
}
});
}, "json");
}