我的效果圖如下:
1 <template> 2 <div class="mcontainer"> 3 <el-form :model="formApp" ref="formApp" :rules="rules"> 4 <el-form-item label="APP選擇" :label-width="formLabelWidth" prop="appCode"> 5 <el-select style="width:100%;" 6 v-model="formApp.appCode" 7 multiple 8 :disabled="type == 'add'?false : true" 9 placeholder="請選擇" 10 @change="changeType"> 11 <el-option 12 v-for="item in selList" 13 :key="item.code" 14 :label="item.name" 15 :value="item.code"> 16 </el-option> 17 </el-select> 18 </el-form-item> 19 <el-form-item label="所屬分類" :label-width="formLabelWidth"> 20 <div v-show="typeList.length > 0" class="check-box" v-for="(item, index) in typeList"> 21 <span class="app-name"> 22 <el-tag>{{ item.appName }}</el-tag> 23 </span> 24 <el-checkbox-group v-model="checkedEquipments[index]"> 25 <el-checkbox 26 @change="handleChange(type,index,item)" 27 v-for="(type, i) in item.types" 28 :disabled="i == 0? true : false" 29 :key="type.id" 30 :label="type.id">{{type.name}}</el-checkbox> 31 </el-checkbox-group> 32 </div> 33 <div v-show="typeList.length == 0" class="no-typelist">請先選擇APP</div> 34 </el-form-item> 35 </el-form> 36 </div> 37 </template> 38 39 <script> 47 export default { 48 name: 'CollectOrder', 49 components: { CollectRecord }, 50 data() { 51 return { 52 formLabelWidth: '80px', 53 /*隨意修改后的checked項, 54 * 因為是二維數組,我的第一個類「全部」默認的ID是0,如果沒有全部,不需要0,直接這樣子就可以[[],[],[],[],[]] 55 * 我默認顯示了5個,具體幾個會根據app的選擇值來重新計算,如果一個APP,就一級,2個APP,就2組, 56 * ******************************** */ 57 checkedEquipments: [[0],[0],[0],[0],[0]], 58 checkEquipArr: [], // 傳給后台的二維數組,根據自己要返回給后台的格式重組過的 59 formApp: { 60 appCode: [], 61 appTypeIds: [],//馬甲及分類列表 62 }, 63 type: 'add', 64 allType: [], //獲取的所有類別 65 typeList: [], 66 } 67 //我得到的類別數據是一個二維數據,如下 68 // [ 69 // { 70 // appCode: "MoneyHome", 71 // appName: "MoneyHome", 72 // types: [ 73 // {id: 21, name: "第二個app--分類一"} 74 // ] 75 // }, 76 // { 77 // appCode: "RupeeHome", 78 // appName: "RupeeHome", 79 // types: [ 80 // {id: 20, name: "aaa"}, 81 // {id: 22, name: "q"} 82 // ] 83 // } 84 // ] 85 }, 86 methods: { 87 //APPP選擇的時候,類別也做相應的改變 88 changeType(val) { 89 //類別的數組占位,app選擇幾個,就會有幾組類別 90 this.checkedEquipments = [];//因為默認的有5組,所以要重新清掉了,根據選擇幾組來確定 91 val.forEach((item,index) => { 92 if(index <= val.length) { 93 this.checkedEquipments.push([0]); 94 } 95 }); 96 console.log('this.checkedEquipments---', this.checkedEquipments); 97 this.typeList = []; 98 //this.allType是我能過接口返回的所有的類,存在這里了,換成自己相應的變量就好 99 let list = Object.assign([],this.allType); 100 //這一步是為了給每一類組數加一個"全部",如果沒有這個需求,就不需這步了 101 let obj = { 102 id: 0, 103 name: '全部', 104 }; 105 list.forEach(item => { 106 //每個類別要加上全部 107 if(item.types.length > 0){ 108 if(item.types[0].name != '全部'){ 109 item.types.unshift(obj); 110 } 111 }else { 112 item.types.unshift(obj); 113 } 114 val.forEach(name => { 115 if(item.appCode == name){ 116 this.typeList.push(item); 117 } 118 }); 119 }); 120 }, 121 //創建保存時的二維數組 122 handleChange (data, index, item) { 123 this.checkEquipArr = []; 124 let arr = this.checkedEquipments; 125 for (let i = 0; i < arr.length; i ++) { 126 let equipment = arr[i]; 127 if (equipment.length > 0) { 128 let obj = { 129 appCode: this.typeList[i].appCode, 130 typeIds: [] 131 } 132 for (let j = 0; j < equipment.length; j++) { 133 obj.typeIds.push(equipment[j]) 134 } 135 this.checkEquipArr.push(obj); 136 } 137 } 138 console.log("QERQWER00000====",this.checkEquipArr); 139 //重組后我傳給后台的二維數組,可以適當根據自己的需求調整格式和字段 140 // [ 141 // { 142 // appCode: "MoneyHome", 143 // typeIds: [0, 21], 144 // }, 145 // { 146 // appCode: "RupeeHome", 147 // typeIds: [0, 20, 22] 148 // } 149 // ] 150 }, 151 }, 152 }; 153 </script>
參考了這篇文章:https://www.cnblogs.com/wjunwei/p/9531322.html
然后根據自己的實際需求作了一些改動。