第一步:给元素添加指令:
<el-table :data="tableData"
ref="table"
border
stripe
height="100%"
tooltip-effect="dark"
style="width:100%"
// 这里注意:指令方法不加括号
v-el-table-tableLazy="tableLazy">
// 表格内容
</el-table>
第二步:自定义指令,这段指令代码可以照抄
directives: {
'el-table-tableLazy': {
bind (el, binding) {
let SELECT_DOM = el.querySelector(
'.el-table__body-wrapper'
)
SELECT_DOM.addEventListener('scroll', function () {
let condition =
this.scrollHeight - this.scrollTop <= this.clientHeight + 50
if (condition) {
binding.value()
}
})
},
},
},
第三步:指令调用的方法,即滚动时触发的方法
需要提前定义的变量有:tableData, page, pageSize, isLoading, total
tableLazy () {
if (this.total == this.tableData.length) {
// 已经滚动到最后
} else {
this.getTableData()
}
},
getTableData () {
let _this = this
if (_this.isLoading) return
_this.isLoading = true
// 组装请求的filterModel
return new Promise(function (resolve, reject) {
_this.请求名称(filterModel)
.then(res => {
if (!!res) {
if (res.ObjectList && res.ObjectList.length) {
_this.tableData = _this.tableData.concat(res.ObjectList)
_this.page++
_this.total = res.TotalCount
_this.isLoading = false
}
}
resolve()
})
.catch(err => {
reject(err)
})
})
},