利用easyUI的樹來實現對角色權限的管理
easyUI 樹的格式:
{
"id":1,
"text":"Foods",
"children":[{
"id":2,
"text":"Fruits",
"state":"closed",
"children":[{
"text":"apple",
"checked":true
},{
"text":"orange"
}]
},{
"id":3,
"text":"Vegetables",
"state":"open",
"children":[{
"text":"tomato",
"checked":true
},{
"text":"carrot",
"checked":true
},{
"text":"cabbage"
},{
"text":"potato",
"checked":true
},{
"text":"lettuce"
}]
}]
}]
java生成tree的數據結構,並轉化成json傳到前端:
//先獲取系統所有權限
List<TRules> ruleList = tRulesService.selectAllRules();
//獲取所有模塊
List<TRules> moduleList = tRulesService.selectAllModules();
//根據角色獲取當前用戶所有權限
String roleRules = tRoleRulesService.selectAllRulesByRoleName(roleName);
//生成樹的格式 text對應module,children 對應role
ArrayList<HashMap<String,Object>> userRoleList = new ArrayList<HashMap<String, Object>>();
//初始化模塊
for(int i = 0; i < moduleList.size(); i++){
HashMap<String,Object> roleMap = new HashMap<String, Object>();
roleMap.put("id",i+1);
roleMap.put("text",moduleList.get(i).getModule());//生成模塊的節點
roleMap.put("state","open");
roleMap.put("children",new ArrayList<HashMap<String,Object>>());//子樹用於保存該模塊下的權限
userRoleList.add(roleMap);
}
//初始化權限
for(int i =0; i < ruleList.size(); i++){
for(int j = 0; j < userRoleList.size(); j++){
HashMap<String,Object> roleMap = userRoleList.get(j);
//找到對應的模塊
if(roleMap.get("text").equals(ruleList.get(i).getModule())){
HashMap<String,Object> childMap = new HashMap<String, Object>();
childMap.put("id",roleMap.get("id") + "" + (i+1));
childMap.put("text",ruleList.get(i).getRuleName());
//如果當前用戶有這個權限,則勾選上
if(!Util.isBlank(roleRules) && roleRules.contains(ruleList.get(i).getRuleName()))
childMap.put("checked","true");
//放到children中
ArrayList<HashMap<String,Object>> list = (ArrayList)roleMap.get("children");
list.add(childMap);
break;
}
}
}
String listJson = JSON.toJSONString(userRoleList);