使用elmentui中的el-select实现输入框搜索、滚动加载功能

效果

1、组件代码

<template>
  <div :style="{display:'inline-block', width: width}">
    <el-select
      v-model="c_id"
      v-loadmore="{fn:loadMore ,type:'remoteMethodCustomer'}"
      :style="{'width': width}"
      filterable
      remote
      :remote-method="remoteMethodCustomer"
      :loading="selectLoadingCustomer"
      placeholder="请选择"
      clearable
      @change="selectChange"
      @clear="clear"
    >
      <el-option
        v-for="item in customerData"
        :key="item.value"
        :label="item.label"
        :value="item.value"
      />
      <div v-if="selectLoadingCustomerAdd && !customerDataNull">
        <span style=" display: flex; justify-content: center;">加载中.....</span>
      </div>
      <div v-if="customerDataNull">
        <span style=" display: flex; justify-content: center;">到底了.....</span>
      </div>
    </el-select>
  </div>
</template>

<script>
import Business from '@/api/business'
import fn from '@/utils/fn'
export default {
  props: {
    width: {
      type: String,
      default: ''
    },
    fatherOption: {
      type: Object,
      default: () => ({})
    },
    cid: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      c_id: '',
      customerData: [],
      remoteMethodCustomerParams: {},
      selectLoadingCustomer: false,
      customerDataNull: false,
      selectLoadingCustomerAdd: false
    }
  },
  watch: {
    fatherOption: {
      immediate: true,
      deep: true,
      handler(val) {
        if (JSON.stringify(val) !== '{}') {
          const existingObject = this.customerData.some(item => item.value === val.value)
          if (!existingObject) {
            this.customerData.push(val)
          }
        }
      }
    },
    cid: {
      immediate: true,
      handler(val) {
        this.c_id = val
      }
    }
  },
  methods: {
    customerDataSelectFn: fn.debounce(async function(params, add) {
      this.selectLoadingCustomerAdd = true
      Business.getCustomerDataResident(params).then(res => {
        const customerData = []
        res.data.forEach(item => {
          customerData.push({
            value: item.id,
            label: '简:' + item.abbr + ' --- ' + '全:' + item.name
          })
        })
        if (customerData.length < 10) {
          this.customerDataNull = true
        } else {
          this.customerDataNull = false
        }
        if (add) {
          this.customerData.push(...customerData)
        } else {
          this.customerData = customerData
        }
        this.selectLoadingCustomerAdd = false
        this.selectLoadingCustomer = false
      })
    }, 200),
    loadMore(type) {
      this.remoteMethodCustomerParams.page++
      this.customerDataSelectFn({ ...this.remoteMethodCustomerParams }, true)
    },
    remoteMethodCustomer(query) {
      const that = this
      if (query !== '') {
        this.selectLoadingCustomer = true
        const params = {
          limit: 10,
          search: query,
          page: 1
        }
        this.remoteMethodCustomerParams = params
        this.customerDataSelectFn(params)
      } else {
        that.customerData = []
      }
    },
    selectChange(row) {
      this.$emit('selectChange', row)
    },
    clear() {
      this.c_id = ''
    }
  }
}
</script>

<style>

</style>

2、最重要的自定义指令实现v-loadmore

// select下拉刷新
Vue.directive('loadmore', {
  inserted(el, binding, vnode) {
    const SELECTWRAP_DOM = el.querySelector('.el-select-dropdown .el-select-dropdown__wrap')
    if (SELECTWRAP_DOM) {
      SELECTWRAP_DOM.addEventListener('scroll', function() {
        const condition = this.scrollHeight - this.scrollTop <= this.clientHeight
        const type = binding.value.type
        // const fn = binding.value.fn
        if (condition) {
          binding.value.fn(type)
        }
      })
    }
  }
})

3、template 

   <SelectOptionCustomer :width="'100%'" :father-option="fatherOption" :cid="ruleForm.cid" @selectChange="customerSelectChange" />

 4.data

  fatherOption: {},
  ruleForm: {
      cid: null
   },

5.methods

    // 选择企业
    customerSelectChange(row) {
      this.ruleForm.cid = row
    },

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值