1.页面:
<!DOCTYPE html> <html> <head> <title>所属机构</title> #parse("include/header.html") </head> <body> <div id="dpLTE" class="container-fluid" v-cloak> <ul id="orgTree" class="ztree"></ul> </div> #parse("include/footer.html") <script type="text/javascript"> var setting = { data : { simpleData : { enable : true, idKey : "orgId", pIdKey : "parentId", rootPId : 0 }, } }; var ztree; var vm = new Vue({ el : ‘#dpLTE‘, data : { orgId : ‘‘, orgName: ‘‘ }, methods : { getOrg : function(menuId) { $.get(‘../../sys/org/selectOrgPermi?_‘ + $.now(), function(r) { ztree = $.fn.zTree.init($("#orgTree"), setting, r); }) }, acceptClick: function() { var node = ztree.getSelectedNodes(); var org = new Array(); org[0] = node[0].orgId; org[1] = node[0].name; dialogClose(); return org; } }, created : function() { this.getOrg(); } }) </script> </body> </html>
2.后台代码:
public List<SysOrgDO> listTree(Long parentId, boolean bol) { List<SysOrgDO> orgList = new ArrayList<SysOrgDO>(); findOrgTreeByParentId(parentId, orgList, bol); SysOrgDO org = new SysOrgDO(); org.setOrgId(parentId); org.setName("ROOT"); org.setParentId(-1L); org.setOpen(true); orgList.add(org); return orgList; } /** * 树形查找 * @param parentId * @param list * @param bol 是否查找多级子节点 */ private void findOrgTreeByParentId(Long parentId, List<SysOrgDO> list, boolean bol) { BaseQuery query = new BaseQuery(); query.put("parentId", parentId); List<SysOrgDO> orgList = sysOrgDao.list(query); if (orgList != null && orgList.size() > 0) { list.addAll(orgList); if(bol){ for(SysOrgDO sdo : orgList){ findOrgTreeByParentId(sdo.getOrgId(), list, bol); } } }else{ return; } }
3.sql:
SELECT t.org_id, t.parent_id, ( SELECT p.name FROM sys_org p WHERE p.org_id = t.parent_id ) AS parentName, t.channel_id, ( SELECT p.name FROM sys_org p WHERE p.org_id = t.channel_id ) AS channelName FROM sys_org t