首页 > 其他分享 >vue学习之-----组件递归调用

vue学习之-----组件递归调用

时间:2023-02-16 20:00:11浏览次数:36  
标签:vue return 递归 children id ruleDataList ----- obj 节点

1、关键点

2、父组件

<template>
    <div>
        <div class="btn-title">
            <el-button @click="addRule(ruleDataList.id)">添加父节点</el-button>
        </div>
        <RuleTable :pid="ruleDataList.id" :ruleData="ruleDataList.children" @delRule="delRule" @addRule="addRule"></RuleTable>
    </div>
</template>
<script>
import RuleTable from './RuleTable.vue'
export default {
    components: {
        RuleTable
    },
    data() {
        return {
            ruleDataList: {
                id: this.$nanoid(),//自定义id
                lvl: 1,//节点等级
                theme: '',//主题
                time: '',//次数
                children: []
            }
        }
    },
    methods: {
        addRule(id) {
            if (id === this.ruleDataList.id) {//>>>一级节点
                this.ruleDataList.children.push({
                    id: this.$nanoid(),//自定义id
                    lvl: 1,//节点等级
                    theme: '',//主题
                    time: '',//次数
                    children: []
                })
            } else {
                this.ruleDataList.children.map(e => {
                    if (e.id === id) {//>>>二级节点
                        let obj = JSON.parse(JSON.stringify(e))
                        obj.id = this.$nanoid();//自定义id
                        obj.lvl = 2
                        e.children.push(obj, {
                            id: this.$nanoid(),//自定义id
                            lvl: 2,//节点等级
                            theme: '',//主题
                            time: '',//次数
                            children: []
                        })
                    } else {
                        let flag = e.children.find(i => i.id === id)
                        if (flag) {//>>>三级节点
                            e.children.push({
                                id: this.$nanoid(),//自定义id
                                lvl: 2,//节点等级
                                theme: '',//主题
                                time: '',//次数
                                children: []
                            })
                        }
                    }
                })
            }
        },
        delRule(id) {
            this.ruleDataList.children = this.ruleDataList.children.filter(i => {
                // 如果是二级节点
                if (i.children && i.children.length > 0) {
                    i.children = i.children.filter(j => {
                        return j.id != id
                    })
                    return i
                }
                // 如果是一级节点
                return i.id != id
            }).map(k => {
                if (k.children && k.children.length === 1) {
                    let obj = JSON.parse(JSON.stringify(k.children[0]))
                    obj.lvl = 1
                    return obj //如果只有一个二级节点,则变为一级节点
                }
                return k //其余,则原样返回
            })
        }
    }
}
</script>
<style>
.btn-title {
    display: flex;
    justify-content: end;
    margin-bottom: 10px;
}
</style>

3、子组件

<template>
    <el-card style="margin-top:5px">
        <el-row>
            <el-col :span="2" align="center" v-if="ruleData.length > 1">
                <div class="left-top" :style="'height:' + halfHeight + 'px'"></div>
                <div class="left-center">组</div>
                <div class="left-bottom" :style="'height:' + halfHeight + 'px'"></div>
            </el-col>
            <el-col :span="22" :id="pid">
                <el-row v-for="item in ruleData" :key="item.id">
                    <RuleTable v-if="item.children.length > 0" :pid="item.id" :ruleData="item.children"
                        @delRule="delRule" @addRule="addRule"></RuleTable>
                    <el-form v-else>
                        <el-col :span="8" class="pd5">
                            <el-form-item prop="theme">
                                <el-input v-model="item.theme" placeholder="请输入主题"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8" class="pd5">
                            <el-form-item prop="time">
                                <el-input v-model="item.time" placeholder="请输入次数"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="6" class="pd5">
                            <el-button type="danger" @click="delRule(item.id)">删除</el-button>
                            <el-button type="primary" @click="addRule(item.id)">新增</el-button>
                        </el-col>
                    </el-form>
                </el-row>
            </el-col>
        </el-row>
    </el-card>
