代码1:
<!-- table表格 -->
<el-table :data="tableData">
<el-table-column label="是否关闭" align="center">
<template slot-scope="scope">
<el-switch
v-model="scope.row.showState"
active-color="#52C4CD"
inactive-color="#DCDFE6"
@change="showClose(scope.$index, scope.row)"
:active-value="true"
:inactive-value="false"
></el-switch>
</template>
</el-table-column>
</el-table>
代码2:
//是否关闭
showClose(index,row){
let flag = row.showState //保存点击之后v-modeld的值(true,false)
row.showState = !row.showState //保持switch点击前的状态
this.$confirm(title, '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
flag ? row.showState = true : row.showState = false // 这一步很重要,row.showState会根据flag的值开启或关闭开关
this.$message({type: 'success', message: '修改成功!'})
}).catch(() => {
this.$message({type:'info',showClose: true,message:'已取消修改!'})
});
},
该文章展示了在Vue.js应用中如何在表格内使用el-switch组件进行状态切换,并结合$confirm弹窗确认操作,以及处理数据的双向绑定。在switch状态改变时,先保存原有状态,然后通过用户确认来决定是否真正更新数据。


2724

被折叠的 条评论
为什么被折叠?



