layui中tree简单应用

一、项目需求:做一个组织架构一级为总队二级为中队各个中队下又分出各个小队,各小队下分支为志愿者人员--

二、直接上代码 (jsp)

PS:由于我的一级大队是确定的所以就直接写到了js中

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<% response.addHeader("x-frame-options","SAMEORIGIN");%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>


  
    
    
    
    
    组织架构
	
	
	    
	
	
	
	
	
	  
      
	
	
	
	
	
  
  
	



  
  
  
<%@ include file="commentop.jsp" %>
当前位置:首页
科普志愿者组织架构

三、java后台

  /**
         * 获取组织架构
         * @param request
         * @return
        */ 
      @SuppressWarnings("unlikely-arg-type")
	  @RequestMapping(value="/getZuzhi")
      @ResponseBody
	  public List getZuzhi(HttpServletRequest request) {
    	     Map  map = new HashMap();
    	     Jifen jifen = new Jifen();
    	    // List resultParent = new ArrayList();
    	     List result = new ArrayList();
      	     List zdList = zyfwService.getZhongdui(jifen);
      	    
      	     for (int i = 0; i < zdList.size(); i++) {//一级
      	    	zuzhi re =new zuzhi();
      	    	re.setId((String)zdList.get(i).get("keyName"));
      	    	re.setTitle((String)zdList.get(i).get("valueName"));
      	    	result.add(re);
			 }
      	     
      	     for (int i = 0; i < result.size(); i++) {//二级
      	    	Jifen jifen2 = new Jifen();
      	    	jifen2.setZhongdui(result.get(i).getId());
      	    	List xdList = zyfwService.hqXdListByZd(jifen2);
      	    	 List result2 = new ArrayList();
      	    	for (int j = 0; j < xdList.size(); j++) {
      	    		zuzhi re2 =new zuzhi();
      	    		re2.setId((String)xdList.get(j).get("xiaodui"));
      	    		re2.setTitle((String)xdList.get(j).get("xiaoduiStr"));
      	    		result2.add(re2);
				}
      	    	result.get(i).setChildren(result2);
			}
      	     
      	 for (int i = 0;i < result.size(); i++) {//i=1
      		System.out.println(result.get(i).getId());
      		if(result.get(i).getChildren() != null && 
               !"".equals(result.get(i).getChildren())) {
      			for (int j = 0; j < result.get(i).getChildren().size(); j++) {
          	    	Jifen jifen3 = new Jifen();
          	    	jifen3.setXiaodui(result.get(i).getChildren().get(j).getId());
          	    	List ryList = zyfwService.getRyByxd(jifen3);
          	    	List result3 = new ArrayList();
          	    	for (int k = 0; k < ryList.size(); k++) {
          	    		zuzhi re3 =new zuzhi();
          	    		re3.setId(k+"_"+(String)ryList.get(k).get("xiaodui"));
          	    		re3.setTitle((String)ryList.get(k).get("xm"));
          	    		result3.add(re3);
          	    		result.get(i).getChildren().get(j).setChildren(result3);
    				}
    			}
      		}
      		
        }  
      
      	     
	  return result;
    }    

四、还有一个实体类辅助

package com.ssm.bean;

import java.util.*;
/**
 * 组织架构
 * @author Administrator
 *
 */
public class zuzhi {
	private String title;
	private String id;
	private List children;
	
	
	public List getChildren() {
		return children;
	}
	public void setChildren(List children) {
		this.children = children;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	
	
}

五、效果

layui中tree简单应用_第1张图片layui中tree简单应用_第2张图片

希望能对小伙伴们有所帮助QAQ

你可能感兴趣的:(web前端,layui-tree,树形结构)