element-ui 下拉框搜索和更多

一:需求

element-ui的el-select下拉更多和远程搜索
https://element.eleme.cn/#/zh-CN/component/select#methods
在这里插入图片描述

二:工具类的封装

class Pages {
  constructor(options = {}) {
    this.options = {
      page: 0,
      pageSize: 10,
      hasNext: true,
      total: 0,
      fetch: () => {},
      ...options
    }
    return this
  }

  setTotal(total) {
    this.options.total = total
    if (Math.ceil(total / this.options.pageSize) <= this.options.page) {
      this.options.hasNext = false
    }
  }

  setPage(page) {
    this.options.page = page
    // this.options.fetch({ page: this.options.page, pageSize: this.options.pageSize })
  }

  reset() {
    this.options.page = 1
    this.options.total = 0
    this.options.hasNext = true
    console.log(123,this)
    this.options.fetch({ page: this.options.page, pageSize: this.options.pageSize }, ...arguments)
  }

  refresh() {
    this.options.fetch({ page: this.options.page, pageSize: this.options.pageSize })
  }

  nextPage() {
    const { hasNext, fetch, pageSize } = this.options
    this.options.page++
    if (hasNext) {
      fetch({ page: this.options.page, pageSize }, ...arguments)
    }
    return this
  }
}

export default Pages

三:自定义滑动指令封装

import Vue from 'vue'
export default () => {
  Vue.directive('scroll', {
    bind (el, binding) {
      let SCROLL_DOM = el.querySelector('.el-select-dropdown .el-select-dropdown__wrap')
      SCROLL_DOM.addEventListener('scroll', function () {
        const scrollDistance = this.scrollHeight - this.scrollTop - this.clientHeight
        if (scrollDistance === 0) {
          binding.value(true)
        }
      })
    }
  })
}

四:业务代码实现

<template>
	<el-select 
    	v-model="ddForm.distributor_id" 
	    v-scroll="() => this.pageQuery.nextPage()" 
	    clearable 
	    @clear="() => this.onRemoteGetMerchant()" 
	    @focus="() => this.onRemoteGetMerchant()" 
	    filterable
	    :loading="remoteLoading" remote 
	    :remoteMethod="(e) => this.onRemoteGetMerchant(e)"
	  >
    	<el-option 
    		v-for="(item, index) in distributorList" 		
    	 	:key="`option-item__${index}`" 
    		:label="item.title"
      		:value="item.value" />
  </el-select>

</template>

//data省略
created() {
    this.pageQuery = new Pages({
      pageSize: 10,
      fetch: this.getDistributorList
    }).nextPage()
  }
  
 methods: {
    onRemoteGetMerchant(e) {
      this.distributorList = []
      this.pageQuery.reset(e)
    },
    async getDistributorList({ page, pageSize }, keywords) {
      let params = {
        pageSize,
        page
      }
      if (!isEmpty(keywords)) {
        params = {
          ...params,
          name: keywords
        }
      }

      if (page == 1) {
        this.remoteLoading = true
      }
      const { list, total_count } = await this.$api.member.getMemberDistributor(params)
      if (page == 1) {
        this.remoteLoading = false
      }
      this.pageQuery.setTotal(total_count)
      this.distributorList = this.distributorList.concat(
        list.map((k) => {
          return {
            title: `${k.shop_code}${k.name}`,
            value: k.distributor_id
          }
        })
      )
    },

在这里插入图片描述注:el-select中使用箭头函数,因为remoteMethod需要传e,获取到输入的值,而其他两个无需传e, @focus=“onRemoteGetMerchant” 这样写,focous时会打印keywords是e!注意此写法!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值