在列表中运用开关滑动,但是选中一个都跟着变化,但是可以控制台打印出正确的此条id
若把改变状态的注释代码解开则刷新页面后的list返回数据是对的,但是页面显示为都开或都关
<el-table :data="list" v-loading="listLoading" element-loading-text="拼命加载中" border fithighlight-current-row>
<el-table-column align="center" label="状态">
<template slot-scope="scope">
<el-switch v-model="value" on-color="#00A854" off-color="#F04134"
@change="changeState(scope.$index,scope.row)">
</el-switch>
</template>
</el-table-column>
</el-table>
list: [], //表格的数据
value: '', //开关
tempUser: { //参数
id: '',
state: '' //状态
},
//改变状态
changeState(index, row) {
for (let i = 0; i < this.list.length; i++) {
if (this.list[i].id == row.id) {
console.log(this.value)//变化正常
console.log(row.id)//变化正常
}
}
this.tempUser.id =row.id
// if (this.value == true) {
// this.tempUser.state = 0
// } else {
// this.tempUser.state = 1
// }
// request({
// url: "api/shop/state",
// method: "POST",
// data: {
// id: this.tempUser.id,
// state: this.tempUser.state
// },
// }).then(data => {})
},
getList() {
//查询列表
this.listLoading = true;
request({
url: "api/shop/list",
method: "POST",
data: this.listQuery,
}).then(data => {
this.listLoading = false;
this.list = data.info.content;
for (var i = 0; i < this.list.length; i++) {
if (this.list[i].state == 0) {
this.value = true
} else {
this.value = false
}
}
})
},
解决
<el-table-column align="center" label="状态">
<template slot-scope="scope">
<el-switch v-model="scope.row.state" on-color="#00A854" off-color="#F04134"
@change="changeState(scope.$index,scope.row)" :active-value="1" :inactive-value="0">
</el-switch>
</template>
</el-table-column>
//改变状态
changeState(index, row) {
this.tempUser.id = row.id
this.tempUser.state = row.state
request({
url: "api/shop/state",
method: "POST",
data: {
id: this.tempUser.id,
state: this.tempUser.state
},
}).then(data => {
// this.getList()
})
},