赞
踩
<template>
<el-select multiple collapse-tags v-model='selectedArray' @change='changeSelect' @remove-tag='removeTag' placeholder='请选择'>
<el-option label='全选' value='全选' @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>
export default { data() { return { selectedArray: [], options: [ { name: '一一', label: 'one' }, { name: '二二', label: 'tow' }, { name: '三三', label: 'three' }, { name: '四四', label: 'four' }, { name: '五五', 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('全选') } else { this.selectedArray = [] } }, changeSelect(val) { if (!val.includes('全选') && val.length === this.options.length) { this.selectedArray.unshift('全选') } else if (val.includes('全选') && (val.length - 1) < this.options.length) { this.selectedArray = this.selectedArray.filter((item) => { return item !== '全选' }) } }, removeTag(val) { if (val === '全选') { this.selectedArray = [] } } } }
<template>
<el-select multiple collapse-tags v-model='selectedArray' @change='changeSelect' placeholder='请选择'>
<el-checkbox v-model="checked" @change='selectAll'>全选</el-checkbox>
<el-option v-for='(item, index) in options' :key='index' :label='item.name' :value='item.name'></el-option>
</el-select>
</template>
export default { data() { return { checked: false, selectedArray: [], options: [ { name: '一一', label: 'one' }, { name: '二二', label: 'tow' }, { name: '三三', label: 'three' }, { name: '四四', label: 'four' }, { name: '五五', 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 } } } }
.el-checkbox {
text-align: right;
width: 100%;
padding-right: 10px;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。