html
<el-table-column label="上下架" width="100px" align="center">
<template slot-scope="{row}">
<el-switch
v-model="row.shelf_state"
class="switch"
:active-value="1"
:inactive-value="0"
active-text="上架"
inactive-text="下架"
@change="handleStatusChange($event,row)"
/>
</template>
</el-table-column>
css
<style>
/* switch按钮样式 */
.switch .el-switch__label {
position: absolute;
display: none;
color: #fff !important;
}
/*打开时文字位置设置*/
.switch .el-switch__label--right {
z-index: 1;
}
/* 调整打开时文字的显示位子 */
.switch .el-switch__label--right span{
margin-right: 9px;
}
/*关闭时文字位置设置*/
.switch .el-switch__label--left {
z-index: 1;
}
/* 调整关闭时文字的显示位子 */
.switch .el-switch__label--left span{
margin-left: 9px;
}
/*显示文字*/
.switch .el-switch__label.is-active {
display: block;
}
/* 调整按钮的宽度 */
.switch.el-switch .el-switch__core,
.el-switch .el-switch__label {
width: 70px !important;
margin: 0;
}
</style>
js
handleStatusChange(data, row) {
stateUpdata({ id: row.id, shelf_state: data }).then(response => {
if (response.code === 20000) {
this.$message({
type: '成功',
message: '操作成功!'
})
} else {
this.$message({
type: '失败',
message: '操作失败!'
})
}
})
},