http://element-cn.eleme.io/#/zh-CN/component/checkbox
这里的例子中,数据源是普通数组.
这里为了需求,将普通数组改为对象数组.
<template>
<el-checkbox :indeterminate="isIndeterminate" v-model="checkAll" @change="handleCheckAllChange">全选</el-checkbox>
<div style="margin: 15px 0;"></div>
<el-checkbox-group v-model="checkedCities" @change="handleCheckedCitiesChange">
<el-checkbox v-for="city in cities" :label="city.id" :key="city.id">{{city.label}}</el-checkbox>
</el-checkbox-group>
</template>
<script>
const cityOptions = [{id:1,label:'上海'},{id:2,label:'北京'},{id:3,label:'广州'}, {id:4,label: '深圳'}];
export default {
data() {
return {
checkAll: false,
checkedCities: [1,2],
cities: cityOptions,
isIndeterminate: true
};
},
methods: {
handleCheckAllChange(val) {
this.checkAll = !!this.checkAll;
let checked = cityOptions.map(function(item){return item.id;});
this.checkedCities = this.checkAll ? checked : [];
this.isIndeterminate = false;
},
handleCheckedCitiesChange(value) {
let checkedCount = value.length;
this.checkAll = checkedCount === this.cities.length;
this.isIndeterminate = checkedCount > 0 && checkedCount < this.cities.length;
}
}
};
</script>