ElementUI的expand table 点击展开,属性变化未引起表格响应

2022-09-21 08:08:46
<template>
    <el-table
            :data="tableData5"
            style="width: 100%"
            row-key="id"
            :expand-row-keys="expandKeys"
            @expand-change="expandChange">
        <el-table-column label="商品 ID"prop="id"></el-table-column>
        <el-table-column label="商品名称" prop="name"></el-table-column>
        <el-table-column label="描述" prop="desc"></el-table-column>
        <el-table-column type="expand">
            <template >
                <el-table v-loading="loading" :data="subTableData">
                    <el-table-column label="所属店铺" prop="shop"></el-table-column>
                    <el-table-column label="店铺 ID" prop="shopId"></el-table-column>
                    <el-table-column label="商品分类" prop="category"></el-table-column>
                    <el-table-column label="店铺地址" prop="address"></el-table-column>
                </el-table>
            </template>
        </el-table-column>
    </el-table>
</template>


<script>
    import axios from "axios"
    var Mock = require('mockjs');

    Mock.mock('/subtable/data',[{
        category: '江浙小吃、小吃零食',
        address: '上海市普陀区真北路',
        shop: '王小虎夫妻店',
        shopId: '10333'
    }
    ])

    export default {
        data() {
            return {
                expandKeys:[],/** 新增 **/
                loading:true,
                subTableData:[],
                tableData5: [{
                    id: '12987122',
                    name: '好滋好味鸡蛋仔',
                    desc: '荷兰优质淡奶,奶香浓而不腻',
                }, {
                    id: '12987123',
                    name: '好滋好味鸡蛋仔',
                    desc: '荷兰优质淡奶,奶香浓而不腻',
                }, {
                    id: '12987125',
                    name: '好滋好味鸡蛋仔',
                    desc: '荷兰优质淡奶,奶香浓而不腻',
                }]
            }
        },
        methods: {
            expandChange(row, expandedRows) {
                if(this.expandKeys.indexOf(row.id)>=0){
                    //收起当前行
                    this.expandKeys.shift()
                    return;
                }
                let _this = this
                //恢复默认值
                _this.loading = true
                _this.subTableData = []
                //加载数据
                axios.get('/subtable/data')
                    .then(function (res) {
                        _this.subTableData = res.data
                        _this.loading = false
                        _this.expandKeys.shift()            /** 新增 **/
                        _this.expandKeys.push(row.id)       /** 新增 **/
                    }).catch(function (error) {
                    console.log(error);
                });
                if (expandedRows.length > 1) {
                    //只展开当前选项
                    expandedRows.shift()
                }
            }
        }
    }
</script>
  • 作者:四块五毛六分二
  • 原文链接:https://blog.csdn.net/weixin_43869524/article/details/100019872
    更新时间:2022-09-21 08:08:46