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

在官網(wǎng)中給到active-value(switch 打開時(shí)的值),inactive-value(switch 關(guān)閉時(shí)的值),并且支持的類型有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í)若想跟表格中匹配幾條數(shù)據(jù)就有幾個(gè)開關(guān)時(shí)需要這樣寫:
1、要拿到當(dāng)前行的數(shù)據(jù)在template標(biāo)簽中使用**slot-scope=“scope”就可以了
2、使用時(shí)在 v-model=“scope.row.字段”就可以把后端返回的開關(guān)數(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ā)時(shí)間調(diào)用后端接口,修改開關(guān)數(shù)據(jù)
給Switch 開關(guān)加上@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ā)時(shí)間
:active-value="scope.row.disable == 1" //根據(jù)后端返回的數(shù)據(jù)綁定1為開的狀態(tài)
:inactive-value="scope.row.disable == 0" //根據(jù)后端返回的數(shù)據(jù)綁定0為關(guān)的狀態(tài)
></el-switch>
</template>
</el-table-column>在methods中寫上方法,去調(diào)接口方法名(e,row,index) //e返回狀態(tài)true或false,row當(dāng)前行數(shù)據(jù),index下標(biāo)
changeStatus(e, row, index) {
//e返回狀態(tài),row當(dāng)前行數(shù)據(jù),index下標(biāo)
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);
})
}到此這篇關(guān)于element多選表格中使用Switch開關(guān)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)element Switch開關(guān)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
el-date-picker時(shí)間清空值為null處理方案
本文介紹關(guān)于Vue.js項(xiàng)目中時(shí)間選擇器組件的問題,當(dāng)選擇后清空導(dǎo)致值變?yōu)閚ull,進(jìn)而引發(fā)后臺(tái)接口報(bào)錯(cuò),通過監(jiān)聽`overallForm.time`的值并設(shè)置為空數(shù)組,成功解決此問題,確保了數(shù)據(jù)正確性,同時(shí),建議避免直接監(jiān)聽整個(gè)對(duì)象以優(yōu)化性能,感興趣的朋友一起看看吧2024-08-08
vue實(shí)現(xiàn)列表倒計(jì)時(shí)
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)列表倒計(jì)時(shí),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09
vee-validate vue 2.0自定義表單驗(yàn)證的實(shí)例
今天小編就為大家分享一篇vee-validate vue 2.0自定義表單驗(yàn)證的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08

