当前位置:   article > 正文

elementUI的table表格改变数据不更新问题解决_element table数据变化后,表格不重新渲染

element table数据变化后,表格不重新渲染

需求:
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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
 data() {
    return {
      itemKey: 0,
    };
  },
  • 1
  • 2
  • 3
  • 4
  • 5
 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);
        }
      });
    },
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

这样就可以实现功能啦~

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/107604?site
推荐阅读
相关标签
  

闽ICP备14008679号