element-ui element-plus el-table 滚动懒加载
原创
2022-10-18
08:50
编辑于
2022-10-18
09:10
element-ui 未提供滚动条的事件,如果想通过滚动条做懒加载,只能自行去找到滚动区域,并监听滚动条事件。
使用dom查找并监听
element-ui 的滚动区域dom为 el-table__body-wrapper
element-plus 的滚动区域dom为 el-scrollbar__wrap
<template>
<el-table :data="tableData" style="width: 100%" height="200" ref="table">
<el-table-column prop="date" label="Date" width="180" />
<el-table-column prop="name" label="Name" width="180" />
<el-table-column prop="address" label="Address" />
</el-table>
</template>
<script>
const tableData = [
{
date: '2016-05-03',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-02',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-04',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
]
export default {
data() {
return {
tableData,
}
},
mounted() {
this.$nextTick(function () {
var me = this
this.$el
.querySelector('.el-scrollbar__wrap')
.addEventListener('scroll', function (this) {
if (this.scrollHeight - this.scrollTop - this.offsetHeight < 50) {
me.tableData.push({
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},{
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},{
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},{
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},{
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
})
}
})
})
},
}
</script>
转载请注明出处。本文地址:
https://www.qinshenxue.com/article/element-ui-or-plus-el-table-scrolling-lazy-loading.html
关注我的公众号