el-checkbox實現全選與單選


實現目的:實現全選與多選,點擊確定的時候獲取每個值的id傳給后台

1、HTML

1 <el-checkbox v-model="checkAll" @change="handleCheckAllChange">全選</el-checkbox>
2 <el-checkbox v-for="(city,i) in cities" :label="city.uuid" :key="i" v-model="city.relation"
3    @change="handleCheckedCitiesChange">{{city.customerName}}</el-checkbox>
5                     
6 <span slot="footer" class="dialog-footer">
7    <el-button type="primary" :disabled="disabled" @click="classifyConfirm">確定</el-button>
8    <el-button type="primary" @click="calceConfirn">取消</el-button>
9 </span>

2、data

 1  data: function () {
 2     return { 3         checkAll: false,
 4         cities: [], //數據源
5   }
6 }
      

3、js

 1 handleCheckAllChange(val) {
 2    if (this.checkAll) {
 3        this.cities.forEach(item => {
 4             item.relation = true   //只改變數據的狀態
 5          })
 6      } else {
 7       this.cities.forEach(item => {
 8           item.relation = false
 9             })
10           }
11       },
12 handleCheckedCitiesChange() {
13   console.log(this.checkedCities);
14   if (this.checkedCities.length == this.cities.length) {
15   this.checkAll = true
16     } else {
17       this.checkAll = false
18    }
19 },
 1 //確認
 2             classifyConfirm() {
 3                 var checkedAll = [];
 4                 this.cities.forEach(item => {
 5                     if(item.relation == true){
 6                         checkedAll.push(item.uuid)   //確認的時候取狀態為true的值,並把要用的字段取出來
 7                     }
 8                 })
 9                 // var checkedAll = [...new Set(this.checkedCities)] //數組去重
10                 var that = this;
11                 var data = {
12                     "customerUuid":that.customerUuid,
13                     "projectCustomerUuid":checkedAll.join(',')
14                 };
15                 console.log(checkedAll)
16                 this.classLoading = true;
17                 $.ajax({
18                     url: Domain + '/xxxxx',
19                     dataType: "json",
20                     method: "POST",
21                     // contentType: "application/json; charset=utf-8",
22                     data:data,
23                     success: function (ret) {
24                         if (ret.retStatus == "1") {
25                             that.$message({
26                                 type: 'success',
27                                 message: '保存成功!'
28                             });
29                             that.classLoading = false;32                         }
33                     }
34                 })
35             },

最開始在改變狀態的時候就想着取id了,導致后面一系列的問題,后面經過大佬的提醒,在最開始的時候不要取值,只改變狀態就行,最后提交的時候遍歷數組,然后把每一項里面為true的取出來就可以了,很簡單就解決了

或者html 這樣寫也可以:

<el-checkbox :indeterminate="roleIsIndeterminate" v-model="roleCheckAll" @change="handleRoleAllChange" :disabled="isAllChecked || onlyAllDisabled">全選</el-checkbox>
<el-checkbox-group v-model="checkedRoles" @change="handleCheckedRolesChange">
<el-checkbox v-for="role in roleList" :label="role.id" :key="role.id" :disabled="role.disabled">{{role.name}}</el-checkbox>
</el-checkbox-group>

 


免責聲明!

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



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