bootstrap treeview 树
//实体
package com.rdkl.securityPublish.model; import java.io.Serializable; import java.util.List; import com.alibaba.fastjson.JSON; public class TreeObject implements Serializable{ private int id; private String text; private State state; private List<TreeObject> nodes; public List<TreeObject> getNodes() { return nodes; } public void setNodes(List<TreeObject> nodes) { this.nodes = nodes; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getText() { return text; } public void setText(String text) { this.text = text; } public State getState() { return state; } public void setState(State state) { this.state = state; } public static void main(String[] args) { TreeObject boj = new TreeObject(); State state = new State(); boj.setState(state); boj.setText("test"); System.out.println(JSON.toJSONString(boj)); } }
//控制层
/**
* 递归取树结构数据 * @param role_id * @param press_id * @param parentid * @return * @project_Name security_publish * @Date 2016年11月22日 * @author 胡跃强 */ public List<TreeObject> getTree (Integer role_id,Integer press_id,Integer parentid){ List<TreeObject> list = new ArrayList<TreeObject>(); List<TreeObject> total = new ArrayList<TreeObject>(); Map<String, Object> map = new HashMap<String, Object>(); map.put("pressid", press_id); map.put("parentid", parentid); map.put("roleid",role_id); List<Power> powerList = powerService.selectPowerByGroupLevel(map); for (Power power : powerList) { TreeObject tb = new TreeObject(); tb.setId(power.getPower_id()); tb.setText(power.getPower_name()); State state = new State(); if(power.getMark()!=0){ state.setChecked(true); } state.setExpanded(true); if(power.getChildcnt() > 0){ List<TreeObject> child = getTree(role_id, press_id, power.getPower_id()); tb.setNodes(child); } tb.setState(state); list.add(tb); } total.addAll(list); return total;}
/** * 根据GroupLevel查询 * @param role_id * @param press_id * @return * @project_Name security_publish * @Date 2016年11月18日 * @author 胡跃强 */ @RequestMapping("selectPowerByGroupLevel") public ModelAndView selectPowerByGroupLevel(Integer role_id,Integer press_id,Integer parentid) { List<TreeObject> list = getTree(role_id, press_id, parentid); ModelAndView view = new ModelAndView("system/role_authorization"); view.addObject("powerList", JSON.toJSONString(list)); view.addObject("role_id", role_id); return view; }
//sql
SELECT SP.POWER_ID, SP.POWER_NAME, ( SELECT COUNT (POWER_ID) FROM SP_POWER spc WHERE SPC.PARENT_ID = SP.POWER_ID ) AS childcnt, NVL(SRP.POWER_ID, 0) AS MARK FROM SP_POWER sp LEFT JOIN SP_ROLE_POWER SRP ON SRP.POWER_ID = SP .POWER_ID AND ROLE_ID = #{roleid} WHERE SP.group_level = #{pressid} AND SP.PARENT_ID = #{parentid} ORDER BY SP.POWER_ID ASC
//前台
<div id="tree"></div>
authorizationinit:function(){ var data = $('#powerList').val(); $('#tree').treeview({ data: eval(data), showCheckbox:true, multiSelect:true, onNodeChecked:function(event,node){ var nodes = node.nodes; if(nodes){ if(nodes.length>0){ var idArray = []; for(var i=0; i<nodes.length; i++){ idArray.push(nodes[i].nodeId); var butNodes = nodes[i].nodes; if(butNodes){ for(var j=0; j<butNodes.length; j++) { idArray.push(butNodes[j].nodeId); } } } $('#tree').treeview('checkNode',[idArray,{silent:true}]); } } }, onNodeUnchecked:function(event,node){ var nodes = node.nodes; if(nodes){ if(nodes.length>0){ var idArray = []; for(var i=0; i<nodes.length; i++){ idArray.push(nodes[i].nodeId); var butNodes = nodes[i].nodes; if(butNodes){ for(var j=0; j<butNodes.length; j++) { idArray.push(butNodes[j].nodeId); } } } $('#tree').treeview('uncheckNode',[idArray,{silent:true}]); } } } }) }