Vue實現(xiàn)下拉加載更多
熟悉Element-UI的開發(fā)者可能都會有這樣的經(jīng)歷,它的無限滾動 InfiniteScroll 并不好用,下面介紹兩種下拉加載的實現(xiàn)方法:
1. 使用el-table-infinite-scroll 插件
(1). 安裝插件
npm install --save el-table-infinite-scroll
(2). 全局引入并注冊
// main.js import elTableInfiniteScroll from 'el-table-infinite-scroll'; Vue.use(elTableInfiniteScroll);
(3). 局部文件引入
<script>
// 引入
import elTableInfiniteScroll from 'el-table-infinite-scroll';
export default {
// 注冊指令
directives: {
'el-table-infinite-scroll': elTableInfiniteScroll
}
}
</script>
(4). 使用指令
<el-table :height="tableHeight" v-el-table-infinite-scroll="loadMore"> </el-table>
(5). 代碼實例
<template>
<div class="app-container">
<el-table :height="tableHeight" v-el-table-infinite-scroll="loadMore" :data="tableList">
<!-- 表格數(shù)據(jù)省略 -->
</el-table>
</div>
</template>
<script>
// 引入插件
import elTableInfiniteScroll from "el-table-infinite-scroll";
export default {
name: "index",
data() {
return {
// 表格高度
tableHeight:"",
// 數(shù)據(jù)總數(shù)
tableCount:0,
// 表格數(shù)據(jù)列表
tableList:[],
// 是否加載中
tableLoading:false,
// 表格搜索條件
tableSearch:{
page:1
}
}
},
// 注冊指令
directives: {
"el-table-infinite-scroll": elTableInfiniteScroll,
},
created() {
let windowHeight =document.documentElement.clientHeight || document.body.clientHeight;
// 動態(tài)計算表格的高度,200為屏幕內(nèi)除了表格以外其他元素的高度,依實際情況而定
this.tableHeight = windowHeight - 200 + "px";
},
mounted(){
this.getTableData(this.tableSearch);
},
methods: {
// 請求表格數(shù)據(jù)
getTableData(search) {
let page = search.page;
let url = "index?page=" + page;
// 首次打開頁面要加載一次數(shù)據(jù),為了防止數(shù)據(jù)過多自動觸發(fā)滾動,此處需要置為加載中
this.tableLoading = true;
this.$http.get(url).then((result) => {
if (res.code == 10000) {
// 拼接數(shù)據(jù)
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("已加載完所有的數(shù)據(jù)!");
this.tableLoading = false;
}
}
},
}
};
</script>
2. 自定義下拉加載方法
上面使用的插件需要依賴Element-UI,如果沒有使用Element-UI,那就只能自己寫一個下拉加載了,實現(xiàn)代碼如下:
<template>
<div class="app-container">
<div :style="{height:tableHeight,overflow:'auto'}" id="tableBox">
<!-- 表格數(shù)據(jù)省略 -->
</div>
</div>
</template>
<script>
export default {
name: "index",
data() {
return {
// 表格高度
tableHeight:"",
// 數(shù)據(jù)總數(shù)
tableCount:0,
// 表格數(shù)據(jù)列表
tableList:[],
// 是否加載中
tableLoading:false,
// 表格搜索條件
tableSearch:{
page:1
}
};
},
created(){
let windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
// 動態(tài)計算表格的高度,200為屏幕內(nèi)除了表格以外其他元素的高度,依實際情況而定
this.tableHeight = windowHeight - 200 +'px';
},
mounted() {
this.getTableData(this.tableSearch);
document.getElementById("tableBox").addEventListener('scroll',this.onTableScroll);
},
beforeDestroy() {
// 移除監(jiān)聽事件
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為閾值,可根據(jù)實際情況調(diào)整
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;
// 首次打開頁面要加載一次數(shù)據(jù),為了防止數(shù)據(jù)過多自動觸發(fā)滾動,此處需要置為加載中
this.tableLoading = true;
this.$http.get(url).then((result) => {
if (res.code == 10000) {
// 拼接數(shù)據(jù)
this.tableList = this.tableList.concat(result.data.list);
this.tableCount = result.count;
this.tableSearch.page = result.current;
this.tableLoading = false;
}
});
},
},
};
</script>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue實現(xiàn)圖片路徑轉(zhuǎn)二進制文件流(binary)
這篇文章主要介紹了vue實現(xiàn)圖片路徑轉(zhuǎn)二進制文件流(binary),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
vue頁面切換項目實現(xiàn)轉(zhuǎn)場動畫的方法
這篇文章主要介紹了vue頁面切換項目實現(xiàn)轉(zhuǎn)場動畫的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
vue.js?Table?組件自定義列寬實現(xiàn)核心方法
這篇文章主要介紹了vue.js?Table?組件自定義列寬實現(xiàn)核心方法,文圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-07-07
vue中echarts關(guān)系圖動態(tài)增刪節(jié)點以及連線方式
這篇文章主要介紹了vue中echarts關(guān)系圖動態(tài)增刪節(jié)點以及連線方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07

