我們會經(jīng)常用到樹形,那么樹形結(jié)構(gòu)的數(shù)據(jù)是在前端做還是在后臺做呢?我自己用過前端的ztree,selectTree等這些屬于前端的組件,后臺只需要把一個表的所有數(shù)據(jù)返回給前段就可以,前端可以通過id,pid來把層級結(jié)構(gòu)劃分,要是我們前端需要后臺直接返回樹結(jié)構(gòu)數(shù)據(jù)怎么辦,那么接下來我給大家介紹一下我寫過的例子。
創(chuàng)新互聯(lián)建站是一家集網(wǎng)站建設(shè),達(dá)拉特企業(yè)網(wǎng)站建設(shè),達(dá)拉特品牌網(wǎng)站建設(shè),網(wǎng)站定制,達(dá)拉特網(wǎng)站建設(shè)報價,網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,達(dá)拉特網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競爭力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時我們時刻保持專業(yè)、時尚、前沿,時刻以成就客戶成長自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
我們先看一張圖了解一下樹結(jié)構(gòu):我這里隨便找一張圖了解一下即可
接下來我們看一下數(shù)據(jù),主要包括id,pid,名稱
接下來我們寫一個小例子,用遞歸方式轉(zhuǎn)換為數(shù)
實(shí)體:
package cn.ccccltd.ccccoi.mediumterminvestment.domain; import java.io.Serializable; import java.util.List; /** * @Description:返回前端,業(yè)務(wù)模塊 * @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實(shí)現(xiàn)類
/** * 查詢投資項(xiàng)目資金來源 */ @Override public ListqueryFundingSource() { //查詢投資項(xiàng)目資金來源 List list = projectBasicInfoMapper.queryDictionaryBean(Constant.FUNDING_SOURCE,Constant.DELFLAG); List dataProcessing = ProjectBasicInfoServiceImpl.dataProcessing(list); List buildByRecursive = TreeUtils.buildByRecursive(dataProcessing); return buildByRecursive; } /** * 將字典數(shù)據(jù)遍歷放入ProjectBasicInfoDTO實(shí)體中 * @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; } /** * 遞歸查找子節(jié)點(diǎn) * @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 ()); } //是否還有子節(jié)點(diǎn),如果有的話繼續(xù)往下遍歷,如果沒有則直接返回 projectBasicInfoDTO.getChildren().add(findChildren(projectBasicInfoDTO2,projectBasicInfoDTOs)); } } return projectBasicInfoDTO; } }
樹結(jié)構(gòu)效果圖
到此這篇關(guān)于JAVA后臺轉(zhuǎn)換成樹結(jié)構(gòu)數(shù)據(jù)返回給前端的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)JAVA轉(zhuǎn)換樹結(jié)構(gòu)返回前端內(nèi)容請搜索創(chuàng)新互聯(lián)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持創(chuàng)新互聯(lián)!