select下拉框滚动加载

element-ui版本使用自定义指令:

main.js:

// 滚动加载更多
Vue.directive('loadMore', {
  bind(el, binding) {
    // 获取element,定义scroll
    let select_dom = el.querySelector('.el-select-dropdown .el-select-dropdown__wrap');
    select_dom.addEventListener('scroll', function () {
      let height = this.scrollHeight - this.scrollTop == this.clientHeight;
      if (height) {
        binding.value()
      }
    })
  }
})

组件中:

<template>
  <el-select class="my-el-select"  v-model="xxx" placeholder="xxx" @change="change" 
  :filter-method="filterMethod" v-loadMore="loadMore" clearable filterable>
      <el-option v-for="item in list" :key="item.id" :label="item.name"  
      :value="item.id">
      </el-option>
  </el-select>
</template>

<script>
  ...

  methods: {
    // 加载更多
    loadMore () {
      this.pageNum++
      // 获取列表
      this.getList()
    },

    // 搜索
    filterMethod(val) {
      this.getList()
    },

    // 事件
    change (val) {
      ...
    }
  }
</script>

antd-vue 版本

<template>
    <a-select show-search allowClear  v-model="xxx"  placeholder="请选择" 
      style="width: 200px" :show-arrow="false"  :filter-option="false"  :not-found- 
      content="null" :options="List"  @popupScroll="handlePopupScroll"  
      @search="handleSearch" />
</template>

<script>
  ...
  
   methods: {
      // 搜索
      handleSearch(value) {
        // 获取列表
        this.getList()
      },

      //  滚动加载
      handlePopupScroll(value) {
        const { scrollHeight, scrollTop, clientHeight } = value.target;
        console.log(scrollHeight, scrollTop, clientHeight)
        if (scrollHeight - scrollTop === clientHeight) {
          this.pageNum++;
          // 获取列表
          this.getList();
        }
      }
    }
</script>

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现下拉框触底加载下一页,可以使用 `element-ui` 的 `el-select` 和 `el-option` 组件结合 `scroll` 事件来实现。具体操作如下: 1. 在 `el-select` 组件中添加 `@scroll.native` 事件,绑定一个方法,用于监听下拉框滚动事件。 2. 在绑定的方法中,判断下拉框是否滚动到底部,如果滚动到底部,则触发加载下一页的方法,并将下一页的数据添加到选项中。 3. 在 `el-option` 组件中添加一个指令 `v-if`,用于判断当前选项是否需要显示。当选项数据为空时,不显示该选项。 下面是一个示例代码: ```html <template> <el-select v-model="value" @scroll.native="handleScroll" :filterable="filterable"> <el-option v-for="option in options" :key="option.value" :label="option.label" :value="option.value" v-if="option.label !== 'loading'"></el-option> <el-option v-if="loading" label="loading"> <template #loading> <i class="el-icon-loading"></i> </template> </el-option> </el-select> </template> <script> export default { data() { return { value: '', options: [], loading: false, filterable: true, page: 1, }; }, methods: { handleScroll(e) { const container = e.target; const scrollHeight = container.scrollHeight; const offsetHeight = container.offsetHeight; const scrollTop = container.scrollTop; if (scrollTop + offsetHeight >= scrollHeight && !this.loading) { this.loading = true; this.loadNextPage(); } }, async loadNextPage() { // 模拟异步加载数据 await new Promise((resolve) => setTimeout(resolve, 1000)); const nextPage = this.page + 1; const newOptions = Array.from({ length: 10 }, (_, index) => ({ label: `Option ${nextPage}${index + 1}`, value: `option${nextPage}${index + 1}`, })); this.options = this.options.concat(newOptions); this.page = nextPage; this.loading = false; }, }, }; </script> ``` 这样,当下拉框滚动到底部时,就会触发加载下一页的方法,并将下一页的数据添加到选项中,从而实现了下拉框触底加载下一页的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值