监听element-ui table滚动事件

3次阅读

共计 939 个字符,预计需要花费 3 分钟才能阅读完成。

背景
做管理平台的项目, 用到了 element-ui,需要通过监听 el-table 滚动的位置来获取最新的数据,那么怎么样监听 el-table 的滚动呢?
准备
我们默认的技术栈是 vue+element-ui

template 代码:
<el-table
:data=”logList”
:show-header=”false”
row-class-name=”table-row-class”
height=”700″
ref=”table”
@row-click=”rowClick”>
<el-table-column type=”expand”>
<el-table-column
label=”log 信息 ”
prop=”message”>
</el-table-column>
</el-table>
绑定监听事件
mounted() {
// 获取需要绑定的 table
this.dom = this.$refs.table.bodyWrapper
this.dom.addEventListener(‘scroll’, () => {
// 滚动距离
let scrollTop = this.dom.scrollTop
// 变量 windowHeight 是可视区的高度
let windowHeight = this.dom.clientHeight || this.dom.clientHeight
// 变量 scrollHeight 是滚动条的总高度
let scrollHeight = this.dom.scrollHeight || this.dom.scrollHeight
if (scrollTop + windowHeight === scrollHeight) {
// 获取到的不是全部数据 当滚动到底部 继续获取新的数据
if (!this.allData) this.getMoreLog()
console.log(‘scrollTop’, scrollTop + ‘windowHeight’, windowHeight + ‘scrollHeight’, scrollHeight)
}
})
}
获取到新的数据后,调整滚动条的位置
getMoreLog() {

this.dom.scrollTop = this.dom.scrollTop – 100

}

结语
至此我们已经完成了对 table 的绑定!

正文完
 0