递归树形结构,点击上一步选中上一级

319 阅读1分钟

使用了两个递归函数

        let that = this;

        function checkPrevious(data,parent){

            data.forEach((item,index)=>{

                if(item.id== that.activeID){

                    if(index != 0){

                        if(data[index-1].children.length !==0){

                            that.findID(data[index-1].children[data[index-1].children.length-1]);

                        }else{

                            // console.log('没有子节点')

                            that.activeID = data[index-1].id;

                            that.content = data[index-1].content;

                        }

                    }else{

                        if(parent){

                        that.activeID = parent.id;

                        that.content = parent.content;

                        }else{

                            that.isPre = false;

                        }

                        // console.log('上一级没有子节点')

                        // console.log(parent,'上一级没有子节点parent')

                    }

                }else{

                    // console.log(data, '-----data')

                    checkPrevious(item.children,item)

                }

            })

        };

        checkPrevious(data)



 findID(data){

        if(data.children.length != 0){

            this.findID(data.children[data.children.length-1])

        }else{

            this.activeID = data.id;

            this.content = data.content;

        }

    },