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()
},
最后测试效果