el-table 改数据源的值但是不刷新

问题:公司项目中使用Vue+elementUI,在使用axios加载数据的时候发现el-table里的数据没有刷新出来

页面代码:

                     <!--绑定数据源-->
                    <el-table
                            :data="form.goods"
                            style="width: 100%"
                            max-height="250">
                        <el-table-column
                                prop="goods.id"
                                label="货品ID"
                                width="120">
                        </el-table-column>
                        <el-table-column
                                prop="goods.goodsNo"
                                label="货品编码"
                                width="120">
                        </el-table-column>
                        <el-table-column
                                prop="goods.goodsName"
                                label="货品名称"
                                width="120">
                        </el-table-column>
                        <el-table-column
                                prop="goods.goodsSpec"
                                label="货品规格"
                                width="120">
                        </el-table-column>
                        <el-table-column
                                prop="goodsCount"
                                label="数量"
                                width="120">
                        </el-table-column>
                        <el-table-column
                                fixed="right"
                                label="操作"
                                width="120">
                            <template slot-scope="scope">
                                <el-button
                                        @click.native.prevent="deleteRow(scope.$index, form.goods)"
                                        type="text"
                                        size="small">
                                    移除
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>

数据定义:

data() {
            return {
                isSubmiting: false,
                isNew: true,
                isSynced: false,
                pageTitle: "",
                singlePacket: '',
                good: {
                    goods: {goodsNo: '', goodsSpec: '', goodsName: '', id: ''},
                    goodsCount: ''
                },
                form: {
                    billNo: '',
                    department: {
                        departmentCode: ''
                    },
                    goods: []//默认这里的数据是空的
                },
            }
        },

axios在created()里加载数据

created() {
            if (this.$route.params.returnTask) {
                this.form = this.$route.params.returnTask;
                Remote.getSurgeryTaskItems(this.form.id).then((res) => {
                    let status = res.data.status; 
                    if (status === 0) {
                        //在这里把后台返回的值给table的数据源,但是此时table没有刷新,打印输出this.form.goods是有值且正确的
                        this.form.goods = res.data.data;
                    } else {
                        this.$message.error("获取详细货品列表失败!");
                    }
                }).catch((error) => {
                    // this.form.goods.goodsNo=value;
                })
            } 
        }

解决过程

在搜索引擎看到很多人遇到相同的情况,有说用$set方法的,但是对于我这个案例不适用。也有说设置el-table的key=Math.random(),但是也无效。

                     <!--绑定数据源-->
                     <!--:key="Math.random()"也无效-->
                    <el-table
                            :data="form.goods"
                            :key="Math.random()"
                            style="width: 100%"
                            max-height="250">
                        <el-table-column
                                prop="goods.id"
                                label="货品ID"
                                width="120">
                        </el-table-column>
                        <el-table-column
                                prop="goods.goodsNo"
                                label="货品编码"
                                width="120">
                        </el-table-column>
                        <el-table-column
                                prop="goods.goodsName"
                                label="货品名称"
                                width="120">
                        </el-table-column>
                        <el-table-column
                                prop="goods.goodsSpec"
                                label="货品规格"
                                width="120">
                        </el-table-column>
                        <el-table-column
                                prop="goodsCount"
                                label="数量"
                                width="120">
                        </el-table-column>
                        <el-table-column
                                fixed="right"
                                label="操作"
                                width="120">
                            <template slot-scope="scope">
                                <el-button
                                        @click.native.prevent="deleteRow(scope.$index, form.goods)"
                                        type="text"
                                        size="small">
                                    移除
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>

之后我思考key的值在axios之后再赋值为随机值就行了,所以

解决

在table上的key绑定一个临时变量,然后在axio的回调赋值后再更新这个变量的值。

                     <!--绑定数据源-->
                    <el-table
                            :data="form.goods"
                            :key="itemkey"
                            style="width: 100%"
                            max-height="250">
                        <el-table-column
                                prop="goods.id"
                                label="货品ID"
                                width="120">
                        </el-table-column>
                        <el-table-column
                                prop="goods.goodsNo"
                                label="货品编码"
                                width="120">
                        </el-table-column>
                        <el-table-column
                                prop="goods.goodsName"
                                label="货品名称"
                                width="120">
                        </el-table-column>
                        <el-table-column
                                prop="goods.goodsSpec"
                                label="货品规格"
                                width="120">
                        </el-table-column>
                        <el-table-column
                                prop="goodsCount"
                                label="数量"
                                width="120">
                        </el-table-column>
                        <el-table-column
                                fixed="right"
                                label="操作"
                                width="120">
                            <template slot-scope="scope">
                                <el-button
                                        @click.native.prevent="deleteRow(scope.$index, form.goods)"
                                        type="text"
                                        size="small">
                                    移除
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>

数据定义:

data() {
            return {
                isSubmiting: false,
                isNew: true,
                isSynced: false,
                pageTitle: "",
                singlePacket: '',
                good: {
                    goods: {goodsNo: '', goodsSpec: '', goodsName: '', id: ''},
                    goodsCount: ''
                },
                form: {
                    billNo: '',
                    department: {
                        departmentCode: ''
                    },
                    goods: []
                },
                itemkey: ''//临时变量
            }
        },

逻辑代码

 created() {
            if (this.$route.params.returnTask) {
                this.form = this.$route.params.returnTask;
                this.isNew = false;
                this.isSynced = this.form.sent;
                Remote.getSurgeryTaskItems(this.form.id).then((res) => {
                    let status = res.data.status;
                    if (status === 0) {
                        this.form.goods = res.data.data;
                        this.itemkey = Math.random();//在这边给key赋随机值
                    } else {
                        this.$message.error("获取详细货品列表失败!");
                    }
                }).catch((error) => {
                    // this.form.goods.goodsNo=value;
                })
            }

参考链接:
https://segmentfault.com/q/1010000012001863

  • 5
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员大包包

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值