element-ui中el-select下拉框滚动时分页加载数据

中使用Element UI的el-select组件实现下滑分页加载的功能,通过自定义指令来判断是否需要滚动加载下一页数据

<template>
  <el-select
    v-model="selectedValue"
    filterable
    :filter-method="(value)=>{remoteMethod(value)}"
    v-el-select-loadmore="loadMore"
    @focus="remoteMethod('')"
  >
    <el-option
      v-for="(option,index) in options"
      :key="index"
      :label="option.label"
      :value="option.value"
    ></el-option>
  </el-select>
</template>
 
<script>
export default {
  name:'mySelect',
  data() {
    return {
      selectedValue: null,
      options: [],
      page: 1,
      pageSize: 10,
      total: 100,//假设总共有100条数据
      stopLoading:false,//最后一次加载之后,不再加载
      query:''//可输入值搜索
    };
  },
  methods: {
    remoteMethod(query){
      if(typeof query === 'string'){
          this.querys = query;
      }else{
          this.querys = '';
      }
      this.loadOptions();
    },
    loadOptions() {
      //如果需要搜索参数可带入参数this.querys
      // 模拟异步加载数据
      setTimeout(() => {
        const start = (this.page - 1) * this.pageSize;
        const end = start + this.pageSize;
        let array = Array.from({ length: this.total }, (_, i) => ({
          value: i + 1,
          label: `Option ${i + 1}`,
        })).slice(start, end);
        if(this.page == 1){
          this.options = array
        }else{
          this.options = [...this.options,...array];
        }
        if(this.options.length == this.total){
          this.stopLoading = true;
        }
      }, 500);
    },
    loadMore() {
      if (!this.stopLoading) {
        this.page += 1;
        this.loadOptions();
      }
    }
  },
  created() {
    this.loadOptions();
  },
  directives: {
    'el-select-loadmore': {
        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();
                }
            });
        }
    }
},
};
</script>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值