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返回树状图节点数据 的全部内容, 来源链接: utcz.com/z/389704.html