小程序列表下拉上拉加載數(shù)據(jù)的方式很多種。
上一篇是結(jié)合導(dǎo)航欄loading顯示正在加載的效果。請(qǐng)參考地址:http://m.itdecent.cn/p/a1903cb4e1be
本篇利用本地假數(shù)據(jù)結(jié)合wx.showToast來展示。
注意:列表實(shí)現(xiàn)前面文章中已實(shí)現(xiàn),本篇只特別注明修改的地方。請(qǐng)參考地址:http://m.itdecent.cn/p/44b07a86c8ff。
實(shí)現(xiàn)方法:
1、在小程序app.json文件中,對(duì)應(yīng)的window下添加代碼:
"enablePullDownRefresh": true,
2、在小程序頁面對(duì)應(yīng)的.js文件中,數(shù)據(jù)初始化部分,newList后面添加totalList用于展示當(dāng)前列表的所有數(shù)據(jù)。
const newList = new Array(10).fill(0)
var totalList = newList;
3、在小程序頁面對(duì)應(yīng)的.js文件中,添加data:
data: {
list:totalList,
pageNum: 1, // 設(shè)置加載的第幾次,默認(rèn)是第一次
pageTotalNum:2, // 設(shè)置總共的分頁數(shù)
},
4、在小程序頁面對(duì)應(yīng)的.js文件Page下添加onPullDownRefresh和onReachBottom方法。
//上拉加載更多
onReachBottom:function(params) {
this.data.pageNum += 1;
if(this.data.pageNum > this.data.pageTotalNum){
wx.showToast({
title: '沒有數(shù)據(jù)了',
})
return
}
wx.showToast({
title: '加載更多',
icon:"loading"
})
wx.request({
url: 'https://wwww.test.com',
complete: res=>{
var templist = new Array(totalList.length + 10).fill(0)
for (let index = 0; index < totalList.length; index++) {
templist[index] = totalList[index];
}
for (let i = totalList.length; i < templist.length; i++) {
templist[i] = {
idx: i+1,
nickname: `nickname${i+1}`,
datetime: `datetime${i+1}`,
last_content: `內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容內(nèi)容`,
head_img_url: imgUrlList[(i) % 5],
}
}
totalList = templist;
this.setData({
list:totalList,
pageNum:this.data.pageNum
})
wx.hideToast();
}
})
},
//下拉刷新
onPullDownRefresh:function(options) {
wx.showToast({
title: 'loading...',
icon:"loading"
})
wx.request({
url: 'https://wwww.test.com',
complete: res=>{
wx.hideToast();
wx.stopPullDownRefresh()
totalList = null;
totalList = newList;
this.setData({
list:totalList,
pageNum:1
})
console.info(totalList.length);
}
})
},
此項(xiàng)目主要是本地假數(shù)據(jù)結(jié)合wx.showToast顯示正在加載的效果,隨筆記錄,不喜勿噴。