antd+vue3實現全選&反選
效果如下:
重要代碼如下:
1 <a-form-item 2 label="選擇研究院" 4 v-bind="validateInfos.instituteIds" 5 > 6 <div style="margin-bottom: 10px"> 7 <a-space> 8 <a-button size="small" type="primary" @click="onCheckAllInstitute(true)"> 9 全選 10 </a-button> 11 <a-button size="small" @click="onCheckAllInstitute(false)">反選</a-button> 12 </a-space> 13 </div> 14 <a-checkbox-group v-model:value="modelRef.instituteIds"> 15 <a-checkbox v-for="item in list" :key="item.instituteId" :value="item.instituteId"> 16 {{ item.instituteAlias }} 17 </a-checkbox> 18 </a-checkbox-group> 19 </a-form-item> 20 21 22 // 全選、反選研究院 23 const onCheckAllInstitute = bool => { 24 if (bool) { 25 modelRef.instituteIds = state.list.map(v => v.instituteId) 26 } else {
// 反選 27 const reverseInstitute = [] 28 state.list.forEach(v => { 29 const index = modelRef.instituteIds.findIndex(x => x === v.instituteId) 30 if (index === -1) { 31 reverseInstitute.push(v.instituteId) 32 } 33 }) 34 modelRef.instituteIds = reverseInstitute 35 } 36 }