element多選表格中使用Switch開關的實現
當在做后臺管理系統的時候,會用到用戶的狀態(tài)管理這個功能。一般后端給返回的類型都是整型0或1。此時想通過該狀態(tài)來禁用或者開啟此用戶。

在官網中給到active-value(switch 打開時的值),inactive-value(switch 關閉時的值),并且支持的類型有boolean / string / number。一般后端傳輸的都是以number類型。
active-value和inactive-value的值分別是字符串的1和0,如果你賦值為數字類型的 1 或 0是無法正常工作的,若賦值為數值類型,需這樣寫:
<el-switch v-model="status" :active-value="1" :inactive-value="0"> </el-switch>
一、 此時若想跟表格中匹配幾條數據就有幾個開關時需要這樣寫:
1、要拿到當前行的數據在template標簽中使用**slot-scope=“scope”就可以了
2、使用時在 v-model=“scope.row.字段”就可以把后端返回的開關數據展示在表格中
<el-table-column prop="disable" label="狀態(tài)" width="120">
<template slot-scope="scope">
<el-switch
v-model="scope.row.mg_state"
></el-switch>
</template>
</el-table-column>二、觸發(fā)時間調用后端接口,修改開關數據
給Switch 開關加上@change="changeStatus($event, scope.row, scope.$index)"
<el-table-column prop="disable" label="狀態(tài)" width="120">
<template slot-scope="scope">
<el-switch
v-model="scope.row.mg_state"
@change="changeStatus($event, scope.row, scope.$index)" //加上觸發(fā)時間
:active-value="scope.row.disable == 1" //根據后端返回的數據綁定1為開的狀態(tài)
:inactive-value="scope.row.disable == 0" //根據后端返回的數據綁定0為關的狀態(tài)
></el-switch>
</template>
</el-table-column>在methods中寫上方法,去調接口方法名(e,row,index) //e返回狀態(tài)true或false,row當前行數據,index下標
changeStatus(e, row, index) {
//e返回狀態(tài),row當前行數據,index下標
console.log(e, row, index);
let userId = row.userId;
let disable = row.disable;
axios.get(`http://xxxxx/xx?userId=${userId}&disable=${disable}`).then((res)=>{
console.log(res);
})
}到此這篇關于element多選表格中使用Switch開關的實現的文章就介紹到這了,更多相關element Switch開關內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vee-validate vue 2.0自定義表單驗證的實例
今天小編就為大家分享一篇vee-validate vue 2.0自定義表單驗證的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08

