Demo: elementplus中select实现触底加载数据(懒加载)

发布时间:2024年01月20日

elementplus中select实现触底加载数据(懒加载)

自定义指令 directives.js

const loadMore = {
  beforeMount(el, binding) {
    // 常规获取dom
    // const selectDom = document.querySelector('.el-select-dropdown .el-select-dropdown__wrap')
    let selectDomPro = document.querySelector('.popperCustom')
    let firstSelectItem = ''
    if (selectDomPro) {
      firstSelectItem = selectDomPro?.querySelector(
        '.el-select-dropdown .el-scrollbar .el-select-dropdown__wrap'
      )
    }
    function loadMores() {
      const isBase = this.scrollHeight - (this.scrollTop + 3) <= this.clientHeight
      if (isBase) {
        binding.value && binding.value()
      }
    }
    el.selectDomInfo = firstSelectItem
    el.userLoadMore = loadMores
    firstSelectItem?.addEventListener('scroll', loadMores)
  },
  beforeUnmount(el) {
    if (el.userLoadMore) {
      el.selectDomInfo.removeEventListener('scroll', el.userLoadMore)
      delete el.selectDomInfo
      delete el.userLoadMore
    }
  }
}
export default loadMore

使用

<!-- 下拉触底加载--懒加载 -->
            <el-select
              v-else-if="item.type == 'selectPro'"
              v-model="form[item.prop]"
              :placeholder="item.placeholder || `请选择${item.label}`"
              :clearable="item.clearable"
              :disabled="item.disabled"
              :filterable="item.filterable"
              :style="{ width: item.width ? item.width + 'px' : '100%' }"
              v-load-more="loadMoreItems"
              popper-class="popperCustom"
            >
              <el-option
                v-for="option in item.data"
                :key="option.value"
                :label="option.label"
                :value="option.value"
                :disabled="option.disabled"
              />
            </el-select>

事件处理函数

// 加载更多
const loadMoreItems = () => {
  console.log('loadMoreItems')
}

全局挂载 main.js

import directives from '@/utils/directives'

app.directive('load-more', directives)
文章来源:https://blog.csdn.net/qq_52696618/article/details/135697433
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。