直接贴代码:
<template>
<div style="margin: 200px;">
<el-select v-model="qqqq" v-el-select-loadmore="loadmore" placeholder="滚动加载接口数据">
<el-option v-for="(dict,index) in selectUser" :key="index" :label="dict.name" :value="dict.id" />
</el-select>
</div>
</template>
<script>
export default {
directives: {
'el-select-loadmore': {
bind(el, binding) {
// 获取element-ui定义好的scroll盒子
const scrollDom = el.querySelector('.el-select-dropdown .el-select-dropdown__wrap');
scrollDom.addEventListener('scroll', function() {
/**
* scrollHeight 获取元素内容高度(只读)
* scrollTop 获取或者设置元素的偏移值,常用于, 计算滚动条的位置, 当一个元素的容器没有产生垂直方向的滚动条, 那它的scrollTop的值默认为0.
* clientHeight 读取元素的可见高度(只读)
* 如果元素滚动到底, 下面等式返回true, 没有则返回false:
* ele.scrollHeight - ele.scrollTop === ele.clientHeight;
*/
const condition = this.scrollHeight - this.scrollTop <= this.clientHeight;
if (condition) {
binding.value();
}
});
}
}
},
data() {
return {
qqqq: '',
selectUser: [],
currentPage: 0, // 分页参数 第几页
pagesize: 10, // 分页参数 每页多少条数据
data:[]
}
},
methods: {
mockData() {
this.data = []
for (let i = this.currentPage; i < this.currentPage*this.pagesize; i++) {
this.data.push({
name: `张${i}`,
id: i
})
}
},
// 下拉懒加载自定义方法
loadmore() {
this.getListMedia()
},
//模拟接口数据
getListMedia() {
setTimeout(() => {
this.currentPage ++
this.data = []
for (let i = this.currentPage; i < this.currentPage*this.pagesize; i++) {
this.data.push({
name: `张${i}`,
id: i
})
}
this.selectUser = [...this.selectUser, ...this.data]
console.log(this.currentPage)
}, 0)
}
},
created() {
this.getListMedia()
}
}
</script>
结果: