1、数据结构
id 主键
pId 关联Id
name 节点名称
level 节点级别
2、标准数据类型
标准的 JSON 数据需要嵌套表示节点的父子包含关系
数据结构:
var nodes = [
{name: "父节点1", children: [
{name: "子节点1"},
{name: "子节点2"}
]}
];
// 获取标准JSON数据
public static List> getStandardJSON() {
// 根据不同框架获取对应的List数据
List> queryList = query.find();
List> parentList = Lists.newArrayList();
for (Mapmap : queryList) {
if (map.get("pId").equals("0")) {
parentList.add(map);
}
}
recursionChildren(parentList, queryList);
return parentList;
}
// 递归获取子节点数据
public static void recursionChildren (List> parentList,
List> allList) {
for (MapparentMap : parentList) {
List> childrenList = Lists.newArrayList();
for (MapallMap : allList) {
if (allMap.get("pId").equals(parentMap.get("id"))) {
childrenList.add(allMap);
}
}
if (!ParamValidUtils.isEmpty(childrenList)) {
parentMap.put("children", childrenList);
recursionChildren(childrenList, allList);
}
}
}
3、简单数据类型
简单模式的 JSON 数据需要使用 id / pId 表示节点的父子包含关系
数据结构:
var nodes = [
{id:1, pId:0, name: "父节点1"},
{id:11, pId:1, name: "子节点1"},
{id:12, pId:1, name: "子节点2"}
];
// 获取简单JSON数据
public static List> getSimpleJSON() {
// 根据不同框架获取对应的List数据
List> queryList = query.find();
List> parentList = Lists.newArrayList();
for (Mapmap : queryList) {
if (map.get("pId").equals("0")) {
parentList.add(map);
}
}
List> returnList = Lists.newArrayList();
for (MapparentMap : parentList) {
recursionChildren(parentMap, queryList, returnList);
}
return returnList;
}
// 递归获取子节点
public static void recursionChildren(MapparentMap,
List> allList, List> returnList) {
returnList.add(parentMap);
for (MapallMap : allList) {
if (allMap.get("pId").equals(parentMap.get("id"))) {
returnList.add(allMap);
test(allMap, allList, returnList);
}
}
}
4、优化
Java递归获取zTree标准结构数据优化
Java递归获取zTree简单结构数据优化