vue2+element-ui 实现下拉框滚动加载

一、自定义滚动指令。

VUE.directive(

'el-select-loadmore': {

bind(el, binding) {

const SELECTWRAP_DOM = el.querySelector('.el-select-dropdown .el-select-dropdown__wrap')

SELECTWRAP_DOM.addEventListener('scroll', function () {

/**

* scrollHeight 获取元素内容高度

* scrollTop 获取或者设置元素的偏移值,常用于计算滚动条的位置,当一个元素的容器没有产生垂直方向的滚动条,那它的scrollTop值默认为0

* clientHeight 获取元素的可见高度

* 如果元素滚动到底,下面的等式返回true,没有则返回false

**/

const condition = (this.scrollHeight - this.scrollTop) <= (this.clientHeight + 10);

if (condition) {

binding.value();

}

})

}

}

})

二、绑定方法。

data() {

return {

adGroupList: [],

adGroupQuery: {

pageNum: 1,

pageSize: 10

},

typeTotal: 0,

selectLoading: false,

};

},

methods: {

// 获取后端数据

getAdGroupList() {

this.selectLoading = true

const params = {

page: this.adGroupQuery.currentPage,

pageSize: this.adGroupQuery.pageSize,

param: {

adGroup: this.adGroupQuery.adGroup

}

}

getAdGroupList(params).then(

response => {

this.selectLoading = false

this.adGroupList = [...this.adGroupList, ...response.result];

this.typeTotal = response.count

}

);

},

//下拉框搜索方法

remoteMethod(query) {

this.adGroupQuery.adGroup= query

this.adGroupQuery.pageNum = 1

this.adGroupList= []

this.getAdGroupList()

},

//滚动加载

loadmore() {

if (this.typeTotal > this.adGroupList.length) {

this.adGroupQuery.pageNum++;

this.getAdGroupList();

}

}

},

三、组件绑定属性。

<el-select

v-model="form.materialId"

v-el-select-loadmore="loadmore"

placeholder="请输入AD组查询"

filterable

remote

clearable

:remote-method="remoteMethod"

style="width:100%">

<el-option v-for="(item, index) in typeList" :key="index" :label="item.name" :value="item.id">

</el-option>

</el-select>

相关推荐
万邦科技Lafite1 小时前
京东按图搜索京东商品(拍立淘) API (.jd.item_search_img)快速抓取数据
开发语言·前端·数据库·python·电商开放平台·京东开放平台
Never_Satisfied3 小时前
在JavaScript / Node.js / 抖音小游戏中,使用tt.request通信
开发语言·javascript·node.js
一只小透明啊啊啊啊4 小时前
Java Web 开发的核心组件:Servlet, JSP,Filter,Listener
java·前端·servlet
你的人类朋友5 小时前
设计模式有哪几类?
前端·后端·设计模式
Yeats_Liao5 小时前
Go Web 编程快速入门 10 - 数据库集成与ORM:连接池、查询优化与事务管理
前端·数据库·后端·golang
啃火龙果的兔子5 小时前
前端八股文react篇
前端·react.js·前端框架
打小就很皮...5 小时前
React 实现 i18next 中英文切换集成
前端·react.js·i18next
拉不动的猪5 小时前
函数组件和异步组件
前端·javascript·面试
淮北4946 小时前
html + css +js
开发语言·前端·javascript·css·html
麦麦大数据6 小时前
F036 vue+flask中医热性药知识图谱可视化系统vue+flask+echarts+mysql
vue.js·python·mysql·flask·可视化·中医中药