网络编程 
首页 > 网络编程 > 浏览文章

监听element-ui table滚动事件的方法

(编辑:jimmy 日期: 2024/5/30 浏览: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的绑定! 希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:详解Nodejs get获取远程服务器接口数据
下一篇:JavaScript的级联函数用法简单示例【链式调用】
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网