java返回树状图节点数据
2021/12/30 12:07:32
本文主要是介绍java返回树状图节点数据,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
前端需要实现设计如图:
后端设计返回的数据类型如下(数据量太大,省略部分数据):
1 {"code":0,"msg":null,"data":[{"sortName":"未知","sortCode":"-","parentCid":"","childrens":[]}, {"sortName":"食品\r\n部","sortCode":"1","parentCid":"","childrens": [{"sortName":"烟酒饮料","sortCode":"100","parentCid":"1","childrens": [{"sortName":"软性饮料","sortCode":"1000","parentCid":"100","childrens": [{"sortName":"碳酸饮料","sortCode":"10000","parentCid":"1000","childrens": [{"sortName":"可乐","sortCode":"100000","parentCid":"10000","childrens":[]}, {"sortName":"加味汽水","sortCode":"100001","parentCid":"10000","childrens":[]}, {"sortName":"其它汽水","sortCode":"100002","parentCid":"10000","childrens":[]}]}, {"sortName":"蔬果汁","sortCode":"10001","parentCid":"1000","childrens": [{"sortName":"蔬菜汁","sortCode":"100010","parentCid":"10001","childrens":[]}, {"sortName":"橙汁","sortCode":"100011","parentCid":"10001","childrens":[]}, {"sortName":"苹果汁","sortCode":"100012","parentCid":"10001","childrens":[]}, {"sortName":"浓缩果汁","sortCode":"100013","parentCid":"10001","childrens":[]}, {"sortName":"其它果汁","sortCode":"100014","parentCid":"10001","childrens":[]}, {"sortName":"桃汁","sortCode":"100015","parentCid":"10001","childrens":[]}, {"sortName":"葡萄汁","sortCode":"100016","parentCid":"10001","childrens":[]}, {"sortName":"混合果汁","sortCode":"100017","parentCid":"10001","childrens":[]}]}, {"sortName":"机能性饮料","sortCode":"10002","parentCid":"1000","childrens": [{"sortName":"咖啡饮料","sortCode":"100020","parentCid":"10002","childrens": []},{"sortName":"功能饮料","sortCode":"100021","parentCid":"10002","childrens": []}]},{"sortName":"茶饮料","sortCode":"10003","parentCid":"1000","childrens": [{"sortName":"奶茶","sortCode":"100030","parentCid":"10003","childrens": []},{"sortName":"加味茶","sortCode":"100031","parentCid":"10003","childrens": []},{"sortName":"功能茶","sortCode":"100032","parentCid":"10003","childrens": []}]},{"sortName":"水","sortCode":"10004","parentCid":"1000","childrens": [{"sortName":"纯水","sortCode":"100040","parentCid":"10004","childrens": []},{"sortName":"矿泉水","sortCode":"100041","parentCid":"10004","childrens": []},{"sortName":"含碳酸水","sortCode":"100042","parentCid":"10004","childrens": []},{"sortName":"其它类水","sortCode":"100043","parentCid":"10004","childrens":[]}]}
这边后端数据类别是共有5个节点,设计实体如下:
public class SortNode { /** * 类被名称 */ private String sortName; /** * 类别代码 */ private String sortCode; /** * 父节点 */ private String parentCid; /** * 所有子节点 */ private List<SortNode> childrens; }
利用递归,实现返回树状数据逻辑代码如下:
public List<SortNode> querySortNode(String storeCode) throws Exception { //从数据库获取配置字段并排序 String sortValue = applicationOptionService.getOptionValue( storeCode, ApplicationOptionService.RUMBA_PREFS_MODULE_NO, LocalPrefParam.BASIC_INFORMATION_GOODS_SORT); List<String> sortLength = Arrays.asList(verifySorts(sortValue, storeCode)); Collections.sort(sortLength); //查询出所有的数据,给父节点赋值 List<SortNode> sortNodes = new ArrayList<>(); List<Sort> sorts = sortDao.getSorts(); sorts.forEach(sort -> { SortNode sortNode = new SortNode(sort.getName(), sort.getCode()); String code = sortNode.getSortCode(); int reduce; int nextSortLength = getLastSortLength(sortLength, code.length()); if (nextSortLength == 0) { reduce = 1; } else { reduce = code.length() - nextSortLength; } String parentCid = code.substring(0, code.length() - reduce); sortNode.setParentCid(parentCid); sortNodes.add(sortNode); }); // 给子节点塞值 List<SortNode> collect = sortNodes.stream() .filter(o -> o.getParentCid().equals("")) .peek(o -> o.setChildrens(getChildSortNode(o, sortNodes))) .collect(Collectors.toList()); return collect; } /** * 递归赋值子节点 */ private List<SortNode> getChildSortNode(SortNode sortNode, List<SortNode> sortNodes) { List<SortNode> collect = sortNodes.stream().filter(o -> o.getParentCid().equals(sortNode.getSortCode())) .peek(o -> o.setChildrens(getChildSortNode(o, sortNodes))) .collect(Collectors.toList()); return collect; }
这篇关于java返回树状图节点数据的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南