1. Checkbox多选框
1.1. 一组备选项中进行多选。
1.2. Checkbox属性
参数 | 说明 | 类型 | 可选值 | 默认值 |
value / v-model | 绑定值 | string / number / boolean | 无 | 无 |
label | 选中状态的值(只有在checkbox-group或者绑定对象类型为array时有效) | string / number / boolean | 无 | 无 |
true-label | 选中时的值 | string / number | 无 | 无 |
false-label | 没有选中时的值 | string / number | 无 | 无 |
disabled | 是否禁用 | boolean | 无 | false |
border | 是否显示边框 | boolean | 无 | false |
size | Checkbox的尺寸, 仅在border为真时有效 | string | medium / small / mini | 无 |
name | 原生name属性 | string | 无 | 无 |
checked | 当前是否勾选 | boolean | 无 | false |
indeterminate | 设置indeterminate状态, 只负责样式控制 | boolean | 无 | false |
1.3. Checkbox事件
事件名称 | 说明 | 回调参数 |
change | 当绑定值变化时触发的事件 | 更新后的值 |
1.4. Checkbox-group属性
参数 | 说明 | 类型 | 可选值 | 默认值 |
value / v-model | 绑定值 | array | 无 | 无 |
size | 多选框组尺寸, 仅对按钮形式的Checkbox或带有边框的Checkbox有效 | string | medium / small / mini | 无 |
disabled | 是否禁用 | boolean | 无 | false |
min | 可被勾选的checkbox的最小数量 | number | 无 | 无 |
max | 可被勾选的checkbox的最大数量 | number | 无 | 无 |
text-color | 按钮形式的Checkbox激活时的文本颜色 | string | 无 | #ffffff |
fill | 按钮形式的Checkbox激活时的填充色和边框色 | string | 无 | #409EFF |
1.5. Checkbox-group事件
事件名称 | 说明 | 回调参数 |
change | 当绑定值变化时触发的事件 | 更新后的值 |
1.6. Checkbox-button属性
参数 | 说明 | 类型 | 可选值 | 默认值 |
label | 选中状态的值(只有在checkbox-group或者绑定对象类型为array时有效) | string / number / boolean | 无 | 无 |
true-label | 选中时的值 | string / number | 无 | 无 |
false-label | 没有选中时的值 | string / number | 无 | 无 |
disabled | 是否禁用 | boolean | 无 | false |
name | 原生name属性 | string | 无 | 无 |
checked | 当前是否勾选 | boolean | 无 | false |
2. Checkbox多选框例子
2.1. 使用脚手架新建一个名为element-ui-checkbox的前端项目, 同时安装Element插件。
2.2. 编写App.vue
<template>
<div id="app">
<h1>基础用法</h1>
<el-checkbox v-model="base_checkbox">备选项</el-checkbox>
<h1>禁用状态</h1>
<el-checkbox v-model="disabled_checkbox1" disabled>备选项1</el-checkbox>
<el-checkbox v-model="disabled_checkbox2" disabled>备选项2</el-checkbox>
<h1>多选框组</h1>
<el-checkbox-group v-model="group_checkbox">
<el-checkbox label="复选框 A"></el-checkbox>
<el-checkbox label="复选框 B"></el-checkbox>
<el-checkbox label="复选框 C"></el-checkbox>
<el-checkbox label="禁用" disabled></el-checkbox>
<el-checkbox label="选中且禁用" disabled></el-checkbox>
</el-checkbox-group>
<h1>indeterminate状态</h1>
<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" :key="city">{{city}}</el-checkbox>
</el-checkbox-group>
<h1>可选项目数量的限制</h1>
<el-checkbox-group v-model="selectable_checkedCities" :min="1" :max="2">
<el-checkbox v-for="city in selectable_cities" :label="city" :key="city">{{city}}</el-checkbox>
</el-checkbox-group>
<h1>按钮样式</h1>
<div>
<el-checkbox-group v-model="button_checkbox_group1">
<el-checkbox-button v-for="city in cities" :label="city" :key="city">{{city}}</el-checkbox-button>
</el-checkbox-group>
</div>
<div style="margin-top: 20px">
<el-checkbox-group v-model="button_checkbox_group2" size="medium">
<el-checkbox-button v-for="city in cities" :label="city" :key="city">{{city}}</el-checkbox-button>
</el-checkbox-group>
</div>
<div style="margin-top: 20px">
<el-checkbox-group v-model="button_checkbox_group3" size="small">
<el-checkbox-button v-for="city in cities" :label="city" :disabled="city === '北京'" :key="city">{{city}}</el-checkbox-button>
</el-checkbox-group>
</div>
<div style="margin-top: 20px">
<el-checkbox-group v-model="button_checkbox_group4" size="mini" disabled>
<el-checkbox-button v-for="city in cities" :label="city" :key="city">{{city}}</el-checkbox-button>
</el-checkbox-group>
</div>
<h1>带有边框</h1>
<el-checkbox v-model="border_checked1" border>备选项1</el-checkbox>
<el-checkbox v-model="border_checked2" border>备选项2</el-checkbox>
</div>
</template>
<script>
const cityOptions = ['上海', '北京', '广州', '深圳']
export default {
data () {
return {
base_checkbox: true,
disabled_checkbox1: false,
disabled_checkbox2: true,
group_checkbox: ['选中且禁用', '复选框 A'],
checkAll: false,
checkedCities: ['上海', '北京'],
cities: cityOptions,
isIndeterminate: true,
selectable_checkedCities: ['上海', '北京'],
selectable_cities: cityOptions,
button_checkbox_group1: ['上海'],
button_checkbox_group2: ['北京'],
button_checkbox_group3: ['广州'],
button_checkbox_group4: ['深圳'],
border_checked1: true,
border_checked2: false
}
},
methods: {
handleCheckAllChange (val) {
this.checkedCities = val ? cityOptions : []
this.checkAll = this.checkedCities.length === this.cities.length
this.isIndeterminate = false
},
handleCheckedCitiesChange (value) {
const checkedCount = value.length
this.checkAll = checkedCount === this.cities.length
this.isIndeterminate = checkedCount > 0 && checkedCount < this.cities.length
}
}
}
</script>
2.3. 运行项目