这种返回格式,最小级会出现: “children”: [],如果前端 使用¶ Cascader 级联选择器 会出现bug
以树形结构返回:
{
"msg": "操作成功",
"code": 200,
"data": [
{
"id": 1,
"name": "AA",
"describe": "DFDSF",
"parentId": 0,
"level": 1,
"ancestors": "0",
"type": 1,
"url": "dgfsdgsgfd",
"createTime": "2021-04-28T08:53:11.000+0000",
"children": [
{
"id": 2,
"parentId": 1,
"level": 2,
"children": [
{
"id": 3,
"name": "dfsd",
"describe": "dfsgfdsg",
"parentId": 2,
"level": 3,
"ancestors": "0,1,2",
"type": 0,
"url": "dfdsg",
"children": []
}
]
},
{
"id": 4,
"name": "DGFFG",
"describe": "DFAGF",
"parentId": 1,
"level": 2,
"children": []
}
]
}
]
}
1.首先看一下表结构:三个字段
id(主键id),
parent_id (父id[根节点为0]),
level (等级 [递增 根节点为1])
2.需要在返回类中设置List:
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class RdHelpCenterVo implements Serializable {
/**
* 主键
*/
private Integer id;
/**
* 父id[根节点为0]
*/
private Integer parentId;
/**
* 等级 [递增 根节点为0]
*/
private Integer level;
/**
* 子树列表
*/
private List<RdHelpCenterVo> children ;
}
3.思路: 将表中所有数据全部取出,使用递归查询【注意:此表的数据不是很多,所以才用树形结构】
public List<RdHelpCenterVo> getTree() {
List<RdHelpCenterVo> allList=helpCenterDao.getTree();
List<RdHelpCenterVo> treeList=new ArrayList<>();
for (RdHelpCenterVo vo :allList){
if (vo.getParentId()==0){
vo.setChildren(getChildrenNode(vo.getId(), allList));
treeList.add(vo);
}
}
return treeList;
}
/**
* 递归获取子节点下的子节点
* @param integer 父节点的ID
* @param treesList 所有菜单树集合
* @return
*/
private List<RdHelpCenterVo> getChildrenNode(Integer integer, List<RdHelpCenterVo> treesList) {
List<RdHelpCenterVo> newTrees = new ArrayList<RdHelpCenterVo>();
for (RdHelpCenterVo department : treesList) {
if (department.getParentId() == 0) {
continue;
}
if (department.getParentId() == integer) {
// 递归获取子节点下的子节点,即设置树控件中的children
department.setChildren(getChildrenNode(department.getId(), treesList));
newTrees.add(department);
}
}
return newTrees;
}
4.有时要删除某一节点,连带删除该节点的所有子节点:
public static List newTrees = new ArrayList();
newTrees 设置成了全局变量,在每次调用时,都要先:
newTrees.clear();
/**
* 方法实现说明: 获取子节点,用于删除,所以不是树形,
*/
public List<RdHelpCenterVo> getChildrenList(List<RdHelpCenterVo> orgList,Integer id){
for (int i = 0; i < orgList.size(); i++) {
RdHelpCenterVo org = orgList.get(i);
//遍历出父id等于参数的id,add进子节点集合
if(org.getParentId()==(id)){
newTrees.add(org);
orgList.remove(org);
//递归遍历下一级
getChildrenList(orgList,org.getId());
i--;
}
}
return newTrees;
}
一般而言:能用树形结构展示,一般都有parentId字段,或类似字段
实体类:
/**
* 实体类:
* @描述 ProProject类 递归展示类
*/
@Data
@ToString
@AllArgsConstructor
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ProProjectRecursionVo {
/**
* 主键
*/
private Integer id;
/**
* 名称
*/
private String name;
/**
* 父id
*/
private Integer parentId;
/**
*
* 子集
*/
List<ProProjectRecursionVo> children=null;
}
public List<UserGroupVo> getUserGroups() {
//获取所有的用户组列表
List<UserGroupVo> list =getChildrenNode(0);
return list;
}
/**
* 递归获取子节点下的子节点
* @param
* @param
* @return
*/
private List<UserGroupVo> getChildrenNode(Integer parentId) {
List<UserGroupVo> newTrees = new ArrayList<>();
//通过parentId 来查询列表
List<UserGroupVo> list = userGroupMapper.getUserGroupListByParentId(parentId);
for (UserGroupVo item : list) {
List<UserGroupVo> news = getChildrenNode(item.getValue());
if (news == null || news.size() == 0) {
newTrees.add(item);
continue;
}else {
item.setChildren(news);
newTrees.add(item);
}
}
return newTrees;
}
结果:
{
"msg": "操作成功",
"code": 200,
"data": [
{
"id": 82,
"name": "银川双优云桥项目",
"parentId": 0,
"children": [
{
"id": 83,
"name": "一期",
"parentId": 82,
"children": [
{
"id": 84,
"name": "教师信息技术应用能力提升",
"parentId": 83
},
{
"id": 85,
"name": "京银互动一带一路共读工程",
"parentId": 83
},
{
"id": 89,
"name": "名校联盟双师课堂",
"parentId": 83
},
{
"id": 91,
"name": "中小学生综合素质评价",
"parentId": 83
},
{
"id": 92,
"name": "中小学生计算思维培养课程",
"parentId": 83
},
{
"id": 96,
"name": "STEAM课程",
"parentId": 83
},
{
"id": 97,
"name": "全学科互联网项目式学习",
"parentId": 83
},
{
"id": 98,
"name": "课堂教学行为大数据分析",
"parentId": 83
},
{
"id": 99,
"name": "语文课程教学改革",
"parentId": 83
},
{
"id": 100,
"name": "在岗研修",
"parentId": 83
},
{
"id": 116,
"name": "mar/B组",
"parentId": 83
},
{
"id": 161,
"name": "师范生粉笔字技能提升项目",
"parentId": 83
}
]
},
{
"id": 183,
"name": "图",
"parentId": 82
}
]
},
{
"id": 118,
"name": "一个项目",
"parentId": 0,
"children": [
{
"id": 119,
"name": "一期",
"parentId": 118,
"children": [
{
"id": 121,
"name": "mar/B组",
"parentId": 119
}
]
}
]
},
{
"id": 122,
"name": "测试",
"parentId": 0,
"children": [
{
"id": 129,
"name": "一期",
"parentId": 122,
"children": [
{
"id": 130,
"name": "mar/A组",
"parentId": 129
}
]
}
]
},
{
"id": 131,
"name": "教师教育学院",
"parentId": 0,
"children": [
{
"id": 132,
"name": "一期",
"parentId": 131,
"children": [
{
"id": 133,
"name": "测试项目",
"parentId": 132
}
]
}
]
},
{
"id": 138,
"name": "研修平台演示",
"parentId": 0,
"children": [
{
"id": 139,
"name": "演示",
"parentId": 138,
"children": [
{
"id": 140,
"name": "京银互动一带一路共读工程",
"parentId": 139
}
]
}
]
},
{
"id": 141,
"name": "数据统计",
"parentId": 0,
"children": [
{
"id": 142,
"name": "数据统计",
"parentId": 141,
"children": [
{
"id": 144,
"name": "测试",
"parentId": 142
}
]
}
]
},
{
"id": 145,
"name": "测试二",
"parentId": 0,
"children": [
{
"id": 146,
"name": "测试二",
"parentId": 145,
"children": [
{
"id": 147,
"name": "测试二",
"parentId": 146
}
]
}
]
},
{
"id": 148,
"name": "ClassIn",
"parentId": 0,
"children": [
{
"id": 149,
"name": "ClassIn",
"parentId": 148,
"children": [
{
"id": 150,
"name": "ClassIn",
"parentId": 149
}
]
}
]
},
{
"id": 151,
"name": "TONGJI",
"parentId": 0,
"children": [
{
"id": 152,
"name": "TONGJI",
"parentId": 151,
"children": [
{
"id": 153,
"name": "TONGJI",
"parentId": 152
}
]
}
]
},
{
"id": 154,
"name": "TONGJI_TEST",
"parentId": 0,
"children": [
{
"id": 155,
"name": "TONGJI_TEST",
"parentId": 154,
"children": [
{
"id": 156,
"name": "TONGJI_TEST",
"parentId": 155
}
]
}
]
},
{
"id": 157,
"name": "最终统计",
"parentId": 0,
"children": [
{
"id": 158,
"name": "最终统计",
"parentId": 157,
"children": [
{
"id": 159,
"name": "最终统计",
"parentId": 158
}
]
}
]
},
{
"id": 160,
"name": "测试20210806",
"parentId": 0
},
{
"id": 165,
"name": "20210927普通话测试项目",
"parentId": 0,
"children": [
{
"id": 166,
"name": "一期",
"parentId": 165,
"children": [
{
"id": 167,
"name": "20210927普通话测试项目",
"parentId": 166
},
{
"id": 173,
"name": "20210928普通话",
"parentId": 166
}
]
}
]
},
{
"id": 174,
"name": "和田项目",
"parentId": 0,
"children": [
{
"id": 175,
"name": "和田一期",
"parentId": 174,
"children": [
{
"id": 176,
"name": "和田优秀干部培训项目(测试)",
"parentId": 175
}
]
}
]
},
{
"id": 177,
"name": "2021教师能力提升测试",
"parentId": 0,
"children": [
{
"id": 178,
"name": "1期",
"parentId": 177,
"children": [
{
"id": 179,
"name": "2021和田墨玉普通话培训",
"parentId": 178
}
]
}
]
},
{
"id": 180,
"name": "测试全流程",
"parentId": 0,
"children": [
{
"id": 181,
"name": "1期",
"parentId": 180,
"children": [
{
"id": 182,
"name": "测试全流程项目",
"parentId": 181
}
]
}
]
},
{
"id": 185,
"name": "云端答疑",
"parentId": 0,
"children": [
{
"id": 186,
"name": "一期",
"parentId": 185,
"children": [
{
"id": 187,
"name": "云端答疑",
"parentId": 186
}
]
}
]
}
]
}
为防止原文失效,转载此贴。原文链接:https://blog.csdn.net/weixin_42528855/article/details/116259541