1. 程式人生 > 其它 >上拉重新整理下拉載入

上拉重新整理下拉載入

<template>
<view class="box">
<view v-for="(item, index) in newsList" class="newslist">
<view class="item">
{{item.title}}
</view>
</view>
<view class="loading">{{loadingText}}</view>
</view>
</template>
<script>
var _self, page = 1, timer = null;
export default {
data(){
return{
newsList:[],
loadingText:'載入中...'
}

},
//頁面載入
onLoad(){
_self = this;
//獲取首頁資料
this.getnewsList()
},

// 下拉重新整理,初始化第一頁資料
onPullDownRefresh(){
this.getnewsList();
},
//頁面滾動到底部的事件
onReachBottom(){
if(timer != null){
clearTimeout(timer);
}
timer = setTimeout(function(){
//載入更多資料
_self.getmorenews();
}, 1000);
},
methods:{
getmorenews(){
if(_self.loadingText != '' && _self.loadingText != '載入更多'){
return false;
}
_self.loadingText = '載入中...';
//在當前頁面顯示導航條載入動畫。
uni.showNavigationBarLoading();
uni.request({
url: 'https://ceshi2.dishait.cn/api/v1/postclass/1/post/'+page,
method: 'GET',
success: function(res){
_self.loadingText = '';
if(res.data == null){
//在當前頁面隱藏導航條載入動畫。
uni.hideNavigationBarLoading();
_self.loadingText = '已載入全部';
return false;
}
page++;
console.log(res);
_self.newsList = _self.newsList.concat(res.data.data.list);
_self.loadingText = '載入更多';
uni.hideNavigationBarLoading();
}
});
},
getnewsList(){
page = 1;
uni.showNavigationBarLoading();
uni.request({
url: `https://ceshi2.dishait.cn/api/v1/postclass/1/post/${page}`,
method: 'GET',
success: function(res){
console.log(res)
page++;
_self.newsList = res.data.data.list;
uni.hideNavigationBarLoading();
uni.stopPullDownRefresh();
_self.loadingText = '載入更多';
}
});
}
}
}
</script>
<style>
.box{
width: 100vw;
height: 100vh;
}
.item{
width: 100vw;
height: 200rpx;
border: 1rpx solid gray;
text-align: center;
line-height: 200rpx;
}
.loading{
text-align: center;
color: #8F8F94;
}
</style>

pages.json加上
"enablePullDownRefresh":true

{
"path": "pages/center/ces",
"style": {
"navigationBarTitleText": "學員管理",
"enablePullDownRefresh":true
}
},