vue+el-select下拉多选实现,全选,反选,清空功能源码

该文章展示了一个使用Vue.js和ElementUI库实现的下拉多选组件,包括全选、反选和清空功能。通过数据绑定和JavaScript方法处理选中状态,同时提供了CSS样式来美化组件。
摘要由CSDN通过智能技术生成

显示效果

vue+elementui实现下拉多选,全选,反选,清空功能

vue+el-select下拉多选实现,全选,反选,清空功能源码

实例代码

页面内引用

组件:
<el-select v-model="orgData" size="small" multiple collapse-tags>
            <div class="select_up">
                <el-button type="text" v-on:click="selectAll">
                    <i class="jw jw-quanxuan " />
                    全选</el-button>
                <el-button type="text" v-on:click="removeTag">
                    <i class="jw jw-qingkong " />
                    清空</el-button>
                <el-button type="text" v-on:click="selectReverse">
                    <i class="jw jw-fanxuan " />
                    反选</el-button>
            </div>
            <div class="select_list">
                <el-option v-for="item in webAddresses" :key="item.orgId" :label="item.orgName" :value="item.orgId" />
            </div>
        </el-select>
js
export default {
    data() {
        return {
            webAddresses: [{
                orgId: '1',
                orgName: '全选1'
            }, {
                orgId: '2',
                orgName: '全选2'
            }, {
                orgId: '3',
                orgName: '全选2'
            }, {
                orgId: '4',
                orgName: '全选2'
            }, {
                orgId: '5',
                orgName: '全选2'
            }, {
                orgId: '6',
                orgName: '全选2'
            }, {
                orgId: '7',
                orgName: '全选2'
            }, {
                orgId: '8',
                orgName: '全选2'
            }, {
                orgId: '9',
                orgName: '全选2'
            }
            ],// 遍历的select option 从后端遍历
            orgData: [],//选中数据
        }
    },
    methods: {
        removeTag() {
            this.orgData = []
        },
        selectAll(val) {
            val = []
            this.webAddresses.map(item => {
                val.push(item.orgId)
            })
            this.orgData = val
        },
        selectReverse(val) {
            val = []
            this.webAddresses.map(item => {
                let index = this.orgData.indexOf(item.orgId);
                console.log(index)
                if (index != -1) {
                    
                    //orgData.splice(index, 1)
                } else {
                    val.push(item.orgId)
                }
            })
            this.orgData = val
        }



        
    }
}
css

.el-select-dropdown__list {
    height: 100%;
    overflow: hidden;

}

.select_up {
    padding: 0 12px;
    font-size: 14px;
    position: absolute;
    z-index: 99999;
    background-color: white;
    top: 0px;
    width: 100%;
    border-radius: 5px 5px 0 0;

    ::v-deep .el-button {
        color: #bcbcbc;
        font-size: 14px;

        i {
            font-size: 14px;
        }
    }

    ::v-deep .el-button:hover {
        color: #409EFF;
    }

    .el-button+.el-button {
        margin-left: 6px;
    }
}

.select_list {
    margin-top: 25px;
}

完整组件

<template>
    <div>
        <el-select v-model="orgData" size="small" multiple collapse-tags>
            <div class="select_up">
                <el-button type="text" v-on:click="selectAll">
                    <i class="jw jw-quanxuan " />
                    全选</el-button>
                <el-button type="text" v-on:click="removeTag">
                    <i class="jw jw-qingkong " />
                    清空</el-button>
                <el-button type="text" v-on:click="selectReverse">
                    <i class="jw jw-fanxuan " />
                    反选</el-button>
            </div>
            <div class="select_list">
                <el-option v-for="item in webAddresses" :key="item.orgId" :label="item.orgName" :value="item.orgId" />
            </div>
        </el-select>

    </div>
</template>
<script>
export default {
    data() {
        return {
            webAddresses: [{
                orgId: '1',
                orgName: '全选1'
            }, {
                orgId: '2',
                orgName: '全选2'
            }, {
                orgId: '3',
                orgName: '全选2'
            }, {
                orgId: '4',
                orgName: '全选2'
            }, {
                orgId: '5',
                orgName: '全选2'
            }, {
                orgId: '6',
                orgName: '全选2'
            }, {
                orgId: '7',
                orgName: '全选2'
            }, {
                orgId: '8',
                orgName: '全选2'
            }, {
                orgId: '9',
                orgName: '全选2'
            }
            ],// 遍历的select option 从后端遍历
            orgData: [],//选中数据
        }
    },
    methods: {
        //清空操作
        removeTag() {
            this.orgData = []
        },
        //全选操作
        selectAll(val) {
            val = []
            this.webAddresses.map(item => {
                val.push(item.orgId)
            })
            this.orgData = val
        },
        //反选操作
        selectReverse(val) {
            val = []
            this.webAddresses.map(item => {
                let index = this.orgData.indexOf(item.orgId);
                //判断现有选中数据是否包含如果不包含则进行反选数据
                if (index != -1) {
                } else {
                    val.push(item.orgId)
                }
            })
            this.orgData = val
        }



        
    }
}
</script>
<style scoped lang="scss">
.el-select-dropdown__list {
    height: 100%;
    overflow: hidden;

}

.select_up {
    padding: 0 12px;
    font-size: 14px;
    position: absolute;
    z-index: 99999;
    background-color: white;
    top: 0px;
    width: 100%;
    border-radius: 5px 5px 0 0;

    ::v-deep .el-button {
        color: #bcbcbc;
        font-size: 14px;

        i {
            font-size: 14px;
        }
    }

    ::v-deep .el-button:hover {
        color: #409EFF;
    }

    .el-button+.el-button {
        margin-left: 6px;
    }
}

.select_list {
    margin-top: 25px;
}
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

G佳伟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值