elementUI table滚动分页

一、 el - table上设置ref属性ref = “table”

 <el-table :data="workforceTemDataCurrect"
              border
              v-loading="tableLoading"
              style="width: 100%"
              height="100%"
              ref="table">
               </el-table>

二、 因为操作dom所以用到生命周期钩子mounted

mounted() {
    // 获取需要绑定的table
    this.dom = this.$refs.table.bodyWrapper
    this.dom.addEventListener('scroll', () => {
      // 表格滚动条滚动的距离
          let scrollTop = this.dom.scrollTop
          // 变量windowHeight是可视区的⾼度
          // let windowHeight = this.dom.clientHeight
          // 变量scrollHeight是滚动条的总⾼度
          let scrollHeight = this.dom.scrollHeight
          let clientHeight = this.dom.clientHeight
          let scrollBottom = scrollHeight - scrollTop - clientHeight
          if (scrollBottom < 1) {      // 这里就是滚动条滚动到底部的时候
        // 获取到的不是全部数据当滚动到底部继续获取新的数据

        // 这里记住我们要把表格渲染的数据和获取到的全部数据分开, 等滑动到底部时我们在取10或者20条添加到渲染数据上
        if (this.workforceTemDataAll.length <= this.workforceTemDataCurrect.length) return  // 因为我们默认都是渲染20条数据,如果总数据没那么多就直接return出去
        this.loading = true  // 开启loading效果防止渲染速度慢用户乱点,导致页面崩溃

        if (this.workforceTemDataCurrect.length + 10 > this.workforceTemDataAll.length) {
          // 如果workforceTemDataAll和workforceTemDataCurrect选差没有十条那么就全部渲染上去
          this.dom.scrollTop = this.dom.scrollTop - 30   // 30 50 都行
          this.workforceTemDataCurrect.push(...this.workforceTemDataAll)
        } else {
          this.dom.scrollTop = this.dom.scrollTop - 30
          let id = this.workforceTemDataCurrect.length
          // 这里取10条,渲染上去 拿多少数据循环多少次就行
          for (let index = id - 1; index <= id + 9; index++) {  // 没有用forEach 因为场景不同这里用 forEach 效率会低
            this.workforceTemDataCurrect.push(this.workforceTemDataAll[index])
          }
        }

        setTimeout(() => {
          this.loading = false
        }, 500)

      }
    })
  }

注意在子组件中调用时要使用$nextTick函数

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值