監聽element-ui table滾動事件

背景

作管理平臺的項目,用到了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的綁定!vue

相關文章
相關標籤/搜索