效果图:
1.表格代码,给el-waitch加上change事件
<el-table-column
prop="status"
label="状态"
align="center"
width="150">
<template slot-scope="scoped">
<el-switch
active-color="#13ce66"
inactive-color="#ff4949"
v-model="scoped.row.status"
@change="changeStatus($event,scoped.row,scoped.$index)">
</el-switch>
</template>
</el-table-column>
注意:使用时在 v-model=“scope.row.字段”可以把后端返回的开关数据展示在表格中
2.调接口。
changeStatus(e,row,index){
console.log(e,row,index) //e返回状态,row当前行数据,index下标
let pramas={
userId:Number(this.userInfo.id),//操作人id
status:row.status,//启用状态:false-未启用,true-启用
worksClass:Number(row.worksClass),// 作品类别
worksClassDonationRuleId:Number(row.id) 当前启用/禁用的记录id
}
this.$api.operateMgmt.donationruleenable(pramas)
.then(res =>{
if(res.code == 200){
this.$message({
message: '开启成功,请刷新页面',
type: 'success'
});
}else{
this.$message.error('当前记录是唯一启用的规则,不可禁用');
}
})
},
重点:方法名(e,row,index) //e返回状态true或false,row当前行数据,index下标
打印出的数据
通过以上两步就可以实现效果图。