vue封装表格实现拓展行展开行

基于vxe表格封装

 

<template>
    <div class="main">
        <vxe-table
            ref="xTable"
            :data="tableData"
            @current-change="toggleRowExpansion"
            @cell-click="handleClickCell"
        >
            <vxe-column type="seq" width="60"></vxe-column>
            <vxe-column field="sex" title="Sex"></vxe-column>
            <vxe-column field="age" title="Age"></vxe-column>
            <vxe-column type="expand" width="1" class-name="expand">
                <template #content="{ row }">
                    <div class="pd-10px">
                        <el-button
                            type="primary"
                            size="small"
                            icon="el-icon-edit"
                        >
                            编辑</el-button
                        >
                    </div>
                </template>
            </vxe-column>
        </vxe-table>
    </div>
</template>

<script>
export default {
    data() {
        return {
            tableData: [
                {
                    id: 10001,
                    name: 'Test1',
                    role: 'Develop',
                    sex: '0',
                    age: 28,
                    address: 'test abc',
                },
                {
                    id: 10002,
                    name: 'Test2',
                    role: 'Test',
                    sex: '1',
                    age: 22,
                    address: 'Guangzhou',
                },
                {
                    id: 10003,
                    name: 'Test3',
                    role: 'PM',
                    sex: '0',
                    age: 32,
                    address: 'Shanghai',
                },
                {
                    id: 10004,
                    name: 'Test4',
                    role: 'Designer',
                    sex: '1',
                    age: 23,
                    address: 'test abc',
                },
                {
                    id: 10005,
                    name: 'Test5',
                    role: 'Develop',
                    sex: '1',
                    age: 30,
                    address: 'Shanghai',
                },
                {
                    id: 10006,
                    name: 'Test6',
                    role: 'Designer',
                    sex: '1',
                    age: 21,
                    address: 'test abc',
                },
            ],
        };
    },
    mounted() {},
    methods: {
        toggleRowExpansion({ row, column }) {
            // 清除扩展列
            this.$refs.xTable.clearRowExpand();
            if (row) {
                let expands = [row];
                // 开启扩展列
                this.$refs.xTable.setRowExpand(expands, true);
            }
            this.$nextTick(() => {
                this.checkExpand();
            });
        },
        checkExpand() {
            let expand = this.$el.querySelector('.vxe-body--expanded-column');
            // 如果扩展没有内容直接隐藏
            if (expand.querySelector('.pd-10px').children.length == 0) {
                expand.style['border-bottom'] = 'none';
                expand.style['display'] = 'none';
            }
        },
        // 点击行事件
        handleClickCell({ row, rowIndex, column }) {
            this.toggleRowExpansion({ row, column });
        },
    },
};
</script>
<style lang="scss" scoped></style>

简单封装只实现了,拓展行的功能...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值