赞
踩
Select 选择器之elementUI版——https://element.eleme.cn/#/zh-CN/component/select
Select 选择器之Ant Design of Vue版——https://www.antdv.com/components/select-cn/
elementUI的下拉框支持多选,但是不支持全选,但需求就要这个功能
应该有以下几种情况:
下拉选项全都勾选时,【全选】自动勾选;
下拉选项部分勾选时,点击【全选】后,所有下拉项全部勾选;
下拉选项全都未勾选时,点击【全选】后,所有下拉选项全部勾选;
下拉选项和【全选】都选上的时候,不勾选任意下拉选项,【全选】按钮就不勾选了;
html
<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>
js
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 = [] } } } }
效果图
实现的功能跟方法一是一样的
html
<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>
js
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 } } } }
css
.el-checkbox {
text-align: right;
width: 100%;
padding-right: 10px;
}
效果图
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。