理论:http://blog.csdn.net/babyxue/article/details/73835444
插依赖Bootstrap 和jQuery
<link href="~/bootstrap-treeview.css" rel="stylesheet" />
<link href="./css/bootstrap.css" rel="stylesheet">
<script src="./js/jquery.js"></script>
<script src="./js/bootstrap-treeview.js"></script>
jsp中
<div class="col-sm-3">
<input type="hidden" id="powerList" name="powerList" value='${powerList}'/>
<input type="hidden" id="ids" name="ids" value="${ids}" />
<c:if test="${powerList!=null}">
<div id="tree"></div>
</c:if>
</div>
js中
var log = { initTree: function () { var data = $('#powerList').val(); $('#tree').treeview({ data: data, showCheckbox:false, multiSelect:false, onNodeSelected:function(event,node){ //选中节点其子节点,父节点 var ids = []; var parentNode = $('#tree').treeview('getParent',node); //父节点// var nodes = node.nodes; //子节点 if(nodes){ for(var i=0; i<nodes.length; i++){ ids.push(nodes[i].id) var childNode = nodes[i].nodes; if(childNode){ for(var j=0; j<childNode.length; j++){ ids.push(childNode[j].id) } } } }else{ ids.push(node.id) } //console.info(ids); $('#ids').val(ids); setTimeout(function () { log.reload(); }, 1000) }, onNodeUnselected:function(event,node){ $('#ids').val(''); setTimeout(function () { log.reload(); }, 1000) } }); }
};
这里得到节点的id的时候,有点小问题
@RequestMapping(value = "view") public ModelAndView toLog(ModelAndView modelAndView) throws Exception { modelAndView.setViewName("system/log"); try{ List<TreeObject> list = getTree(Long.parseLong("-1")); TreeObject treeObject = new TreeObject(); treeObject.setId(-1L); treeObject.setText("全部菜单"); State rootState = new State(); rootState.setExpanded(true); rootState.setChecked(false); treeObject.setState(rootState); //只显示递归产生的两层 TreeObject treeObj = new TreeObject(); for(int i = 0; i < list.size(); i++){ List<TreeObject> list2 = list.get(i).getNodes(); if(list2 != null && list2.size() > 0){ for(int j = 0; j < list2.size(); j++){ if(list2.get(j).getNodes() != null){ list2.get(j).setNodes(null); } } } } treeObject.setNodes(list); modelAndView.addObject("powerList", "[" + JSON.toJSONString(treeObject) + "]"); }catch(Exception ex){ logger.error("进入日志管理页面出错!",ex); } return modelAndView; } //递归树 public List<TreeObject> getTree(Long parentid) { List<TreeObject> list = new ArrayList<TreeObject>(); List<TreeObject> total = new ArrayList<TreeObject>(); Map<String, Object> map = new HashMap<String, Object>(); map.put("parent_id", parentid); map.put("sys_type", 1); List<Power> powerList = powerService.getPowerByGroupLevel(map); for (Power power : powerList) { TreeObject tb = new TreeObject(); tb.setId(power.getPower_id()); tb.setText(power.getPower_name()); State state = new State(); state.setChecked(false); state.setExpanded(false); if (power.getChildcnt() > 0) { List<TreeObject> child = getTree(power.getPower_id()); tb.setNodes(child); } tb.setState(state); list.add(tb); } total.addAll(list); return total; }