element多選表格中使用Switch開關的實現(xiàn)
當在做后臺管理系統(tǒng)的時候,會用到用戶的狀態(tài)管理這個功能。一般后端給返回的類型都是整型0或1。此時想通過該狀態(tài)來禁用或者開啟此用戶。
在官網(wǎng)中給到active-value(switch 打開時的值),inactive-value(switch 關閉時的值),并且支持的類型有boolean / string / number。一般后端傳輸?shù)亩际且詎umber類型。
active-value
和inactive-value
的值分別是字符串的1
和0
,如果你賦值為數(shù)字類型的 1 或 0是無法正常工作的,若賦值為數(shù)值類型,需這樣寫:
<el-switch v-model="status" :active-value="1" :inactive-value="0"> </el-switch>
一、 此時若想跟表格中匹配幾條數(shù)據(jù)就有幾個開關時需要這樣寫:
1、要拿到當前行的數(shù)據(jù)在template標簽中使用**slot-scope=“scope”就可以了
2、使用時在 v-model=“scope.row.字段”就可以把后端返回的開關數(shù)據(jù)展示在表格中
<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ā)時間調(diào)用后端接口,修改開關數(shù)據(jù)
給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" //根據(jù)后端返回的數(shù)據(jù)綁定1為開的狀態(tài) :inactive-value="scope.row.disable == 0" //根據(jù)后端返回的數(shù)據(jù)綁定0為關的狀態(tài) ></el-switch> </template> </el-table-column>
在methods中寫上方法,去調(diào)接口方法名(e,row,index) //e返回狀態(tài)true或false,row當前行數(shù)據(jù),index下標
changeStatus(e, row, index) { //e返回狀態(tài),row當前行數(shù)據(jù),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開關的實現(xiàn)的文章就介紹到這了,更多相關element Switch開關內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vee-validate vue 2.0自定義表單驗證的實例
今天小編就為大家分享一篇vee-validate vue 2.0自定義表單驗證的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08