Element下拉框?qū)崿F(xiàn)滾動(dòng)加載更多功能實(shí)現(xiàn)

Element下拉框?qū)崿F(xiàn)滾動(dòng)加載更多功能實(shí)現(xiàn)

需求:下拉框默認(rèn)顯示20條數(shù)據(jù),可使用遠(yuǎn)程搜索為顯示的數(shù)據(jù),但是部分用戶喜歡滾動(dòng)選擇。

如圖所示,el-select官方事件并沒有監(jiān)聽滾動(dòng)的事件,所以我們可以采用vue的directives自定義指令實(shí)現(xiàn)。


el-selectEvents.png

首選在src也就是main.js的同級目錄下新建一個(gè)directives.js文件

import Vue from 'vue'
let MyDirective = {}
export default MyDirective.install = function(vue, options) {
  Vue.directive('loadmore', {
    bind (el, binding) {
      const selectDom = el.querySelector('.el-select-dropdown .el-select-dropdown__wrap')
      selectDom.addEventListener('scroll', function () {
        const isEnd = this.scrollHeight - this.scrollTop <= this.clientHeight
        if (isEnd) {
          binding.value()
        }
      })
    }
  })
}

在main.js中引入并注冊

import directives from './directives'
// 全局注冊指令
Vue.use(directives)

index.vue 在下拉框中加入v-loadmore=“加載事件名”即可實(shí)現(xiàn)

<el-select
  v-loadmore="loadPartner"
  v-model="listQuery.partner_id"
  placeholder="請選擇或搜索團(tuán)隊(duì)"
  filterable clearable
  :filter-method="searchPartner"
  @change="handleFilter"
  @clear="searchPartner('',false)"
  @blur="searchPartner('')"
  class="input-item">
  <el-option
    v-for="item in partners"
    :key="item.id"
    :label="item.name"
    :value="item.id">
  </el-option>
</el-select>

注意:其他細(xì)節(jié)自行注意,例如加載到底或者下拉框有聯(lián)動(dòng)等。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容