Element 自定义指令 下拉分页,获取无限数据
template 代码
<el-form-item>
<el-select
v-model="form.batchId"
v-loadmore="loadmoreBatchList"
placeholder="请输入批次名称"
filterable
clearable>
<el-option v-for="(item, key) in batchList.list" :key="key" :label="item.batchName" :value="item.id"></el-option>
</el-select>
</el-form-item>
data 下拉数据:
batchList: {
list: [],
total: 0,
pageNo: 1,
pageSize: 100
},
methods 方法:
import { uniqBy } from 'lodash'
loadmoreBatchList() {
if (this.batchList.total > this.batchList.list.length) {
this.getBatchList(data, ++this.batchList.pageNo)
}
},
getBatchList(data, pageNo = 1) {
// data 为接口传递的自定义数据
this.$store.dispatch('getOutboundBatchsList', {
...data,
pageNo: pageNo,
pageSize: this.batchList.pageSize
})
.then(({ code = 0, data = {} }) => {
if (code == 1 && data.total) {
this.batchList = {
...this.batchList,
list: uniqBy([...this.batchList.list, ...data.list], 'id'),
total: data.total
}
}
})
},
loadmore 自定义指令
Vue.directive('loadmore', {
bind(el, binding) {
// 获取element-ui定义好的scroll盒子
const SELECTWRAP_DOM = el.querySelector(
'.el-select-dropdown .el-select-dropdown__wrap'
)
if (SELECTWRAP_DOM) {
SELECTWRAP_DOM.addEventListener('scroll', function() {
/*
* scrollHeight 获取元素内容高度(只读)
* scrollTop 获取或者设置元素的偏移值,常用于, 计算滚动条的位置, 当一个元素的容器没有产生垂直方向的滚动条, 那它的scrollTop的值默认为0.
* clientHeight 读取元素的可见高度(只读)
* 如果元素滚动到底, 下面等式返回true, 没有则返回false:
* ele.scrollHeight - ele.scrollTop === ele.clientHeight;
*/
const CONDITION =
this.scrollHeight - this.scrollTop - 1 <= this.clientHeight
if (CONDITION) {
console.log('----------------------------')
console.log('loadmore', CONDITION)
console.log('scrollHeight', this.scrollHeight)
console.log('scrollTop', this.scrollTop)
console.log('clientHeight', this.clientHeight)
console.log('----------------------------')
binding.value()
}
})
}
}
})
标签:
Element UI
, Vue
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)