JAVA后臺(tái)轉(zhuǎn)換成樹結(jié)構(gòu)數(shù)據(jù)返回給前端的實(shí)現(xiàn)方法
我們會(huì)經(jīng)常用到樹形,那么樹形結(jié)構(gòu)的數(shù)據(jù)是在前端做還是在后臺(tái)做呢?我自己用過前端的ztree,selectTree等這些屬于前端的組件,后臺(tái)只需要把一個(gè)表的所有數(shù)據(jù)返回給前段就可以,前端可以通過id,pid來把層級(jí)結(jié)構(gòu)劃分,要是我們前端需要后臺(tái)直接返回樹結(jié)構(gòu)數(shù)據(jù)怎么辦,那么接下來我給大家介紹一下我寫過的例子。
我們先看一張圖了解一下樹結(jié)構(gòu):我這里隨便找一張圖了解一下即可
接下來我們看一下數(shù)據(jù),主要包括id,pid,名稱
接下來我們寫一個(gè)小例子,用遞歸方式轉(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 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實(shí)現(xiàn)類
/** * 查詢投資項(xiàng)目資金來源 */ @Override public List<ProjectBasicInfoDTO> queryFundingSource() { //查詢投資項(xiàng)目資金來源 List<DictionaryBean> list = projectBasicInfoMapper.queryDictionaryBean(Constant.FUNDING_SOURCE,Constant.DELFLAG); List<ProjectBasicInfoDTO> dataProcessing = ProjectBasicInfoServiceImpl.dataProcessing(list); List<ProjectBasicInfoDTO> buildByRecursive = TreeUtils.buildByRecursive(dataProcessing); return buildByRecursive; } /** * 將字典數(shù)據(jù)遍歷放入ProjectBasicInfoDTO實(shí)體中 * @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; } /** * 遞歸查找子節(jié)點(diǎn) * @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>()); } //是否還有子節(jié)點(diǎn),如果有的話繼續(xù)往下遍歷,如果沒有則直接返回 projectBasicInfoDTO.getChildren().add(findChildren(projectBasicInfoDTO2,projectBasicInfoDTOs)); } } return projectBasicInfoDTO; } }
樹結(jié)構(gòu)效果圖
到此這篇關(guān)于JAVA后臺(tái)轉(zhuǎn)換成樹結(jié)構(gòu)數(shù)據(jù)返回給前端的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)JAVA轉(zhuǎn)換樹結(jié)構(gòu)返回前端內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java中String、StringBuffer與StringBuilder的區(qū)別
這篇文章主要介紹了java 中String和StringBuffer與StringBuilder的區(qū)別,在開發(fā)過程中經(jīng)常會(huì)用到String這個(gè)類進(jìn)行操作。需要的朋友可以收藏下,方便下次瀏覽觀看2021-12-12Java中弱引用和軟引用的區(qū)別以及虛引用和強(qiáng)引用介紹
很早Java API就添加了弱引用(WeakReference)和軟引用(SoftReference),但并不是所有的程序員都熟悉這兩個(gè)概念2014-04-04Spring security密碼加密實(shí)現(xiàn)代碼實(shí)例
這篇文章主要介紹了Spring security密碼加密實(shí)現(xiàn)代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04詳解springboot項(xiàng)目帶Tomcat和不帶Tomcat的兩種打包方式
這篇文章主要介紹了詳解springboot項(xiàng)目帶Tomcat和不帶Tomcat的兩種打包方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09Java getRealPath("/")與getContextPath()區(qū)別詳細(xì)分析
這篇文章主要介紹了Java getRealPath("/")與getContextPath()區(qū)別詳細(xì)分析,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08