递归实现无限级DWZ tree
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了递归实现无限级DWZ tree相关的知识,希望对你有一定的参考价值。
最近开发的项目使用DWZ(JUI)框架,在使用其Tree插件时发现,DWZ Tree只能展示指定层数的数据,无法做到动态加载,因此使用递归算法来实现DWZ Tree动态加载数据,实现无限级tree(理论上),废话不多说,上代码,实体类:
package com.*.*.business.entity.org; import java.util.List; /** * @author hn * @date 2017-8-10 * @Description : 机构实体 */ public class Org { private String orgId;//机构id private String orgNo;//机构编号 private String orgName;//机构名字 private String superId;//上级id private String orgAddr;//机构地址 private String contact;//联系人 private String tel;//电话 private String phone;//手机 private String createtime;//创建时间 private String deep;//深度 private String haveChild;//是否存在子机构 private List<Org> childList;//嵌套子级结构数据 /** * @return the childList */ public List<Org> getChildList() { return childList; } /** * @param childList the childList to set */ public void setChildList(List<Org> childList) { this.childList = childList; } /** * @return the haveChild */ public String getHaveChild() { return haveChild; } /** * @param haveChild the haveChild to set */ public void setHaveChild(String haveChild) { this.haveChild = haveChild; } /** * @return the deep */ public String getDeep() { return deep; } /** * @param deep the deep to set */ public void setDeep(String deep) { this.deep = deep; } public String getOrgId() { return orgId; } public void setOrgId(String orgId) { this.orgId = orgId; } public String getOrgNo() { return orgNo; } public void setOrgNo(String orgNo) { this.orgNo = orgNo; } public String getOrgName() { return orgName; } public void setOrgName(String orgName) { this.orgName = orgName; } public String getSuperId() { return superId; } public void setSuperId(String superId) { this.superId = superId; } public String getOrgAddr() { return orgAddr; } public void setOrgAddr(String orgAddr) { this.orgAddr = orgAddr; } public String getContact() { return contact; } public void setContact(String contact) { this.contact = contact; } public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getCreatetime() { return createtime; } public void setCreatetime(String createtime) { this.createtime = createtime; } /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { return "Org [orgId=" + orgId + ", orgNo=" + orgNo + ", orgName=" + orgName + ", superId=" + superId + ", orgAddr=" + orgAddr + ", contact=" + contact + ", tel=" + tel + ", phone=" + phone + ", createtime=" + createtime + ", deep=" + deep + ", haveChild=" + haveChild + ", childList=" + childList + "]"; } }
Controller类:
/** * @author hn * @date 2017年8月10日 * @Description : */ @Controller @RequestMapping(value="/upgrade") public class UpgradeController { @RequestMapping("/queryDevice") public String queryDevice(HttpServletRequest request, HttpServletResponse response, Model model) { try { List<Org> orgList = orgService.queryOrgByDeep("0"); String deep = orgService.maxDeep(); List<Org> treeList =recursion(orgList); String deviceNo_old = request.getParameter("deviceNo"); List<Device> deviceList = deviceService.queryDeviceAll(); model.addAttribute("treeList", treeList); model.addAttribute("deviceList", deviceList); model.addAttribute("deep", deep); model.addAttribute("deviceNo_old", deviceNo_old); } catch (Exception e) { logger.error(e.getMessage(), e); } return "business/upgrade/upgrade/queryDevice"; } //递归方法 @SuppressWarnings({ "rawtypes", "unchecked" }) private List<Org> recursion(List<Org> treeList){ for(Org org : treeList){ List<Org> list = orgService.queryOrgByPId(org.getOrgNo()); if(list.size()!=0){ recursion(list); } org.setChildList(list); } return treeList; } }
在页面展示时也需要进行递归展示:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> <c:forEach items="${treeList}" var="module1"> <c:set var="index" value="${index + 1}" scope="request" /> <!-- 每一次循环,index+1 --> <c:if test="${level==0}"> <ul class="tree treeFolder treeCheck"> </c:if> <c:if test="${level!=0}"> <ul> </c:if> <li><a>${module1.orgName}</a> <c:set var="child_var" value="0" scope="request" /> <!-- 每一次循环,index+1 --> <c:forEach items="${deviceList}" var="device"> <c:if test="${module1.orgNo eq device.orgNo }"> <ul> <li><c:if test="${fn:contains(deviceNo_old,device.devNo)==true}"> <a tname="name" tvalue="${device.devNo}" checked="true">${device.devNo }</a> </c:if> <c:if test="${fn:contains(deviceNo_old,device.devNo)==false}"> <a tname="name" tvalue="${device.devNo}">${device.devNo }</a> </c:if></li> </ul> <c:set var="child_var" value="1" scope="request" /> <!-- 每一次循环,index+1 --> </c:if> </c:forEach> <c:if test="${child_var==‘0‘}"> <ul> </ul> </c:if> <c:if test="${fn:length(module1.childList) > 0}"> <!-- 如果有childen --> <c:set var="treeList" value="${module1.childList}" scope="request" /> <c:set var="level" value="${level + 1}" scope="request" /> <!-- 循环一次子列表,level+1 --> <c:import url="recursion.jsp" /> <!--递归--> </c:if></li> </ul> </c:forEach> <c:set var="level" value="${level - 1}" scope="request" /> <!-- 退出时,level-1 -->
结果展示如下:
(注:如果数据量较大,请选用异步加载方式,否则一次性加载速度过慢)
以上是关于递归实现无限级DWZ tree的主要内容,如果未能解决你的问题,请参考以下文章