</template>
<script>
export default {
    name: 'RuleTable', //子组件的name名要和组件名一致
    props: {
        ruleData: {
            type: Array,
            default: () => []
        },
        pid: {
            type: String,
            default: () => ''
        }
    },
    data() {
        return {
            halfHeight: 0,//竖线的高度
        }
    },
    watch: {
        ruleData: {
            handler(val) {
                console.log("ruleData--val:", val)
                if (val) {
                    this.updateHeight()
                }
            },
            deep: true,
            immediate: true
        }
    },

    methods: {
        delRule(id) {
            this.$emit('delRule', id)
        },
        addRule(id) {
            this.$emit('addRule', id)
        },
        updateHeight() {
            this.$nextTick(() => {
                let leftHeight = document.getElementById(this.pid + '').offsetHeight
                console.log("实时高度--leftHeight:", leftHeight)
                this.halfHeight = (leftHeight - 21) / 2
            })
        }
    }
}
</script>
<style>
.left-top {
    width: 2px;
    background-color: #bbb;
}

.left-bottom {
    width: 2px;
    background-color: #bbb;
}

.pd5 {
    padding-left: 5px;
}
</style>

 

标签:vue,return,递归,children,id,ruleDataList,-----,obj,节点
From: https://www.cnblogs.com/zhaoyingzhen/p/17124093.html

相关文章

  • ssh-copy-id三步实现SSH无密码登录和ssh常用命令
    原文地址https://blog.csdn.net/liu_qingbo/article/details/78383892ssh-copy-id-p22-i~/.ssh/id_rsa.pubroot@远程ipssh-p'22''root@远程ip'产生如下代码,......
  • vue项目,使用query传参,页面显示重新刷新或回归后数据丢失
    1、将需要传输的数据使用  JSON.stringify()  转译成字符串形式进行传输  2、在需要接收的页面使用  JSON.parse()  将数据格式再转回来即可使用,且刷新回......
  • rn项目下载@ant-design/react-native时发生冲突
    rn项目,使用npmi@ant-design/react-native下载antd。下载依赖时报错: 如果你也遇到这个问题,直接告诉你结论,那就是最新的@ant-design/react-native5.0.3不支持react18......
  • springboot---多环境启动命令格式
    一、多环境命令启动maven插件中首先clean,再package打包,(修改字符集为UTF-8)使用cmd命令java-jars(Tab键自动补全) -spring.profiles.active=test启动项目  修改端......
  • spring-ImportSelector接口原理
    ImportSelector接口源码packageorg.springframework.context.annotation;importorg.springframework.core.type.AnnotationMetadata;/***Interfacetobeimple......
  • HIVE - HIVEQL学习笔记
    HiveLearningHIVEQL和MYSQL最为接近数据操作SHOWTABLESINcrm_integrationShowtables'dim_con*' DESCRIBEEXTENDEDcrm_integration.itg_fact_sales_orderDE......
  • Epoll原理解析--网卡接收数据说起
     转至 https://blog.csdn.net/armlinuxww/article/details/92803381 太重要了怕丢失,冒昧转一下  从网卡接收数据说起下边是一个典型的计算机结构图,计算机由CPU......
  • drf回顾,前端发展历史,vue介绍,第一个helloword,插值语法
    目录drf回顾,前端发展历史,vue介绍,第一个helloword,插值语法今日内容概要今日内容详细1drf回顾2前端发展历史3vue介绍4第一个helloworld5插值语法drf回顾,前端发展历史,vu......
  • 台阶-Nim游戏
    台阶-Nim游戏现在,有一个$n$级台阶的楼梯,每级台阶上都有若干个石子,其中第$i$级台阶上有$a_i$个石子$(i\geq1)$。两位玩家轮流操作,每次操作可以从任意一级台阶上拿......
  • 差分隐私-实现方法与性质
    实现方法与性质离散值域:随机回答在很多场合,回答是或者不是,本身就属于隐私数据。也就是说有些隐私是离散的数据。随机回答的局部差分隐私:考虑任意一个被调查者i。由于数......