当前位置:   article > 正文

Vue element-ui select 下拉框数据懒加载_element ui select 懒加载

element ui select 懒加载
<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>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/114838
推荐阅读
相关标签