需求分析:Vue 的双向数据绑定,使得修改数据后,视图就会跟着发生更新,比如对数组进行增加元素、切割等操作。然而直接通过下标修改数组内容后,视图却不发生变化,因此,我们要想通过index下标来改变数组来达到目的是行不通的。这时我们可以使用Vue.set(对象,属性,值)或 this.$set(对象,属性,值)来改变数组
代码如下:
<el-table-column align="center" label="访问分数" min-width="300px">
<template slot-scope="scope">
<div v-if="scope.row.flag">
<el-col :span="14" style="padding-left:0px;padding-right:0px">
<el-input-number
v-model="scope.row.api_importance_value"
controls-position="right"
:min="-1"
:max="100"
@change="handleChange1"
/>
</el-col>
<el-col :span="6" style="padding-left:0px;padding-right:0px;margin-top:10px;margin-left:20px">
<span style="color:blue ;cursor:pointer;margin-top:10px;" @click="save(scope.$index,scope.row)"><i class="el-icon-circle-check" /></span>
<span style="color:blue ;cursor:pointer;margin-top:10px;margin-left:10px" @click="remove(scope.$index,scope.row)"> <i class="el-icon-circle-close" /></span>
</el-col>
</div>
<span v-else class="spanan">{{ scope.row.api_importance_value }}<i class="el-icon-edit-outline" style="margin-left:20px;color:blue ;cursor:pointer;" @click.prevent="editShow(scope.$index,scope.row)" /></span>
</template>
</el-table-column>
方法: this.$set(对象,属性,值)
//打开
editorShow(index, rows) {
this.$set(this.tableData[index], 'flag', true)
},
// 关闭
async remove(index, rows) {
this.$set(this.tableData[index], 'flag', false)
},
//保存
async saveeditor(index, rows) {
this.$set(this.tableData[index], 'flag', false)
},