el-table视图不更新问题

el-table嵌套el-upload和el-input时,表格数据已更新,但视图不更新。

使用this.$set设置单个数据或单行数据部分情况无效,

el-input更新数据解决方案:换成原生input框,设置成和el-input类似的样式就可以了(但不知为何

注:tableRes是整个表格绑定的data数据

  <el-table-column label="价格" prop="price">
       <template v-slot="scope">
              <!-- <el-input
                @input="priceChange(tableRes, scope.$index, scope.row)"
                v-model="scope.row.price"
                style="width:100px"
                size="small"
              ></el-input> -->
              <input
                type="number"
                class="original-input"
                @input="priceChange(tableRes, scope.$index, scope.row)"
                v-model="scope.row.price"
                style="width:100px"
              />
       </template>
  </el-table-column>
 priceChange(table, index, row) {
      //console.log(index)
      console.log(row.price)

      //this.tableRes[index].price = row.price
      this.$set(table, index, row)
      //this.$forceUpdate()
    },

el-upload更新数据解决方案:给el-table绑定一个key。更新数据时将绑定的那个值取反(也不知道为什么能行)

<el-table
          border
          :key="isUpdate"
          class="dynamic-table"
          ref="dynamic-table"
          :data="tableRes"
        >
</el-table>

<!-- isUpdate: false --!>
<el-table-column label="图片">
            <template v-slot="scope">
              <el-upload
                :headers="token"
                accept=".jpg,.png"
                :show-file-list="false"
                :file-list="scope.row.price_image"
                :action="imgUrl"
                list-type="picture"
                :on-success="
                  (response, file, fileList) =>
                    specImgSuccess(
                      response,
                      file,
                      fileList,
                      scope.$index,
                      tableRes,
                      scope.row
                    )
                "
                name="image"
              >
                <img
                  v-if="
                    scope.row.price_image && scope.row.price_image.length !== 0
                  "
                  :src="scope.row.price_image[0].url"
                  class="avatar"
                  width="50"
                />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </template>
          </el-table-column>
specImgSuccess(response, file, fileList, index, table, row) {
      console.log(row)
      //   this.tableRes[index].price_image = []
      this.$set(this.tableRes[index], 'price_image', [])
      if (response.code === 0) {
        //this.tableRes[index].price_image.push({ url: response.data.url })
        this.$set(this.tableRes[index], 'price_image', [
          { url: response.data.url, name: '??' },
        ])
        this.isUpdate = !this.isUpdate
        //  this.$set(this.tableRes, index, row)
      } else {
        this.$message.error(response.message)
      }

      this.$forceUpdate()
    },

最后测试效果

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
如果你使用的是 Element UI 的 `el-table` 组件,并且发现在对数据进行增删改操作后,视图没有自动更新,可以尝试以下方法: 1. 使用 `this.$set` 方法更新数据 在 Vue 中,如果你对一个数组或对象进行了增删改操作,Vue 是无法检测到这个操作的。因此,需要使用 `this.$set` 方法来手动更新数据。 例如,如果你要更新 `tableData` 数组中的第一项数据: ```javascript this.$set(this.tableData, 0, { name: 'new name', age: 20 }); ``` 这个方法会告诉 Vue,你修改了 `tableData` 数组中的第一项数据,需要重新渲染视图。如果你直接修改了这个数组中的某一项数据,Vue 是无法检测到这个修改的。 2. 在 `el-table-column` 组件上使用 `prop` 属性 如果你使用了 `el-table-column` 组件来渲染表格列,需要在组件上使用 `prop` 属性,指定渲染的数据字段。 例如: ```html <el-table-column label="姓名" prop="name"></el-table-column> <el-table-column label="年龄" prop="age"></el-table-column> ``` 这样,当你对 `tableData` 数组中的数据进行增删改操作后,视图就会自动更新。这是因为 `el-table-column` 组件会监听 `prop` 属性对应的数据字段,一旦数据发生变化,就会重新渲染视图。 以上两种方法,可以分别解决不同的场景下 `el-table` 视图更新问题。如果以上两种方法都无法解决你的问题,可以提供更详细的代码和数据,以便更好地定位问题
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值