2016-11-10 13:46:55 +00:00
< script >
2017-02-22 07:02:34 +00:00
const cityOptions = ['Shanghai', 'Beijing', 'Guangzhou', 'Shenzhen'];
2016-11-10 13:46:55 +00:00
module.exports = {
data() {
return {
checkList: ['selected and disabled','Option A'],
// checkList2: ['Option A'],
checked: true,
checked1: false,
checked2: true,
2017-01-16 02:39:24 +00:00
isValid: 'valid',
checkAll: false,
cities: cityOptions,
2017-02-22 07:02:34 +00:00
checkedCities: ['Shanghai', 'Beijing'],
2017-03-22 21:22:00 +00:00
checkedCities1: ['Shanghai', 'Beijing'],
2017-04-21 08:12:38 +00:00
isIndeterminate: true,
checkboxGroup1: ['Shanghai'],
checkboxGroup2: ['Beijing'],
checkboxGroup3: ['Guangzhou']
2016-11-10 13:46:55 +00:00
};
2017-01-16 02:39:24 +00:00
},
methods: {
handleChange(ev) {
console.log(ev);
},
handleCheckAllChange(event) {
this.checkedCities = event.target.checked ? cityOptions : [];
this.isIndeterminate = false;
},
handleCheckedCitiesChange(value) {
let checkedCount = value.length;
this.checkAll = checkedCount === this.cities.length;
this.isIndeterminate = checkedCount > 0 & & checkedCount < this.cities.length ;
}
2016-11-10 13:46:55 +00:00
}
};
< / script >
## Checkbox
2017-02-22 07:02:34 +00:00
A group of options for multiple choices.
2016-11-10 13:46:55 +00:00
### Basic usage
Checkbox can be used alone to switch between two states.
2017-02-22 07:02:34 +00:00
:::demo Define `v-model` (bind variable) in `el-checkbox` . The default value is a `Boolean` for single `checkbox` , and it becomes `true` when selected. Content inside the `el-checkbox` tag will become the description following the button of the checkbox.
2016-11-10 13:46:55 +00:00
```html
< template >
<!-- `checked` should be true or false -->
< el-checkbox v-model = "checked" > Option< / el-checkbox >
< / template >
< script >
export default {
data() {
return {
checked: true
};
}
};
< / script >
```
:::
2016-12-23 11:36:42 +00:00
### Disabled State
2016-11-10 13:46:55 +00:00
Disabled state for checkbox.
::: demo Set the `disabled` attribute.
```html
< template >
< el-checkbox v-model = "checked1" disabled > Option< / el-checkbox >
< el-checkbox v-model = "checked2" disabled > Option< / el-checkbox >
< / template >
< script >
export default {
data() {
return {
checked1: false,
checked2: true
};
}
};
< / script >
```
:::
### Checkbox group
It is used for multiple checkboxes which are bound in one group, and indicates whether one option is selected by checking if it is checked.
2017-02-22 07:02:34 +00:00
:::demo `checkbox-group` element can manage multiple checkboxes in one group by using `v-model` which is bound as an `Array` . Inside the `el-checkbox` element, `label` is the value of the checkbox. If no content is nested in that tag, `label` will be rendered as the description following the button of the checkbox. `label` also corresponds with the element values in the array. It is selected if the specified value exists in the array, and vice versa.
2016-11-10 13:46:55 +00:00
```html
< template >
< el-checkbox-group v-model = "checkList" >
< el-checkbox label = "Option A" > < / el-checkbox >
< el-checkbox label = "Option B" > < / el-checkbox >
< el-checkbox label = "Option C" > < / el-checkbox >
< el-checkbox label = "disabled" disabled > < / el-checkbox >
< el-checkbox label = "selected and disabled" disabled > < / el-checkbox >
< / el-checkbox-group >
< / template >
< script >
export default {
data () {
return {
checkList: ['selected and disabled','Option A']
};
}
};
< / script >
```
2017-01-16 02:39:24 +00:00
:::
### Indeterminate
The `indeterminate` property can help you to achieve a 'check all' effect.
:::demo
```html
< template >
< el-checkbox :indeterminate = "isIndeterminate" v-model = "checkAll" @change =" handleCheckAllChange " > Check all</ el-checkbox >
< div style = "margin: 15px 0;" > < / div >
< el-checkbox-group v-model = "checkedCities" @change =" handleCheckedCitiesChange " >
2017-04-28 06:03:42 +00:00
< el-checkbox v-for = "city in cities" :label = "city" :key = "city" > {{city}}< / el-checkbox >
2017-01-16 02:39:24 +00:00
< / el-checkbox-group >
< / template >
< script >
2017-02-22 07:02:34 +00:00
const cityOptions = ['Shanghai', 'Beijing', 'Guangzhou', 'Shenzhen'];
2017-01-16 02:39:24 +00:00
export default {
data() {
return {
checkAll: true,
2017-02-22 07:02:34 +00:00
checkedCities: ['Shanghai', 'Beijing'],
2017-01-16 02:39:24 +00:00
cities: cityOptions,
2017-02-20 03:39:52 +00:00
isIndeterminate: true
2017-01-16 02:39:24 +00:00
};
},
methods: {
handleCheckAllChange(event) {
this.checkedCities = event.target.checked ? cityOptions : [];
this.isIndeterminate = false;
},
handleCheckedCitiesChange(value) {
let checkedCount = value.length;
this.checkAll = checkedCount === this.cities.length;
this.isIndeterminate = checkedCount > 0 & & checkedCount < this.cities.length ;
}
}
};
< / script >
```
2017-03-22 21:22:00 +00:00
:::
### Minimum / Maximum items checked
2017-03-23 08:03:49 +00:00
The `min` and `max` properties can help you to limit the number of checked items.
2017-03-22 21:22:00 +00:00
:::demo
```html
< template >
< el-checkbox-group
v-model="checkedCities1"
2017-03-23 08:03:49 +00:00
:min="1"
:max="2">
2017-04-28 06:03:42 +00:00
< el-checkbox v-for = "city in cities" :label = "city" :key = "city" > {{city}}< / el-checkbox >
2017-03-22 21:22:00 +00:00
< / el-checkbox-group >
< / template >
< script >
const cityOptions = ['Shanghai', 'Beijing', 'Guangzhou', 'Shenzhen'];
export default {
data() {
return {
checkedCities1: ['Shanghai', 'Beijing'],
cities: cityOptions
};
}
};
< / script >
```
2016-11-10 13:46:55 +00:00
:::
2017-04-21 08:12:38 +00:00
### Button style
Checkbox with button styles.
:::demo You just need to change `<el-checkbox>` element into `<el-checkbox-button>` element. We also provide `size` attribute for these buttons: `large` and `small` .
```html
< template >
< div style = "margin: 15px 0;" > < / div >
< el-checkbox-group v-model = "checkboxGroup1" >
2017-04-28 06:03:42 +00:00
< el-checkbox-button v-for = "city in cities" :label = "city" :key = "city" > {{city}}< / el-checkbox-button >
2017-04-21 08:12:38 +00:00
< / el-checkbox-group >
< div style = "margin: 15px 0;" > < / div >
< el-checkbox-group v-model = "checkboxGroup2" size = "small" >
2017-04-28 06:03:42 +00:00
< el-checkbox-button v-for = "city in cities" :label = "city" :disabled = "city === 'Shenzhen'" :key = "city" > {{city}}< / el-checkbox-button >
2017-04-21 08:12:38 +00:00
< / el-checkbox-group >
< div style = "margin: 15px 0;" > < / div >
< el-checkbox-group v-model = "checkboxGroup3" size = "large" fill = "#324057" text-color = "#a4aebd" :min = "1" :max = "3" >
2017-04-28 06:03:42 +00:00
< el-checkbox-button v-for = "city in cities" :label = "city" :key = "city" > {{city}}< / el-checkbox-button >
2017-04-21 08:12:38 +00:00
< / el-checkbox-group >
< / template >
< script >
const cityOptions = ['Shanghai', 'Beijing', 'Guangzhou', 'Shenzhen'];
export default {
data () {
return {
checkboxGroup1: ['Shanghai'],
checkboxGroup2: ['Beijing'],
checkboxGroup3: ['Guangzhou'],
cities: cityOptions
};
}
}
< / script >
```
:::
2016-11-10 13:46:55 +00:00
### Checkbox Attributes
| Attribute | Description | Type | Options | Default|
|---------- |-------- |---------- |------------- |-------- |
| label | value of the checkbox when used inside a `checkbox-group` | string | — | — |
| true-label | value of the checkbox if it's checked | string, number | — | — |
| false-label | value of the checkbox if it's not checked | string, number | — | — |
2016-11-14 04:16:13 +00:00
| name | native 'name' attribute | string | — | — |
2016-11-10 13:46:55 +00:00
| disabled | if the checkbox is disabled | boolean | — | false |
| checked | if the checkbox is checked | boolean | — | false |
| indeterminate | same as `indeterminate` in native checkbox | boolean | — | false |
2017-03-23 08:03:49 +00:00
### Checkbox-group Attributes
| Attribute | Description | Type | Options | Default|
|---------- |-------- |---------- |------------- |-------- |
2017-04-21 08:12:38 +00:00
|size | the size of checkbox buttons | string | large/small | —
|fill | border and background color when button is active | string | — | #20a0ff |
|text-color | font color when button is active | string | — | #ffffff |
2017-03-23 08:03:49 +00:00
| min | minimum number of checkbox checked | number | — | — |
| max | maximum number of checkbox checked | number | — | — |
2017-07-20 06:30:56 +00:00
### Checkbox Events
2016-11-10 13:46:55 +00:00
| Event Name | Description | Parameters |
|---------- |-------- |---------- |
| change | triggers when the binding value changes | Event object |