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. 编写index.js
import Vue from 'vue'
import VueRouter from 'vue-router'
import Checkbox from '../components/Checkbox.vue'
import GroupCheckbox from '../components/GroupCheckbox.vue'
import IndeterminateCheckbox from '../components/IndeterminateCheckbox.vue'
Vue.use(VueRouter)
const routes = [
{ path: '/', redirect: '/Checkbox' },
{ path: '/Checkbox', component: Checkbox },
{ path: '/GroupCheckbox', component: GroupCheckbox },
{ path: '/IndeterminateCheckbox', component: IndeterminateCheckbox }
]
const router = new VueRouter({
routes
})
export default router
2.3. 在components下创建Checkbox.vue
<template>
<div>
<h1>基础用法</h1>
<h4>在el-checkbox元素中定义v-model绑定变量, 单一的checkbox中, 默认绑定变量的值会是Boolean, 选中为true。</h4>
<el-checkbox v-model="base_checkbox1" :true-label="1" :false-label="-1">旅游</el-checkbox>
<el-checkbox v-model="base_checkbox2" :true-label="2" :false-label="-1">游泳</el-checkbox>
<h1>禁用状态</h1>
<h4>设置disabled属性即可。</h4>
<el-checkbox v-model="disabled_checkbox1" true-label="旅游" false-label="" disabled>旅游</el-checkbox>
<el-checkbox v-model="disabled_checkbox2" true-label="游泳" false-label="" disabled>游泳</el-checkbox>
<h1>带有边框</h1>
<h4>设置border属性可以渲染为带有边框的多选框。</h4>
<el-checkbox checked true-label="旅游" false-label="" border @change="handleCheckedCitiesChange">旅游</el-checkbox>
<el-checkbox checked true-label="游泳" false-label="" border @change="handleCheckedCitiesChange">游泳</el-checkbox>
</div>
</template>
<script>
export default {
data () {
return {
base_checkbox1: 1,
base_checkbox2: 2,
disabled_checkbox1: '旅游',
disabled_checkbox2: '游泳'
}
},
methods: {
handleCheckedCitiesChange (val) {
console.log(val)
}
}
}
</script>
2.4. 在components下创建GroupCheckbox.vue
<template>
<div>
<h1>多选框组</h1>
<h4>checkbox-group元素能把多个checkbox 管理为一组, 只需要在Group中使用v-model绑定Array类型的变量即可。el-checkbox的label属性是该checkbox对应的值, 若该标签中无内容, 则该属性也充当checkbox按钮后的介绍。label与数组中的元素值相对应, 如果存在指定的值则为选中状态, 否则为不选中。</h4>
<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>可选项目数量的限制</h1>
<h4>使用min和max属性能够限制可以被勾选的项目的数量。</h4>
<el-checkbox-group v-model="checkedCities" :min="1" :max="2">
<el-checkbox v-for="city in cities" :label="city" :key="city">{{city}}</el-checkbox>
</el-checkbox-group>
<h1>按钮样式</h1>
<h4>只需要把el-checkbox元素替换为el-checkbox-button元素即可。此外, Element还提供了size属性。</h4>
<div>
<el-checkbox-group v-model="button_checkbox_group1" text-color="#F56C6C" fill="#67C23A">
<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>
</div>
</template>
<script>
export default {
data () {
return {
group_checkbox: ['选中且禁用', '复选框 A'],
checkedCities: ['北京', '上海'],
cities: ['上海', '北京', '广州', '深圳'],
button_checkbox_group1: ['上海'],
button_checkbox_group2: ['北京'],
button_checkbox_group3: ['广州'],
button_checkbox_group4: ['深圳']
}
}
}
</script>
2.5. 在components下创建IndeterminateCheckbox.vue
<template>
<div>
<h1>indeterminate状态</h1>
<h4>indeterminate属性用以表示checkbox的不确定状态, 一般用于实现全选的效果。</h4>
<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>
</div>
</template>
<script>
const cityOptions = ['北京', '上海', '广州', '深圳']
export default {
data () {
return {
checkAll: false,
checkedCities: ['北京', '上海'],
cities: cityOptions,
isIndeterminate: true
}
},
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.6. 运行项目, 访问http://localhost:8080/#/Checkbox
2.7. 运行项目, 访问http://localhost:8080/#/GroupCheckbox
2.8. 运行项目, 访问http://localhost:8080/#/IndeterminateCheckbox