重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
我们会经常用到树形,那么树形结构的数据是在前端做还是在后台做呢?我自己用过前端的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 Listchildren; 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 getChildren() { return children; } public void setChildren(List 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 ListqueryFundingSource() { //查询投资项目资金来源 List list = projectBasicInfoMapper.queryDictionaryBean(Constant.FUNDING_SOURCE,Constant.DELFLAG); List dataProcessing = ProjectBasicInfoServiceImpl.dataProcessing(list); List buildByRecursive = TreeUtils.buildByRecursive(dataProcessing); return buildByRecursive; } /** * 将字典数据遍历放入ProjectBasicInfoDTO实体中 * @param list * @return */ public static List dataProcessing(List list){ List 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 ListbuildByRecursive(List projectBasicInfoDTOs) { List trees = new ArrayList (); 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 projectBasicInfoDTOs) { for (ProjectBasicInfoDTO projectBasicInfoDTO2 : projectBasicInfoDTOs) { if(projectBasicInfoDTO.getId().equals(projectBasicInfoDTO2.getpId())) { if(projectBasicInfoDTO.getChildren() == null) { projectBasicInfoDTO.setChildren(new ArrayList ()); } //是否还有子节点,如果有的话继续往下遍历,如果没有则直接返回 projectBasicInfoDTO.getChildren().add(findChildren(projectBasicInfoDTO2,projectBasicInfoDTOs)); } } return projectBasicInfoDTO; } }
树结构效果图
到此这篇关于JAVA后台转换成树结构数据返回给前端的实现方法的文章就介绍到这了,更多相关JAVA转换树结构返回前端内容请搜索创新互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持创新互联!