借鉴之学习——递归

在这里插入图片描述

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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值