element select 滚动加载 异步查询

1 先给vue新增loadmore指令

// main.js 中载入指令文件

import '@/utils/element-loadmore.js'
// utils/element-loadmore.js 中添加loadmore指令

import Vue from 'vue'
Vue.directive('loadmore', {
  bind(el, binding) {
    const SELECTWRAP_DOM = el.querySelector('.el-select-dropdown .el-select-dropdown__wrap')
    SELECTWRAP_DOM.addEventListener('scroll', function() {
      const CONDITION = this.scrollHeight - this.scrollTop <= this.clientHeight
      if (CONDITION) {
        binding.value()
      }
    })
  }
})

2 编写通用插件和引用

// 组件 RemoteSelectSearch.vue(有些参数没有抽取和添加,可自行优化)

<template>
  <el-select
    v-model="selectVal"
    v-loadmore="loadMore"
    clearable
    filterable
    :placeholder="placeholder"
    remote
    :remote-method="remoteMethod"
    @visible-change="getEnterpriseName"
    @change="val => { $emit('selectChangeValue', val) }"
  >
    <el-option
      v-for="item in remoteItems"
      :key="item.id"
      :value="item.id"
      :label="item.realName"
    />
  </el-select>
</template>

<script>
export default {
  props: {
    initRemoteItems: { // 搜索选择列表
      type: Array,
      default: _ => []
    },
    initVal: { // 默认值
      type: [Number, String],
      default: null
    }
  },
  data() {
    return {
      selectVal: this.initVal || null,
      placeholder: '请输入关键词',

      remoteApi: '/sPartyGroup/getSPartyGroupUsers',
      remoteItems: this.initRemoteItems || [], // api远程返回的数据
      searchIndex: 1, // api远程搜索页数
      searchVal: '' // api远程搜索内容
    }
  },
  watch: {
    initVal(newVal) {
      this.selectVal = newVal
    },
    initRemoteItems(newVal) {
      this.remoteItems = newVal
    }
  },
  methods: {
    // 远程初始化搜索
    getEnterpriseName() {
      this.searchVal = ''

      const params = {
        pageIndex: 1,
        pageSize: 20,
        search: this.searchVal
      }

      if (this.searchIndex !== 1) return

      this.remoteItems = []
      this.$post(this.remoteApi, params, 1).then(({ data }) => {
        this.remoteItems = data.records || []
        console.log('this.remoteItems1', this.remoteItems)
      })
    },
    // 远程滚动加载
    loadMore() {
      this.searchIndex++

      const params = {
        pageIndex: this.searchIndex,
        pageSize: 20,
        search: this.searchVal
      }

      this.$post(this.remoteApi, params, 1).then(({ data }) => {
        (data.records || []).map((item) => {
          this.remoteItems.push(item)
        })
        console.log('this.remoteItems2', this.remoteItems)
      })
    },

    // 远程搜索
    remoteMethod(inputValue) {
      this.searchIndex = 1
      this.searchVal = inputValue

      const params = {
        pageIndex: 1,
        pageSize: 20,
        search: this.searchVal
      }

      this.$post(this.remoteApi, params, 1).then(({ data }) => {
        this.remoteItems = data.records || []
        console.log('this.remoteItems3', this.remoteItems)
      })
    }
  }
}
</script>
// 多个相同组件记得加key

<RemoteSelectSearch 
    key="secretary" 
    ref="secretary" 
    class="wd100" 
    :init-remote-items="[id: 1, realName: 'promise@w']" 
    :init-val="editData.secretaryId" 
    @selectChangeValue="val => { editData.secretaryId = val }" 
/>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在element的el-select组件中实现滚动加载更多,可以使用以下步骤: 1. 将el-select的popper-class属性设置为一个自定义类名,以便样式定制。 2. 在自定义类名的样式中,将popper的max-height设置为一个较小的值,以便在下拉列表中只显示部分选项。 3. 使用Vue的scroll事件监听器来检测下拉列表的滚动位置,并在滚动到底部时触发加载更多数据的函数。 4. 在触发加载更多数据的函数中,将新数据添加到原来的选项列表中,并更新下拉列表的高度以显示所有选项。 以下是一个简单的示例代码: HTML模板: ```html <el-select v-model="selected" popper-class="my-popper" @visible-change="handleVisibleChange"> <el-option v-for="option in options" :key="option.value" :label="option.label" :value="option.value"></el-option> </el-select> ``` JavaScript代码: ```js export default { data() { return { selected: '', options: [], loading: false, page: 1, perPage: 10, } }, methods: { handleVisibleChange(visible) { if (visible && this.options.length === 0) { this.loadData() } }, loadData() { this.loading = true // 模拟异步加载数据 setTimeout(() => { const newOptions = [] for (let i = 0; i < this.perPage; i++) { const value = (this.page - 1) * this.perPage + i newOptions.push({ label: `Option ${value}`, value, }) } this.options = [...this.options, ...newOptions] this.page++ this.loading = false }, 1000) }, handleScroll(event) { const target = event.target const distance = target.scrollHeight - target.scrollTop - target.clientHeight if (distance < 10 && !this.loading) { this.loadData() } }, }, mounted() { this.$nextTick(() => { const popper = this.$refs.select.$refs.popper popper.addEventListener('scroll', this.handleScroll) }) }, beforeDestroy() { const popper = this.$refs.select.$refs.popper popper.removeEventListener('scroll', this.handleScroll) }, } ``` 在上面的代码中,handleVisibleChange方法用于在下拉列表显示时自动加载第一页数据,loadData方法用于异步加载更多数据,handleScroll方法用于监听下拉列表的滚动事件并在需要时触发加载更多数据的操作。在mounted钩子函数中,我们使用$refs获取下拉列表的popper元素,并添加scroll事件监听器。在beforeDestroy钩子函数中,我们移除scroll事件监听器以避免内存泄漏。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值