vue3+el-select實(shí)現(xiàn)觸底加載更多功能(ts版)
思路
使用全局指令來實(shí)現(xiàn)觸底加載功能
通過
el-select
中的popper-class
給下拉框加入一個(gè)類名
在src下的utils文件夾中創(chuàng)建selectLoadMoreDirective.ts文件
import { Directive, DirectiveBinding } from 'vue' const loadMore: Directive = { beforeMount(el: any, binding: DirectiveBinding) { console.log(el) const selectDom = (document.querySelector(".more_select_dropdown") as any).querySelector(".el-select-dropdown .el-select-dropdown__wrap"); console.log(selectDom, 'selectDom++++++++++++++'); function loadMores(this: any) { const isBase = this.scrollHeight - this.scrollTop <= this.clientHeight if (isBase) { binding.value && binding.value() } } el.selectDomInfo = selectDom el.userLoadMore = loadMores nextTick(() => { selectDom?.addEventListener('scroll', loadMores) }) }, beforeUnmount(el: any) { if (el.userLoadMore) { el.selectDomInfo.removeEventListener('scroll', el.userLoadMore) delete el.selectDomInfo delete el.userLoadMore } } } export default loadMore
main.ts
import { createApp } from 'vue' import App from './App.vue' import router from '/@/router' import loadMore from '@/utils/selectLoadMoreDirective' const app = createApp(App) app.use(router) app.directive('loadMore', loadMore)
home.vue
<template> <div class="p-2"> <el-select v-model="selectedValue" v-loadMore="loadMore" class="more-select" popper-class="more_select_dropdown" @change="selectChange"> <el-option v-for="(item, index) in list" :key="`${item.value}${index}`" :label="item.label" :value="item.value" /> </el-select> </div> </template> <script setup name="Home" lang="ts"> import { ComponentInternalInstance, computed, getCurrentInstance, nextTick, onMounted, reactive, ref, toRefs } from 'vue'; const selectedValue = ref([]) const selectData = reactive({ pageIndex: 1, pageSize: 5, finished: false }) const list = ref([ { value: 1, label: '測試1' }, { value: 2, label: '測試2' }, { value: 3, label: '測試3' }, { value: 4, label: '測試4' }, { value: 5, label: '測試5' }, { value: 6, label: '測試6' }, { value: 7, label: '測試7' }, { value: 8, label: '測試8' }, ]) //觸底函數(shù) const loadMore = () => { console.log(' 觸底了'); // 防抖處理 setTimeout(() => { if (selectData.finished) return //值為true,則代表沒有數(shù)據(jù)了 selectData.pageIndex += 1 // list.value = list.value.concat(list2.value) for (let i = 1; i <= 8; i++) { list.value.push({ value: i, label: `測試${i}` }) } }, 500) } //選中值發(fā)生變化時(shí)觸發(fā) const selectChange = () => { console.log('選中的xxxx') } </script>
到此這篇關(guān)于vue3+el-select實(shí)現(xiàn)觸底加載更多功能的文章就介紹到這了,更多相關(guān)vue3+el-select 加載更多功能內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決iview table組件里的 固定列 表格不自適應(yīng)的問題
這篇文章主要介紹了解決iview table組件里的 固定列 表格不自適應(yīng)的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11Vue之Vue.set動(dòng)態(tài)新增對象屬性方法
下面小編就為大家分享一篇Vue之Vue.set動(dòng)態(tài)新增對象屬性方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-02-02Vue項(xiàng)目引用百度地圖并實(shí)現(xiàn)搜索定位等功能(案例分析)
這篇文章主要介紹了Vue項(xiàng)目引用百度地圖并實(shí)現(xiàn)搜索定位等功能(案例分析),本篇文章為案例分析,技術(shù)點(diǎn)較多,所以篇幅較長,認(rèn)真閱覽的你一定會學(xué)到很多知識,需要的朋友可以參考下2022-09-09vue使用動(dòng)畫實(shí)現(xiàn)滾動(dòng)表格效果
這篇文章主要為大家詳細(xì)介紹了vue使用動(dòng)畫實(shí)現(xiàn)滾動(dòng)表格效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04vue監(jiān)聽滾動(dòng)事件實(shí)現(xiàn)滾動(dòng)監(jiān)聽
本文主要介紹了vue監(jiān)聽滾動(dòng)事件實(shí)現(xiàn)滾動(dòng)監(jiān)聽的相關(guān)資料。具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-04-04vue+webpack實(shí)現(xiàn)異步加載三種用法示例詳解
這篇文章主要介紹了vue+webpack實(shí)現(xiàn)異步加載的三種用法,文中給大家提到了vue+webpack實(shí)現(xiàn)異步組件加載的代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2018-04-04淺談vue單頁面中有多個(gè)echarts圖表時(shí)的公用代碼寫法
這篇文章主要介紹了淺談vue單頁面中有多個(gè)echarts圖表時(shí)的公用代碼寫法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07