您好,登錄后才能下訂單哦!
我們會經常用到樹形,那么樹形結構的數據是在前端做還是在后臺做呢?我自己用過前端的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轉換樹結構返回前端內容請搜索億速云以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持億速云!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。