When writing a function, we find that el-select supports multiple elections, but it doesn't support all elections. It's wordless. Let's do it by ourselves. There are two ways. The second way is simpler.
Method 1: Add a drop-down item, and then there should be the following situations:
- When all drop-down options are checked, [full selection] is automatically checked.
- When the drop-down option part is checked, all drop-down items are checked after clicking on the full selection.
- When all the drop-down options are not checked, click on the [full selection], all the drop-down options are not checked.
- When the drop-down option and the [full selection] are both selected, the [full selection] button will not be checked unless any drop-down option is checked.
The above is what I want to achieve, I'm very verbose... Look at the code...
html section:
<template> <el-select multiple collapse-tags v-model='selectedArray' @change='changeSelect' @remove-tag='removeTag' placeholder='Please choose'> <el-option label='All election' value='All election' @click.native='selectAll'></el-option> <el-option v-for='(item, index) in options' :key='index' :label='item.name' :value='item.name'></el-option> </el-select> </template>
Part js:
export default { data() { return { selectedArray: [], options: [ { name: 'one by one', label: 'one' }, { name: '22', label: 'tow' }, { name: '33', label: 'three' }, { name: '44', label: 'four' }, { name: '55', label: 'five' } ] } }, methods: { selectAll() { if (this.selectedArray.length < this.options.length) { this.selectedArray = [] this.options.map((item) => { this.selectedArray.push(item.name) }) this.selectedArray.unshift('All election') } else { this.selectedArray = [] } }, changeSelect(val) { if (!val.includes('All election') && val.length === this.options.length) { this.selectedArray.unshift('All election') } else if (val.includes('All election') && (val.length - 1) < this.options.length) { this.selectedArray = this.selectedArray.filter((item) => { return item !== 'All election' }) } }, removeTag(val) { if (val === 'All election') { this.selectedArray = [] } } } }
Look at the picture.
Method 2: Add a check box directly to achieve the same function as method 1.
html section:
<template> <el-select multiple collapse-tags v-model='selectedArray' @change='changeSelect' placeholder='Please choose'> <el-checkbox v-model="checked" @change='selectAll'>All election</el-checkbox> <el-option v-for='(item, index) in options' :key='index' :label='item.name' :value='item.name'></el-option> </el-select> </template>
Part js:
export default { data() { return { checked: false, selectedArray: [], options: [ { name: 'one by one', label: 'one' }, { name: '22', label: 'tow' }, { name: '33', label: 'three' }, { name: '44', label: 'four' }, { name: '55', label: 'five' } ] } }, methods: { selectAll() { this.selectedArray = [] if (this.checked) { this.options.map((item) => { this.selectedArray.push(item.name) }) } else { this.selectedArray = [] } }, changeSelect(val) { if (val.length === this.options.length) { this.checked = true } else { this.checked = false } } } }
css:
.el-checkbox { text-align: right; width: 100%; padding-right: 10px; }
Design sketch: