vue使用el-select无限加载数据,实现滚动加载,远程搜索

由于接口返回数据好几千条,所以使用滚动加载。

首先写vue 自定义指令:loadmore.js

// select下拉滚动加载//
const loadmore = {
  bind(el, binding) {
    const selectDom = el.querySelector('.el-select-dropdown .el-select-dropdown__wrap')
    // console.log("滚动加载>>>", selectDom)
    selectDom.addEventListener('scroll', function () {
      const height = this.scrollHeight - Math.ceil(this.scrollTop) <= (this.clientHeight+1)
      // if (height) {
      //   binding.value()
      // }
      if (height && this.scrollTop !== 0) {
        binding.value();
      }
    })
  }
}
export default loadmore

然后在页面使用自定义指令:

引入自定义指令:

import Vue from 'vue'
import selectLoadMore from '@/utils/loadmore.js'
const directives = {
  selectLoadMore
}
Object.keys(directives).forEach(name => Vue.directive(name, directives[name]))

使用: 

<el-col>
          <el-form-item label="选择发送人:" prop="sendUserEipsArray">
            <el-select 
              filterable 
              clearable 
              multiple 
              v-model="formData.sendUserEipsArray" 
              placeholder="请选择"
              @blur="query=''"
              v-selectLoadMore="()=>selectLoadMore()"
              @change="userChange"
              remote
              :remote-method="(query)=>remoteMethod(query,'remote')"
              loading-text="加载中"
              :loading="loading">
              <el-option
                v-for="item in sendUers"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
//请求接口:
async remoteMethod(query, type){
       // 远端搜索
      if (type == 'remote') {
        this.query = query || ''
        this.condition.pageNo = 1;
        this.condition.username=this.query
        this.total = 0;
        this.sendUers = []
      } else if (type == 'scroll') {
          // 滚动搜索
        // this.condition.username=this.query
      } else {
        // 初次加载
        this.condition={
          pageSize:20,
          pageNo: 1,
          realName: '',
          username: '',
          phone: '',
          roleId: '',
        },
        this.total = 0;
        this.sendUers = []
      }
      const res = await getAction(`接口地址`, {
        status: 1,
        ...this.condition
      })
      this.loading = true;
      if(res.data.success){
        this.loading = false;
        console.log('用户管理列表>>>',res.data.result)
        // 初次搜索和远端搜索,需要重置总数
        if (!type || type == 'remote') {
          this.total = res.data.result.total
        }
        let arr=[]
        arr=res.data.result.records.map(ele=>{
          return {
            value:ele.username,
            label:`${ele.realName}(${ele.username})`
          }
        })
        arr.forEach(ite=>{
          this.sendUers.push(ite)
        })
        // this.sendUers=arr
        console.log('options处理过的>>>',this.sendUers)
      } else {
        this.loading = false;
        this.$message.warning(res.data.message)
      }
      
    },

//方法:
// 滚动条-触底加载下一页
   selectLoadMore(){
      console.log("触底加载。。。。")
      // 如果超出最大页码,停止
      if (Math.ceil(this.total / this.condition.pageSize) == this.condition.pageNo) {
        return;
      }
      this.condition.pageNo += 1
      this.remoteMethod(this.query, 'scroll')
    },
mounted(){
this.remoteMethod()
}


对于 el-select 组件的滚动加载,你可以使用 el-select 的 remote 方法配合分页来实现。以下是一个简单的示例代码: ```html <template> <el-select v-model="selectedValue" filterable remote reserve-keyword :remote-method="remoteMethod" :loading="loading" :options="options" :total="total" @visible-change="handleVisibleChange" ></el-select> </template> <script> export default { data() { return { selectedValue: '', // 选择的值 options: [], // 下拉选项 total: 0, // 总数 loading: false, // 加载状态 page: 1, // 当前页码 pageSize: 10, // 每页显示数量 }; }, methods: { // 远程搜索方法,根据输入关键字获取数据 remoteMethod(query) { this.loading = true; // 模拟异步请求数据,你需要根据实际情况调用接口获取数据 setTimeout(() => { // 假设返回的数据结构为 { data: [], total: 100 } const response = { data: [], // 实际数据数组 total: 100, // 总数 }; this.options = response.data; this.total = response.total; this.loading = false; }, 1000); }, // 滚动加载下一页数据 handleVisibleChange(visible) { if (visible && this.page * this.pageSize < this.total) { this.page += 1; this.loading = true; // 模拟异步请求数据,你需要根据实际情况调用接口获取下一页数据 setTimeout(() => { // 假设返回的数据结构为 { data: [] } const response = { data: [], // 实际数据数组 }; this.options = [...this.options, ...response.data]; this.loading = false; }, 1000); } }, }, }; </script> ``` 在上面的代码中,el-select 组件被设置为远程搜索模式(remote),并且使用了 remote-method 属性来指定远程搜索的方法。在 remoteMethod 方法中,你可以根据输入的关键字从服务器获取相关数据,并更新 options 和 total 属性。每次下拉框出现时,会触发 handleVisibleChange 方法,你可以在该方法中判断是否需要加载下一页数据,并在请求完成后更新 options 属性。 请注意,上述示例代码仅为演示用途,实际的接口调用和数据处理需要根据你的具体需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值