公司需求是 根据当前员工查找他对应整个分公司及部门的名字,思路:根据员工查找出他属于哪个分公司,拿到分公司PID(可以理解为是子公司的父节点ID),再使用递归方法依次查找,(在递归方法中只要你传父节点ID,不管其子下有没有子节点),
效果图:
现在贴代码:
package com.neusoft.talentbase.organization.orgunit.orgunit.vo; import java.util.List; import com.neusoft.talentbase.framework.core.base.vo.BaseVO; public class MyOrgUnitVO extends BaseVO { /** * 组织单元ID */ private long orgid; /** * 组织单元名称 */ private String orgName; private List<MyOrgUnitVO> list;// 存放子节点 private String open; // 在前台把当前节点展开 /** * 父节点ID */ private long pid; private int isCost; private int isZhangtao; private int type; public int getType() { return type; } public void setType(int type) { this.type = type; } public int getIsCost() { return isCost; } public void setIsCost(int isCost) { this.isCost = isCost; } public int getIsZhangtao() { return isZhangtao; } public void setIsZhangtao(int isZhangtao) { this.isZhangtao = isZhangtao; } public long getPid() { return pid; } public void setPid(long pid) { this.pid = pid; } public List<MyOrgUnitVO> getList() { return list; } public void setList(List<MyOrgUnitVO> list) { this.list = list; } public long getOrgid() { return orgid; } public void setOrgid(long orgid) { this.orgid = orgid; } public String getOrgName() { return orgName; } public void setOrgName(String orgName) { this.orgName = orgName; } public String getOpen() { return open; } public void setOpen(String open) { this.open = open; } }
package com.neusoft.talentbase.organization.orgunit.orgunit.dao; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import org.hibernate.Hibernate; import com.neusoft.talentbase.framework.core.base.dao.BaseDAOImpl; import com.neusoft.talentbase.organization.orgunit.orgunit.vo.MyOrgUnitVO; /** * * {} * @author tangjia * @created 2013-10-23 下午01:56:24 * @lastModified * @history */ public class MyOrgUnitDAOImpl extends BaseDAOImpl implements MyOrgUnitDAO{ public List<MyOrgUnitVO> getOrgUnitTree(long unitId,boolean flag) { String sql=""; if(flag) { sql="select o.c_name as name ,u.c_orgunitid as unitid ,i.c_iscost as iscost ,o.c_type as type "+ " from tb_org_unitrelation u, tb_org_orgunit o,tb_iscostcentre i "+ " where u.c_orgunitid = o.c_oid_orgunit and u.c_orgunitid=i.c_unitid "+ " and u.c_parentunitid in " + " (select u.c_parentunitid from tb_org_unitrelation u where u.c_orgunitid="+unitId+")" + " and u.c_status=1 and i.c_status=1 and o.c_status=1 and u.c_orgunitid="+unitId; } else { sql="select o.c_name as name,u.c_orgunitid as unitid ,i.c_iscost as iscost ,o.c_type as type from " + "tb_org_unitrelation u, tb_org_orgunit o ,tb_iscostcentre i where u.c_orgunitid = o.c_oid_orgunit"+ " and u.c_orgunitid=i.c_unitid and i.c_status=1 and o.c_status=1 and u.c_status=1 and u.c_parentunitid="+unitId; } List list=this.getSession().createSQLQuery(sql) .addScalar("name", Hibernate.STRING) .addScalar("unitid", Hibernate.LONG) .addScalar("iscost", Hibernate.INTEGER).addScalar("type", Hibernate.INTEGER).list(); List <MyOrgUnitVO> orgUnitlist=new ArrayList<MyOrgUnitVO>(); for (int i = 0; i < list.size(); i++) { Object[] obj=(Object[])list.get(i); //查帐套 String s=" select count(*) as counts from rlship_base rl,tb_org_orgunit_append app ,tb_org_orgunit org "+ " where rl.rrolecode = app.c_org_code "+ " and app.c_oid_orgunit = org.c_oid_orgunit "+ " and rl.typeid = '2000772'"+ " and org.c_oid_orgunit ="+obj[1].toString(); List li=this.getSession().createSQLQuery(s).addScalar("counts", Hibernate.INTEGER).list(); MyOrgUnitVO orgUnit=new MyOrgUnitVO(); orgUnit.setOrgName(obj[0].toString()); orgUnit.setOpen("true");//这里设为true是因为需求中要把所有的节点打开 orgUnit.setPid(Long.valueOf(obj[1].toString())); orgUnit.setIsCost(Integer.parseInt(obj[2].toString())); orgUnit.setType(Integer.parseInt(obj[3].toString())); if(Integer.parseInt(li.get(0).toString())>0) { orgUnit.setIsZhangtao(1);//有帐套 } else { orgUnit.setIsZhangtao(0);//没有帐套 } orgUnitlist.add(orgUnit); } for(MyOrgUnitVO unit:orgUnitlist) { List li=getOrgUnitTree(unit.getPid(),false); unit.setList(li); } return orgUnitlist; } }
/** * * { 获取该用户的组织树} * @param mapping * @param form * @param request * @param response * @return * @throws Exception * @author tangjia * @created 2013-10-18 下午06:07:17 * @lastModified * @history */ public ActionForward myOrgUnit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { //获取操作用户权限 long unitOid = 0; SecurityJdbcDAO securityDAO = (SecurityJdbcDAO) getBean("securityJdbcDAOForAction"); HashMap securityMap = (HashMap) securityDAO .getUserFuncHiberarchyAndRootOrgUnit("no_config_func"); if (securityMap == null || securityMap.get("rootNodeId") == null || securityMap.isEmpty()) { OrgUnitCommonService orgUnitCommonService = (OrgUnitCommonService) getBean("orgUnitCommonService"); unitOid = orgUnitCommonService.getDefaultUnit().getOid(); } else { unitOid = Long.parseLong(String.valueOf(securityMap .get("rootNodeId"))); } //总公司员工对应于寿险总公司 if(unitOid==1||unitOid==21266433){ unitOid=245569460; } MyOrgUnitService myOrgUnitDao=(MyOrgUnitService) getBean("myOrgUnitService"); List<MyOrgUnitVO> list=myOrgUnitDao.getOrgUnitTree(unitOid,true); JSONArray jsonObj = JSONArray.fromObject(list); //转换成json格式,前台用的是ztree控件,解析的数据可以为json格式,ztree控件对数据格式有要求,旗下子节点必须是以children 所以我把javabean里面的list替换成children,还有个name是一样的意思 String s=jsonObj.toString().replaceAll("list", "children").replaceAll("orgName", "name"); request.setAttribute("jsonList", s); // System.out.println(jsonObj.toString()); // System.out.println(s); return mapping.findForward("myUnit"); }
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <!-- ztree --> <link rel="stylesheet" href="<%= request.getContextPath() %>/scripts/jquery-treeview-js/css/demo.css" type="text/css"> <link rel="stylesheet" href="<%= request.getContextPath() %>/scripts/jquery-treeview-js/css/zTreeStyle.css" type="text/css"> <script src="<%= request.getContextPath() %>/scripts/jquery-treeview-js/jquery-1.4.4.min.js" type="text/javascript"></script> <script src="<%= request.getContextPath() %>/scripts/jquery-treeview-js/jquery.ztree.core-3.5.js" type="text/javascript"></script> </head> <body> <div style="width:100%;"> <div style="float:left;width:25%;"><span style="background-color:#ccc; font-size:20px">成本中心</span></div> <div style="float:right;width:63%; "><span style="background-color:#ccc; font-size:20px">帐套中心</span></div> </div><br/> <div class="content_wrap"> <div class="zTreeDemoBackground left"> <ul id="treeDemo" class="ztree"></ul> </div> <div class="zTreeDemoBackground right"> <ul id="treeDemo1" class="ztree"></ul> </div> <!-- <div style="width:50%; margin-left:30px;float:left;overflow-y: scroll; height:400px;position: relative;"> <div id="tree3"></div> </div> <div style="width:40%; margin-left:30px;float:right;height:400px;overflow-y: scroll;position: relative;"> <div id="tree2"></div> </div>--> </div> <script type="text/javascript"> var jsonObj = <%=request.getAttribute("jsonList")%>; //----------------------ztree------------------------ function setFontCss(treeId, treeNode) { if(treeNode.type == 1 && treeNode.isCost==1) return treeNode.type == 1 ? {"background-color":"#FF6666 "} : {}; if(treeNode.type == 2 && treeNode.isCost==1) return treeNode.type == 2 ? {"background-color":"#FF9933 "} : {}; if(treeNode.type == 3 && treeNode.isCost==1) return treeNode.type == 3 ? {"background-color":"#FFFF99 "} : {}; if(treeNode.type == 4 && treeNode.isCost==1) return treeNode.type == 4 ? {"background-color":"#339933 "} : {}; if(treeNode.type == 5 && treeNode.isCost==1) return treeNode.type == 5 ? {"background-color":"#CCFF99 "} : {}; if(treeNode.type == 6 && treeNode.isCost==1) return treeNode.type == 6 ? {"background-color":"#99CCFF "} : {}; if(treeNode.type == 7 && treeNode.isCost==1) return treeNode.type == 7 ? {"background-color":"#9999CC "} : {}; }; function setFontCss1(treeId, treeNode) { if(treeNode.isZhangtao == 1 ) return treeNode.isZhangtao == 1 ? {"background-color":"#99CC33 "} : {}; }; var setting = { view: { showLine: true,//显示连接线 showIcon: true,//显示节点图片 //fontCss: {color:"red"} fontCss: setFontCss//节点颜色 } }; var setting1 = { view: { showLine: true,//显示连接线 showIcon: false,//显示节点图片 //fontCss: {color:"red"} fontCss: setFontCss1//节点颜色 } }; $(document).ready(function(){ $.fn.zTree.init($("#treeDemo"), setting, jsonObj); }); $(document).ready(function(){ $.fn.zTree.init($("#treeDemo1"), setting1, jsonObj); }); </script> </body> </html>
到此就把从数据库中查询出来的数据 ,展示在jsp 树上面了
相关推荐
总的来说,Thinkphp5.0实现无极限评论系统的关键在于理解框架的MVC架构,熟练运用模型、控制器和视图,以及掌握数据库的关联查询和递归算法。通过这样的设计,我们可以构建出一个灵活、可扩展的评论系统,满足用户...
本文将详细解析"php无极限分类源码"的相关知识点,并结合提供的"class"文件名称,探讨PHP如何实现无限分类的原理与实践。 首先,无限分类的核心思想是利用递归或自关联来构建层级结构。在PHP中,常见的方法有两种:...
【无极限可刷新js树 js tree】是一种基于JavaScript实现的动态树形菜单组件,它具有多项功能特性,适用于构建高效且交互性强的Web界面。这个组件的特点在于其灵活性和兼容性,无需依赖任何大型框架,使得它在各种...
在IT领域,特别是数据库设计和前端展示中,"树形结构无限级分类"是一个常见的需求。这个主题涉及到如何在数据库中存储具有层级关系的数据,并在应用程序中以树状形式进行展示。下面我们将深入探讨这个话题。 首先,...
总结起来,利用 PHP 和 MySQL,我们可以通过一次查询获取所有分类数据,然后通过递归函数构建分类树,实现无极限分类栏目的展示。这种方法既节省了数据库查询次数,又保证了数据的实时性,是处理多层级分类的有效...
- **关于极限情况下B+树的删除**:在B+树中删除元素时,需要注意节点合并的情况,以保证树的性质不受破坏。 - **B树和B+树的读写次数?效率怎么计算?**:B树和B+树的读写效率主要取决于树的高度和每个节点的键值...
- 极限情况下B+树的删除可能导致节点合并或重新分配,需要特殊处理以保持树的性质。 52. **B树和B+树的读写次数?效率怎么计算?** - B树和B+树的读写次数取决于树的高度和查询的深度。效率计算通常基于最坏情况...
案例展示了如何使用决策树分类器进行乳腺癌的诊断,为疾病的早期检测提供了有效的手段。 #### 极限学习机在回归拟合及分类问题中的应用研究——对比实验 极限学习机(Extreme Learning Machine, ELM)是一种快速...