class="java" name="code">/** * 装成树 * @param regions * @return */ //获取树的集合 public List<EasyUITreeDTO> toEasyUITree(List<Region> regions) { // TODO Auto-generated method stub //实体类集合转化为dtos List<RegionDTO> regionsdto=toDTOs(regions); //将dtos装入EasyUITreeDTOs List<EasyUITreeDTO> regionList=this.toEasyUITreeDTO(regionsdto); List<EasyUITreeDTO> roots = new ArrayList<EasyUITreeDTO>(); //找到树的根节点 for (EasyUITreeDTO regionTree : regionList) { String str=regionTree.getPid(); if(StringUtils.isEmpty(str)){ //获取父节点下的子节点 regionTree.setChildren(getChildrenNode(regionTree.getId(),regionList)); regionTree.setState("open"); regionTree.setIconCls("icon-coalmine"); roots.add(regionTree); } } return roots; } /** * 获取子节点 * @param id * @param regionList * @return */ private List<EasyUITreeDTO> getChildrenNode(String id, List<EasyUITreeDTO> regionList) { // TODO Auto-generated method stub List<EasyUITreeDTO> newTreeList=new ArrayList<EasyUITreeDTO>(); for (EasyUITreeDTO regionTreeDTO : regionList) { if( StringUtils.isEmpty(regionTreeDTO.getPid())){ continue; } //这是一个子节点 if(id.equals(regionTreeDTO.getPid())){ //递归获取子节点下的子节点 regionTreeDTO.setChildren(getChildrenNode(regionTreeDTO.getId(),regionList)); newTreeList.add(regionTreeDTO); } } return newTreeList; } /** * dtos转化为EasyUITreeDTOs * @param regionsdto * @return */ private List<EasyUITreeDTO> toEasyUITreeDTO(List<RegionDTO> regionsdto) { // TODO Auto-generated method stub List<EasyUITreeDTO> list=new ArrayList<EasyUITreeDTO>(); for (RegionDTO regionDTO : regionsdto) { EasyUITreeDTO result = new EasyUITreeDTO(); result.setId(regionDTO.getId()); result.setText(regionDTO.getName()); result.setPid(regionDTO.getUpRegionId()); list.add(result); } return list; }
?