vue+element-ui 实现下拉框滚动加载


该功能是由 自定义滚动指令结合下拉框 :remote-method 远程搜索实现的

一、el-select组件绑定属性

 <el-select clearable v-model="form.materialId" v-el-select-loadmore="loadmore" placeholder="请选择序列" filterable remote :multiple="type == 'add' ? true : false" :remote-method="remoteMethod" :loading="selectLoading" style="width:100%">
     <el-option v-for="(item, index) in typeList" :key="index" :label="item.materialName" :value="item.id">
     </el-option>
 </el-select>
  1. 开启远程搜索 参考官方文档

    在这里插入图片描述

  2. 绑定自定义指令 v-el-select-loadmore=“loadmore”

二、自定义滚动指令

 directives: {
    'el-select-loadmore': {
      bind(el, binding) {
        const SELECTWRAP_DOM = el.querySelector('.el-select-dropdown .el-select-dropdown__wrap')
        SELECTWRAP_DOM.addEventListener('scroll', function () {
          /**
            * scrollHeight 获取元素内容高度
            * scrollTop 获取或者设置元素的偏移值,常用于计算滚动条的位置,当一个元素的容器没有产生垂直方向的滚动条,那它的scrollTop值默认为0
            * clientHeight 获取元素的可见高度
            * 如果元素滚动到底,下面的等式返回true,没有则返回false
            * 
          **/
          const condition = (this.scrollHeight - this.scrollTop) <= (this.clientHeight + 10);
          if (condition) {
            binding.value();
          }
        })
      }
    }
  },

三、绑定方法

 data() {
    return {
      typeList: [],
      typeQuery: {
        pageNum: 1,
        pageSize: 10,
        materialName: ""
      },
      typeTotal: 0,
      selectLoading: false,
    };
  },
  methods: {
    // 获取后端数据
    getTypeList() {
      this.selectLoading = true

      const params = new URLSearchParams();
      for (const key in this.typeQuery) {
        params.append(key, this.typeQuery[key]);
      }
      getTypeList(params).then(
        response => {
          this.selectLoading = false
          this.typeList = [...this.typeList, ...response.rows];
          this.typeTotal = response.total
        }
      );
    },
    
    //下拉框搜索方法
    remoteMethod(query) {
      this.typeQuery.materialName = query
      this.typeQuery.pageNum = 1
      this.typeList = []
      this.getTypeList()
    },
    
    //滚动加载
    loadmore() {
      if (this.typeTotal > this.typeList.length) {
        this.typeQuery.pageNum++;
        this.getTypeList();
      }
    },
  

  },
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值