思路
微信小程序的上拉加载下拉刷新会用到onPullDownRefresh和onReachBottom方法,首先page为1,如果长度大于0就进行for循环,用push方法在student后面追加数组,再进行page++。在上拉加载时page为1并且student数组为空。
代码
const app = getApp();
var page = 1;
var isfinish = false;
function loadmore(that){
if(isfinish) return;
wx.request({
url: "xxx", //仅为示例,并非真实的接口地址
data:{
page:page
},
success: (res) => {
that.setData({student: res.data})
page++;
if(res.data.length>0){
var student = that.data.student;
for(var i = 0;i < res.data.length;i++){
student.push(res.data[i]);
}
that.setData({ student: student })
page++;
}else{
isfinish = true;
}
wx.stopPullDownRefresh();
console.log(res.data)
}
})
}
Page({
data: {
student:[],
},
onPullDownRefresh: function () {
page = 1;
isfinish = false;
this.setData({student:[]});
loadmore(this);
},
onReachBottom: function () {
var that = this;
loadmore(that);
},
onPageScroll: function (e) {
// Do something when page scroll
// console.log(e);
},
onLoad: function () {
page = 1;
isfinish = false;
this.setData({student:[]});
loadmore(this);
},
})