public class CourseCategoryServiceImpl extends ServiceImpl<CourseCategoryMapper, CourseCategory>
implements CourseCategoryService {
public List<CourseCategory> findCategoiresTree() {
// 1 :查询表中所有的数据
List<CourseCategory> allList = this.list(); // 思考空间,为什么查询的是所有
// 2: 找到所有的根节点 pid = 0
List<CourseCategory> rootList = allList.stream().filter(category -> category.getPid().equals(0))
.sorted((a,b)->a.getSorted()-b.getSorted()).collect(Collectors.toList());
// 3 : 查询所有的非根节点
List<CourseCategory> subList = allList.stream().filter(category -> !category.getPid().equals(0)).collect(Collectors.toList());
// 4 : 循环根节点去subList去找对应的子节点
rootList.forEach(root -> busortxxxxxxxxxxxxxxxx(root,subList));
return rootList;
}
private void busortxxxxxxxxxxxxxxxx(CourseCategory root,List<CourseCategory> subList){
// 通过根节点去id和子节点的pid是否相等,如果相等的话,代表是当前根的子集
List<CourseCategory> childrenList = subList.stream().filter(category -> category.getPid().equals(root.getId()))
.sorted((a,b)->a.getSorted()-b.getSorted())
.collect(Collectors.toList());
// 如果你当前没一个子集,初始化一个空数组
if(!CollectionUtils.isEmpty(childrenList)) {//--------------------递归的临界条件------------------------
// 查询以后放回去
root.setChildrenList(childrenList);
// 再次递归构建即可 -----------------------------------------这里就是递归的开始----------------------------
childrenList.forEach(category -> busortxxxxxxxxxxxxxxxx(category,subList));
}else{
root.setChildrenList(new ArrayList<>());
}
}
z-tree
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
*{margin: 0;padding:0 }
ul li{list-style: none;margin:5px 25px;}
#app{background:#eee;padding:20px;}
</style>
</head>
<body>
<div id="app">
<h1>{{title}}</h1>
<tree :list="list"></tree>
</div>
<script src="vue.min.js"></script>
<script>
Vue.component("tree",{
props:{
list:Array
},
template:"<ul>\n" +
" <li v-for=\"(item,index) in list \" :key=\"index\" >\n" +
" <p @click='expand(item)'><img v-if=\"item.childrens\" :src='item.isexpand ?
\"minu.png\":\"add.png\" '> <img :src='item.type==2 ? \"file.png\":\"folder.png\" '>
{{item.title}} <span v-if=\"item.childrens\">({{item.childrens.length}})</span></p>\n" +
" <tree v-show='item.isexpand' :list=\"item.childrens\"></tree>\n" +
" </li>\n" +
" </ul>",
methods:{
expand:function(obj){
obj.isexpand = !obj.isexpand;
}
}
})
var vue = new Vue({
el:"#app",
data:{
title:"学相伴无限递归Tree",
list:[
{
"id":"1",
"title":"Java",
"descrciption":"Java",
"mark":"1",
"status":"1",
"type":"1",
"pid":"0",
"isexpand":true,
"createTime":"2021-10-18 14:19:05",
"updateTime":"2021-10-18 14:19:05",
"childrens":[
{
"id":"5",
"title":"Java面向对象",
"descrciption":"Java面向对象",
"mark":"1",
"status":"1",
"type":"1",
"pid":"1",
"isexpand":false,
"createTime":"2021-10-18 14:19:05",
"updateTime":"2021-10-18 14:19:05",
"childrens":null
},
{
"id":"6",
"title":"Spring",
"descrciption":"Spring",
"mark":"1",
"status":"1",
"type":"1",
"pid":"1",
"isexpand":false,
"createTime":"2021-10-18 14:19:05",
"updateTime":"2021-10-18 14:19:05",
"childrens":[
{
"id":"7",
"title":"SpringMvc",
"descrciption":"SpringMvc",
"mark":"1",
"status":"1",
"type":"2",
"pid":"6",
"isexpand":false,
"createTime":"2021-10-18 14:19:05",
"updateTime":"2021-10-18 14:19:05",
"childrens":null
},
{
"id":"8",
"title":"SpringBoot",
"descrciption":"SpringBoot",
"mark":"1",
"status":"1",
"type":"2",
"pid":"6",
"isexpand":false,
"createTime":"2021-10-18 14:19:05",
"updateTime":"2021-10-18 14:19:05",
"childrens":null
}
]
},
{
"id":"9",
"title":"Mybatis",
"descrciption":"Mybatis",
"mark":"1",
"status":"1",
"type":"1",
"pid":"1",
"isexpand":false,
"createTime":"2021-10-18 14:19:05",
"updateTime":"2021-10-18 14:19:05",
"childrens":[
{
"id":"10",
"title":"MybatisPlus",
"descrciption":"MybatisPlus",
"mark":"1",
"status":"1",
"type":"1",
"pid":"9",
"isexpand":false,
"createTime":"2021-10-18 14:19:05",
"updateTime":"2021-10-18 14:19:05",
"childrens":null
}
]
}
]
},
{
"id":"2",
"title":"Js",
"descrciption":"Js",
"mark":"1",
"status":"1",
"type":"1",
"pid":"0",
"isexpand":false,
"createTime":"2021-10-18 14:19:05",
"updateTime":"2021-10-18 14:19:05",
"childrens":[
{
"id":"11",
"title":"nodejs",
"descrciption":"MybatisPlus",
"mark":"1",
"status":"1",
"type":"1",
"pid":"2",
"isexpand":false,
"createTime":"2021-10-18 14:19:05",
"updateTime":"2021-10-18 14:19:05",
"childrens":null
}
]
},
{
"id":"3",
"title":"Go",
"descrciption":"Go",
"mark":"1",
"status":"1",
"type":"1",
"pid":"0",
"isexpand":false,
"createTime":"2021-10-18 14:19:05",
"updateTime":"2021-10-18 14:19:05",
"childrens":null
},
{
"id":"4",
"title":"Python",
"descrciption":"Python",
"mark":"1",
"status":"1",
"type":"1",
"pid":"0",
"isexpand":false,
"createTime":"2021-10-18 14:19:05",
"updateTime":"2021-10-18 14:19:05",
"childrens":null
}
]
}
})
</script>
</body>
</html>