vue递归怎样实现表格一级数据和二级显示相同
Admin 2022-09-07 群英技术资讯 1738 次浏览
很多朋友都对“vue递归怎样实现表格一级数据和二级显示相同”的内容比较感兴趣,对此小编整理了相关的知识分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获,那么感兴趣的朋友就继续往下看吧!说明: el-table有一个表格一级数据和二级数据显示的是一样的,像这种就可以用递归实现。
数据结构是这样子的:
tableData:[{
name: "Lucy",
age: 18,
mobile: "11111111111",
type: 1,
friends:[{
name: "Lucy-friend1",
age: 16,
mobile: "11111111111"
},{
name: "Lucy-friend2",
age: 16,
mobile: "11111111111"
}]
}]
像以上这种数据结构想要如下图一样显示:

我用的是el-table组件,所以单独建了一个组件用于表格递归显示。regionTableTemplate里expend中用到的递归显示二级数据
代码如下:
<template>
<el-table :data="tableData" class="table-sub" ref="regionTable" :show-header="showHeader" :row-class-name="isShowExpend">
<el-table-column type="expand">
<template slot-scope="scope">
<template v-if="scope.row.friends">
<regionTableTemplate class="in-table" :tableData="scope.row.friends" :showHeader="false"></regionTableTemplate>
</template>
</template>
</el-table-column>
<el-table-column prop="name" label="姓名"></el-table-column>
<el-table-column prop="age" label="年龄"></el-table-column>
<el-table-column prop="mobile" label="手机号"></el-table-column>
<el-table-column label="操作" width="220">
<template slot-scope="scope">
<el-button type="text">详情</el-button>
<el-button type="text"> 创建可用区</el-button>
<el-button :disabled="scope.row.type === 1?true:false" type="text">删除</el-button>
</template>
</el-table-column>
</el-table>
</template>
<script>
export default {
name: "regionTableTemplate",
props:{
tableData: Array,
showHeader: Boolean
},
methods:{
//展开行 用于没有friends数组即没有子数据
isShowExpend(row, index) {
if (row.row.friends&&row.row.friends) {
return ''
} else {
return 'hide-expand'
}
}
}
}
</script>
<style>
</style>
父组件调用
<template>
<div id="ops-region-wrapper">
<div class="ops-list-wrapper">
<table-vue :tableData="tableData" :showHeader="true" @showRegionFlag="showRegionFlag" @showDelDialog="showDelDialog"></table-vue>
<Pager :pages='pages' @changeCurrent='changeCurrent'></Pager>
</div>
</div>
</template>
<script>
import Pager from '@/components/Pager.vue';
import tableVue from './components/table.vue';
export default {
components:{
Pager,
tableVue
},
data() {
return {
tableData: [{
name: "Lucy",
age: 18,
mobile: "11111111111",
type: 1,
friends:[{
name: "Lucy-friend1",
age: 16,
mobile: "11111111111"
},{
name: "Lucy-friend2",
age: 16,
mobile: "11111111111"
}]
}],
pages: {//分页
showItem: 15,
total: 0,
currentPage: 1
},
}
},
methods: {
//分页
changeCurrent(val) {
this.pages.currentPage = val;
}
},
mounted() {
this.getList();
}
}
</script>
<style lang="scss">
#ops-region-wrapper{
.el-table {
margin-top: 15px;
tr .el-table__expanded-cell {
padding: 0;
border-bottom: none;
}
.el-table__expand-icon {
width: 18px;
height: 18px;
line-height: 16px;
background: rgba(54, 134, 255, 0.2);
color: #3686ff;
border: 1px solid #3686FF;
box-sizing: border-box;
border-radius: 50%;
transform: scale(0.8);
i {
font-weight: bold;
font-size: 12px;
left: 48%;
}
}
.el-table__expand-icon--expanded{
transform: rotate(90deg) scale(0.8);
}
.hide-expand .el-table__expand-column>.cell {
display: none;
}
}
.in-table{
&::before{
border: none;
}
margin: 0;
padding: 0;
.el-table__expand-column>.cell,.el-table__expanded-cell {
display: none;
}
}
.el-button--text {
margin: 0 20px 0 0;
@include pageFont(12px, #006EFF, 400, "PingFangSC-Regular, PingFang SC", 17px);
&.is-disabled{
color: #979797;
}
}
.is-click {
cursor: pointer;
text-decoration: underline;
text-decoration-color: #3686FF;
@include pageFont(12px, #3686FF, 400, "PingFangSC-Regular, PingFang SC", 17px);
}
}
</style>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
assert模块提供了简单的断言测试功能,主要用来内部使用,也可能require(‘assert’)后在外部进行使用。 assert模块的API为locked状态,也就是说,这个模块的API将不会再有添加或修改了。 Assert模块方法列表: assert(value[,message]) assert.deepEqual(actual
Javascript查看大图功能代码实现,点击小图片可以查看大图,就是把大图放置在顶层(z-index大于当前页面的),并且还可以加一些额外的比如透明度什么的。
斐波那契数列来源于兔子繁殖问题,所以也叫兔子序列,下面这篇文章主要给大家介绍了关于JavaScript输出斐波那契数列的实现方法,需要的朋友可以参考下
关于JavaScript命名约定下文有详细的介绍,小编觉得挺实用的,对大家学习或工作或许有帮助,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
TypeScript团队发布了TypeScript 4.1,其中包括功能强大的模板字面量类型、映射类型的键重映射以及递归条件类型。下面本篇文章就来带大家了解一下TypeScript中的模板字面量类型,希望对大家有所帮助!
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008