vue el-table实现递归嵌套的示例代码


Posted in Vue.js onAugust 14, 2022

说明: 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"
	}]
}]

像以上这种数据结构想要如下图一样显示:

vue el-table实现递归嵌套的示例代码

我用的是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>

到此这篇关于vue el-table实现递归嵌套的示例代码的文章就介绍到这了,更多相关vue el-table递归嵌套内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Vue.js 相关文章推荐
Vue+scss白天和夜间模式切换功能的实现方法
Jan 05 Vue.js
vue3中轻松实现switch功能组件的全过程
Jan 07 Vue.js
Vue项目打包部署到apache服务器的方法步骤
Feb 01 Vue.js
vue如何使用rem适配
Feb 06 Vue.js
vue 使用饿了么UI仿写teambition的筛选功能
Mar 01 Vue.js
Vue多选列表组件深入详解
Mar 02 Vue.js
vue实现水波涟漪效果的点击反馈指令
May 31 Vue.js
详解gantt甘特图可拖拽、编辑(vue、react都可用 highcharts)
Nov 27 Vue.js
vue route新窗口跳转页面并且携带与接收参数
Apr 10 Vue.js
vue项目proxyTable配置和部署服务器
Apr 14 Vue.js
vue使用watch监听属性变化
Apr 30 Vue.js
el-table-column 内容不自动换行的解决方法
Aug 14 Vue.js
vue实现input输入模糊查询的三种方式
Aug 14 #Vue.js
vue本地构建热更新卡顿的问题“75 advanced module optimization”完美解决方案
Aug 05 #Vue.js
Vue深入理解插槽slot的使用
Aug 05 #Vue.js
vue3 自定义图片放大器效果的示例代码
Jul 23 #Vue.js
vue递归实现树形组件
Jul 15 #Vue.js
VUE递归树形实现多级列表
Jul 15 #Vue.js
Vue2项目中对百度地图的封装使用详解
You might like
php miniBB中文乱码问题解决方法
2008/11/25 PHP
PHP图片自动裁切应付不同尺寸的显示
2014/10/16 PHP
PHP图像处理之使用imagecolorallocate()函数设置颜色例子
2014/11/19 PHP
php写app接口并返回json数据的实例(分享)
2017/05/20 PHP
gearman中worker常驻后台,导致MySQL server has gone away的解决方法
2020/02/27 PHP
javascript 折半查找字符在数组中的位置(有序列表)
2010/12/09 Javascript
jquery ready(fn)事件使用介绍
2013/08/21 Javascript
js动态创建标签示例代码
2014/06/09 Javascript
jQuery遍历json中多个map的方法
2015/02/12 Javascript
轻松使用jQuery双向select控件Bootstrap Dual Listbox
2015/12/13 Javascript
深入探究JavaScript中for循环的效率问题及相关优化
2016/03/13 Javascript
jquery取消事件冒泡的三种方法(推荐)
2016/05/28 Javascript
Vue.js自定义指令的用法与实例解析
2017/01/18 Javascript
vue响应式系统之observe、watcher、dep的源码解析
2019/04/09 Javascript
使用vue完成微信公众号网页小记(推荐)
2019/04/28 Javascript
微信小程序页面间跳转传参方式总结
2019/06/13 Javascript
微信小程序在text文本实现多种字体样式
2019/11/08 Javascript
Python中为feedparser设置超时时间避免堵塞
2014/09/28 Python
python中pygame针对游戏窗口的显示方法实例分析(附源码)
2015/11/11 Python
python 自动化将markdown文件转成html文件的方法
2016/09/23 Python
Python实现FTP上传文件或文件夹实例(递归)
2017/01/16 Python
python 实现自动远程登陆scp文件实例代码
2017/03/13 Python
Pandas 合并多个Dataframe(merge,concat)的方法
2018/06/08 Python
Python操作redis实例小结【String、Hash、List、Set等】
2019/05/16 Python
django fernet fields字段加密实践详解
2019/08/12 Python
Python字符串格式化输出代码实例
2019/11/22 Python
Python 爬虫的原理
2020/07/30 Python
python中openpyxl和xlsxwriter对Excel的操作方法
2021/03/01 Python
HTML5中FileReader接口使用方法实例详解
2017/08/26 HTML / CSS
高中生学习的自我评价
2013/12/14 职场文书
社团活动总结范文
2014/04/26 职场文书
艾滋病宣传活动总结
2014/05/08 职场文书
运动会1000米加油稿
2015/07/21 职场文书
智慧人生:永远不需要向任何人解释你自己
2019/08/20 职场文书
导游词之天津古文化街
2019/11/09 职场文书
python 爬取哔哩哔哩up主信息和投稿视频
2021/06/07 Python