当前位置: 移动技术网 > IT编程>脚本编程>vue.js > 监听element-ui table滚动事件的方法

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

2019年07月23日  | 移动技术网IT编程  | 我要评论

我是特种兵2演员名单,防爆温湿度计,无限之我写的无限

背景

做管理平台的项目,用到了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的绑定! 希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网