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

显示效果

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

vue+el-select下拉多选实现,全选,反选,清空功能源码_第1张图片

实例代码

页面内引用

组件:

<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;
}

完整组件




你可能感兴趣的:(vue,vue.js,javascript,前端)