最近項目需要一個動態禁用el-checkbox選項的功能,已經被選擇過的項需要在“批量新增”的彈窗中禁用(禁用掉已經在列表中的“西安工廠”)
image.png
Element中只提供了
<el-checkbox-group v-model="checkboxGroup3" size="small">
<el-checkbox-button v-for="city in cities" :label="city" :disabled="city === '北京'" :key="city">{{city}}</el-checkbox-button>
</el-checkbox-group>
這里使用ES6的語法includes(),把已選擇的列表id放入數組disabledStores中,其中store是為el-checkbox綁定數據的storeOptions中的值,store.id是數組中值的id,代碼如下:
:disabled="disabledStores.includes(store.id)"
完整代碼如下:
<el-checkbox-group v-model="checkedStores" @change="checkedStoresChange">
<el-checkbox v-for="store in storeOptions" :label="store" :key="store.id" :disabled="disabledStores.includes(store.id)" >{{store.name}}</el-checkbox>
</el-checkbox-group>
就這樣,可以把已經添加到列表的值id,push進數組disabledStores中,動態禁用批量操作中checkbox多選框,防止數據被覆蓋!