JAVA后台转换成树结构数据返回给前端的实现方法
作者:qq_42281649
这篇文章主要介绍了JAVA后台转换成树结构数据返回给前端的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
我们会经常用到树形,那么树形结构的数据是在前端做还是在后台做呢?我自己用过前端的ztree,selectTree等这些属于前端的组件,后台只需要把一个表的所有数据返回给前段就可以,前端可以通过id,pid来把层级结构划分,要是我们前端需要后台直接返回树结构数据怎么办,那么接下来我给大家介绍一下我写过的例子。
我们先看一张图了解一下树结构:我这里随便找一张图了解一下即可
接下来我们看一下数据,主要包括id,pid,名称
接下来我们写一个小例子,用递归方式转换为数
实体:
package cn.ccccltd.ccccoi.mediumterminvestment.domain; import java.io.Serializable; import java.util.List; /** * @Description:返回前端,业务模块 * @author yujiaqi * @date: 2019年9月9日下午2:07:19 */ public class ProjectBasicInfoDTO implements Serializable { private static final long serialVersionUID = 1L; private String id; private String pId; private String key; private String title; private String value; private List<ProjectBasicInfoDTO> children; private Boolean selectable; private Boolean disableCheckbox; private Boolean disabled; public String getId() { return id; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public void setId(String id) { this.id = id; } public String getpId() { return pId; } public void setpId(String pId) { this.pId = pId; } public String getKey() { return key; } public void setKey(String key) { this.key = key; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public List<ProjectBasicInfoDTO> getChildren() { return children; } public void setChildren(List<ProjectBasicInfoDTO> children) { this.children = children; } public Boolean getSelectable() { return selectable; } public void setSelectable(Boolean selectable) { this.selectable = selectable; } public Boolean getDisableCheckbox() { return disableCheckbox; } public void setDisableCheckbox(Boolean disableCheckbox) { this.disableCheckbox = disableCheckbox; } public Boolean getDisabled() { return disabled; } public void setDisabled(Boolean disabled) { this.disabled = disabled; } public static long getSerialversionuid() { return serialVersionUID; } }
service实现类
/** * 查询投资项目资金来源 */ @Override public List<ProjectBasicInfoDTO> queryFundingSource() { //查询投资项目资金来源 List<DictionaryBean> list = projectBasicInfoMapper.queryDictionaryBean(Constant.FUNDING_SOURCE,Constant.DELFLAG); List<ProjectBasicInfoDTO> dataProcessing = ProjectBasicInfoServiceImpl.dataProcessing(list); List<ProjectBasicInfoDTO> buildByRecursive = TreeUtils.buildByRecursive(dataProcessing); return buildByRecursive; } /** * 将字典数据遍历放入ProjectBasicInfoDTO实体中 * @param list * @return */ public static List<ProjectBasicInfoDTO> dataProcessing(List<DictionaryBean> list){ List<ProjectBasicInfoDTO> list1 = Lists.newArrayList(); for (DictionaryBean dictionaryBean : list) { ProjectBasicInfoDTO projectBasicInfoDTO = new ProjectBasicInfoDTO(); projectBasicInfoDTO.setId(dictionaryBean.getItemId()); projectBasicInfoDTO.setpId(dictionaryBean.getItemParentId()); projectBasicInfoDTO.setKey(dictionaryBean.getItemId()); projectBasicInfoDTO.setTitle(dictionaryBean.getItemFullName()); projectBasicInfoDTO.setValue(dictionaryBean.getItemFullName()); projectBasicInfoDTO.setSelectable(false); projectBasicInfoDTO.setDisableCheckbox(true); projectBasicInfoDTO.setDisabled(true); list1.add(projectBasicInfoDTO); } return list1; }
递归方法工具类
package cn.ccccltd.ccccoi.mediumterminvestment.util; import java.util.ArrayList; import java.util.List; import cn.ccccltd.ccccoi.mediumterminvestment.domain.ProjectBasicInfoDTO; public class TreeUtils { /** * 使用递归方法建树 * @param treeNodes * @return */ public static List<ProjectBasicInfoDTO> buildByRecursive(List<ProjectBasicInfoDTO> projectBasicInfoDTOs) { List<ProjectBasicInfoDTO> trees = new ArrayList<ProjectBasicInfoDTO>(); for (ProjectBasicInfoDTO projectBasicInfoDTO : projectBasicInfoDTOs) { if ("".equals(projectBasicInfoDTO.getpId()) || projectBasicInfoDTO.getpId() == null ) { trees.add(findChildren(projectBasicInfoDTO,projectBasicInfoDTOs)); } } return trees; } /** * 递归查找子节点 * @param treeNodes * @return */ public static ProjectBasicInfoDTO findChildren(ProjectBasicInfoDTO projectBasicInfoDTO,List<ProjectBasicInfoDTO> projectBasicInfoDTOs) { for (ProjectBasicInfoDTO projectBasicInfoDTO2 : projectBasicInfoDTOs) { if(projectBasicInfoDTO.getId().equals(projectBasicInfoDTO2.getpId())) { if(projectBasicInfoDTO.getChildren() == null) { projectBasicInfoDTO.setChildren(new ArrayList<ProjectBasicInfoDTO>()); } //是否还有子节点,如果有的话继续往下遍历,如果没有则直接返回 projectBasicInfoDTO.getChildren().add(findChildren(projectBasicInfoDTO2,projectBasicInfoDTOs)); } } return projectBasicInfoDTO; } }
树结构效果图
到此这篇关于JAVA后台转换成树结构数据返回给前端的实现方法的文章就介绍到这了,更多相关JAVA转换树结构返回前端内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!