<template>
    <el-select placeholder="请选择" v-model="selectedCode" @change="changeSelected" :filter-method="filterMethodHandler" filterable v-el-select-lazy='loadMore'>
        <el-option :label="'请选择'" value=""></el-option>
        <el-option :label="item[labelKey]" :key="item.id" :value="item[labelKey]" v-for="item in expectCodeFilter">
            <span class="fl">{{item[labelKey]}}</span>
            <span class="fl codeName t-overflow" :title="item[labelValue]">({{item[labelValue]}})</span>
        </el-option>
    </el-select>
</template>
<script>
    export default {
        name: "selectLazyLoading",
        directives:{
            'el-select-lazy': {
                bind(el, binding) {
                    const SELECTWRAP_DOM = el.querySelector( '.el-select-dropdown .el-select-dropdown__wrap');
                    SELECTWRAP_DOM.addEventListener('scroll', function() {
                        const condition = this.scrollHeight - this.scrollTop <= this.clientHeight;
                        if (condition) {
                            binding.value();
                        }
                    });
                }
            }
        },
        data(){
            return {
                formData:{
                    pageIndex:1,
                    pageSize:20,
                },
                selectedCode:'',
                allCodeData:[]
            }
        },
        props:{
            selectModel:String,
            labelKey:{
                type:String,
                default:''
            },
            labelValue:{
                type:String,
                default:''
            },
            renderCode:{
                type:Array,
                default:function(){
                    return []
                }
            },
        },
        model:{
            prop:'selectModel',
            event:'selection'
        },
        computed:{
            expectCodeFilter(){
                let num = this.formData.pageIndex * this.formData.pageSize;
                return this.allCodeData.filter((ele,index)=>{
                    return index < num;
                })
            }
        },
        watch: {
            selectModel:{
                handler:function(val) {
                    this.selectedCode = val
                },
                immediate:true
            }
        },
        methods:{
            filterMethodHandler(query){
                //搜索时,从全部的数据中查找
                if(query !== ''){
                    this.allCodeData =  this.renderCode.filter(item => item[this.labelKey].toLowerCase().indexOf(query.toLowerCase()) > -1)
                } else {
                    //如果搜索框为空,则初始化下拉框
                    this.initSelectData();
                }
            },
            changeSelected(val){
                //获取下拉框选中的值
                let sendEmit = {};
                for (let i = 0; i < this.expectCodeFilter.length; i++) {
                    const item = this.expectCodeFilter[i];
                    if(item[this.labelKey] === val){
                        sendEmit = item;
                        break
                    }
                }
                this.$emit('getChangeValue',sendEmit);
                this.$emit('selection',val);
            },
            loadMore(){
                //下拉框滚动时触发
                this.formData.pageIndex++;
            },
            initSelectData(){
                //初始化搜索下拉框
                this.formData = {
                    pageIndex:1,
                    pageSize:20,
                };
                this.allCodeData = this.renderCode;
            },
        }
    }
</script>

<style lang="less" scoped>
    .codeName{
        padding-left: 6px;
        max-width: 200px;
    }
</style>

GitHub 加速计划 / eleme / element
54.06 K
14.63 K
下载
A Vue.js 2.0 UI Toolkit for Web
最近提交(Master分支:3 个月前 )
c345bb45 7 个月前
a07f3a59 * Update transition.md * Update table.md * Update transition.md * Update table.md * Update transition.md * Update table.md * Update table.md * Update transition.md * Update popover.md 7 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