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、子组件RuleTable(递归调用)

<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>

4、效果

最新文章

  1. C#:获取设备电量相关信息
  2. java基础-泛型2
  3. AFNetworking简单用法
  4. ubuntu-E:Encountered a section with no Package: header的解决办法 (转)
  5. python + selenium相关事件和元素定位
  6. wamp的配置
  7. matlab:clear,close,clc
  8. ORA-01109:数据库未打开
  9. convert NameValueCollection/Dictionary&lt;string, object&gt; to JSON string
  10. 对JAVA动态代理的理解
  11. 0 Explore TreeView
  12. 解决 IE 不支持 document.getElementsByClassName() 的方法
  13. WIN32api总结
  14. c/c++ void 指针
  15. 【转】Android中EditText中的InputType类型含义与如何定义
  16. BZOJ 3218(a + b Problem-二分图套值域线段树)
  17. SQL入门学习3-数据更新
  18. Maven搭建struts2+spring+hibernate环境
  19. fatal: The remote end hung up unexpectedly
  20. PHP和Python如何选择?或许可以考虑这三个问题

热门文章

  1. log4j 完美版配置
  2. ECharts 提示框组件Tooltip属性大全(包含文本注释)
  3. STM32F4库函数初始化系列:DMA串口接收
  4. WAVE音频文件格式及其64位扩展格式的简要介绍
  5. 【ASP.NET Core】标记帮助器——元素筛选
  6. JavaScript原型和原型链?有什么特点?
  7. LeetCode-2055 蜡烛之间的盘子 及库函数 lower_bound 和 upper_bound学习使用
  8. Occlusion(遮挡剔除)
  9. Vulnhub:Five86-2靶机
  10. Prometheus学习笔记之设置存储时间为30天不生效