elementUI的table表格改变数据不更新问题解决

导读:本篇文章讲解 elementUI的table表格改变数据不更新问题解决,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

需求:
table表格中 给按钮添加10秒倒计时
点击按钮后将按钮改为禁止点击状态,10秒后恢复正常
如图:
在这里插入图片描述
本以为是一个很简单的需求,直接就写了,结果发现没效果,最后通过打印该行数据发现: 行数据值已经改变了 但是页面上还是false
在这里插入图片描述
错误代码:
在这里插入图片描述

问题原因:在 Vue 实例创建时,以及 data 赋值时 btnDisabled并未声明,因此就没有被Vue转换为响应式的属性,自然就不会触发视图的更新。

解决方法:

使用:key 或者 v-if
修改绑定在 table 上面的 key 值,可以触发 table 的重新渲染,这样就可以把修改后的 data 在表格里面更新渲染。

修改后代码:

    <el-table
      ref="table"
      v-loading="crud.loading"
      :header-cell-style="{ color: '#FFF', background: '#333' }"
      :cell-style="{ color: '#FFF', background: '#333' }"
      :data="crud.data"
      style="width: 100%"
      :max-height="520"
      :default-sort="{ prop: 'updatetime', order: 'descending' }"
      @sort-change="sortChange"
      :key="itemKey"
    >
      <template slot="empty">
        <span style="color: #969799">{{ $t("NeoLight.empty") }}</span>
      </template>
      <el-table-column prop="reqCode" :sortable="true" label="设备" />
      <el-table-column prop="userCode" :sortable="true" label="消息" />
      <el-table-column prop="apiType" :sortable="true" label="类型">
        <template slot-scope="scope">
          <span v-if="scope.row.apiType == 2">转储单入库</span>
          <span v-else-if="scope.row.apiType == 3">排程发料</span>
          <span v-else-if="scope.row.apiType == 5">入库上架</span>
          <span v-else-if="scope.row.apiType == 6">出库下架</span>
          <span v-else-if="scope.row.apiType == 11">订单发料完成</span>
          <span v-else>{{ scope.row.apiType }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="$t('storagePos.operation')"
        width="150px"
        align="center"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            :disabled="scope.row.btnDisabled"
            icon="el-icon-tickets"
            size="mini"
            type="primary"
            @click="output(scope.$index, scope.row)"
          >
            重发
          </el-button>
        </template>
      </el-table-column>
    </el-table>
 data() {
    return {
      itemKey: 0,
    };
  },
 methods:{
         // 重发
    output(index, row) {
      row.btnDisabled = true;
      this.itemKey++;
      //调用倒计时方法 key 自动加一
      this.timers = setTimeout(() => {
        row.btnDisabled = false;
        this.itemKey++;
      }, 10000);
      reSend(row.reqCode).then((res) => {
        if (res.code === 0) {
          this.$infoMsg.showInfoMsg(window.vm.$i18n.t("tips.issue"), this);
        } else {
          this.$infoMsg.showErrorMsg(res.msg, this);
        }
      });
    },
}

这样就可以实现功能啦~

参考链接:
https://www.jb51.net/article/238665.htm

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/79186.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!