el-switch 通過請求的結果開啟/關閉


說明:el-switch的change事件只能拿到開關改變后的值,所以這里用input事件,並使用value綁定值,不使用v-model綁定

模板代碼:

1           <el-table-column fixed="right" label="開關" width="100px">
2             <template slot-scope="scope">
3               <el-switch :value="scope.row.ternaryFlag"     @input='changeTernaryFlag(scope.row, $event)' active-color="#409eff" inactive-color="#f5f5f5">
4               </el-switch>
6             </template>
7           </el-table-column>

js代碼:

 1     changeTernaryFlag(row, val) {
 2       const data = {
 3         id: row.id,
 4         ternaryFlag: val
 5       }
 6       let str = val ? '獲取' : '放棄'
 7       this.$confirm(`確定要${str}嗎?`, '提示', {
 8         confirmButtonText: '確定',
 9         cancelButtonText: '取消',
10         type: 'warning'
11       }).then(() => {
12         const loading = this.$loading({
13           lock: true,
14           text: 'Loading',
15           spinner: 'el-icon-loading',
16           background: 'rgba(0, 0, 0, 0.7)'
17         })
18         ternaryFlag(data).then(res => {
19           console.log(res)
20           loading.close()
21           if (res.data.code === '200') {
22             // 請求成功  修改狀態
23             for (const item of this.tableData) {
24               if (item.id === row.id) {
25                 let temData = this.tableData
26                 item.ternaryFlag = val
27                 this.tableData = temData
28                 break
29               }
30             }
31             this.$message({
32               type: 'success',
33               message: `${str}成功!`
34             })
35           } else {
36             this.$message.error(res.data.msg)
37           }
38         })
39       }).catch(() => {
40         this.$message({
41           type: 'info',
42           message: `${str}失敗!`
43         })
44       })
45 
46     }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM