使用el-table-infinite-scroll 插件
- 安装插件;
npm install --save el-table-infinite-scroll
- 全局引入并注册;
// main.js
import elTableInfiniteScroll from 'el-table-infinite-scroll';
Vue.use(elTableInfiniteScroll);
- 局部文件引入;
<script>
// 引入
import elTableInfiniteScroll from 'el-table-infinite-scroll';
export default {
// 注册指令
directives: {
'el-table-infinite-scroll': elTableInfiniteScroll
}
}
</script>
- 使用指令;
<el-table :height="tableHeight" v-el-table-infinite-scroll="loadMore">
</el-table>
- 代码实例;
方法根据情况而定
<template>
<div class="app-container">
<el-table :height="tableHeight" v-el-table-infinite-scroll="loadMore" :data="tableList">
<!-- 表格数据省略 -->
</el-table>
</div>
</template>
<script>
// 引入插件
import elTableInfiniteScroll from "el-table-infinite-scroll";
export default {
name: "index",
data() {
return {
// 表格高度
tableHeight:"",
// 数据总数
tableCount:0,
// 表格数据列表
tableList:[],
// 是否加载中
tableLoading:false,
// 表格搜索条件
tableSearch:{
page:1
}
}
},
// 注册指令
directives: {
"el-table-infinite-scroll": elTableInfiniteScroll,
},
created() {
let windowHeight =document.documentElement.clientHeight || document.body.clientHeight;
// 动态计算表格的高度,200为屏幕内除了表格以外其他元素的高度,依实际情况而定
this.tableHeight = windowHeight - 200 + "px";
},
mounted(){
this.getTableData(this.tableSearch);
},
methods: {
// 请求表格数据
getTableData(search) {
let page = search.page;
let url = "index?page=" + page;
// 首次打开页面要加载一次数据,为了防止数据过多自动触发滚动,此处需要置为加载中
this.tableLoading = true;
this.$http.get(url).then((result) => {
if (res.code == 10000) {
// 拼接数据
this.tableList = this.tableList.concat(result.data.list);
this.tableCount = result.count;
this.tableSearch.page = result.current;
this.tableLoading = false;
}
});
},
// 加载更多
loadMore() {
if (!this.tableLoading) {
this.tableLoading = true;
if (this.tableList.length < this.tableCount) {
this.tableSearch.page++;
this.getTableData(this.tableSearch);
} else {
this.$message("已加载完所有的数据!");
this.tableLoading = false;
}
}
},
}
};
</script>