轉自於:https://www.jb51.net/article/211727.htm(親測可用)
這里需要后端做分頁
1. 使用el-table-infinite-scroll 插件
(1). 安裝插件
1
|
npm install --save el-table-infinite-scroll
|
(2). 全局引入並注冊
1
2
3
4
5
|
// main.js
import elTableInfiniteScroll from
'el-table-infinite-scroll'
;
Vue.use(elTableInfiniteScroll);
|
(3). 局部文件引入
1
2
3
4
5
6
7
8
9
10
|
<script>
// 引入
import elTableInfiniteScroll from
'el-table-infinite-scroll'
;
export
default
{
// 注冊指令
directives: {
'el-table-infinite-scroll'
: elTableInfiniteScroll
}
}
</script>
|
(4). 使用指令
1
2
3
|
<
el-table
:height
=
"tableHeight"
v-el-table-infinite-scroll
=
"loadMore"
>
</
el-table
>
|
(5). 代碼實例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
<
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>
|
2. 自定義下拉加載方法
上面使用的插件需要依賴Element-UI,如果沒有使用Element-UI,那就只能自己寫一個下拉加載了,實現代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
<
template
>
<
div
class
=
"app-container"
>
<
div
:style
=
"{height:tableHeight,overflow:'auto'}"
id
=
"tableBox"
>
<!-- 表格數據省略 -->
</
div
>
</
div
>
</
template
>
<
script
>
export default {
name: "index",
data() {
return {
// 表格高度
tableHeight:"",
// 數據總數
tableCount:0,
// 表格數據列表
tableList:[],
// 是否加載中
tableLoading:false,
// 表格搜索條件
tableSearch:{
page:1
}
};
},
created(){
let windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
// 動態計算表格的高度,200為屏幕內除了表格以外其他元素的高度,依實際情況而定
this.tableHeight = windowHeight - 200 +'px';
},
mounted() {
this.getTableData(this.tableSearch);
document.getElementById("tableBox").addEventListener('scroll',this.onTableScroll);
},
beforeDestroy() {
// 移除監聽事件
window.removeEventListener('scroll', this.onTableScroll)
},
methods: {
onTableScroll(){
var obj = document.getElementById("tableBox");
var scrollHeight = obj.scrollHeight;
var scrollTop = obj.scrollTop;
var objHeight = obj.offsetHeight;
// 100為閾值,可根據實際情況調整
if(scrollHeight <= (scrollTop + objHeight + 100) && !this.tableLoading && this.tableList.length<
this.tableCount
){
this.tableLoading
=
true
;
this.tableSearch.page++;
setTimeout(()=>{
this.getTableData(this.tableSearch);
},300)
}
},
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;
}
});
},
},
};
</
script
>
|