小程序上拉加载更多实现代码

// page/info/info-index.js
const app=getApp()
Page({

/**
* 页面的初始数据
*/
data: {
list:[],
redNum: "",
pageRows: 6,
currentPage: 1,
isEnd: false, //是否是全部数据
},
//获取消息中心
getInformationList(typeValue){
var that= this;
wx.request({
url: `${app.data.requestUrl }/sale/information/list`,
method: 'post',
data: {
pageRows: that.data.pageRows,
currentPage: typeValue === 2 ? 1 : that.data.currentPage,
},
header: {
'content-type': 'application/json',
'token': app.data.mytoken,
},
success: function (res) {
if(res.data.code== 200){
var redList = res.data.data.list;
var num = that.data.redNum
for ( var i = 0; i < redList.length; i++) {
if (redList[i].status== 0){
num++;
}
}
var arr = res.data.data.list
for ( var i = 0; i < arr.length; i++) {
arr[i].createTime = app.formatTime(arr[i].createTime, "Y-MM-dd hh:mm")
}
const arrMoreList = typeValue ? arr : that.data.list.concat(arr);
const isEnd = (res.data.data.allRows - res.data.data.currentPage * res.data.data.pageRows) <= 0;
that.setData({
'list': arrMoreList,
'currentPage': res.data.data.currentPage + 1,
'isEnd': isEnd,
'redNum': num
})
console.log(arrMoreList)
//将消息条数全局
app.data.rednum = that.data.redNum
} else{
app.showToast(app.data.appError, 'none')
}
},
fail: function (err) { app.showToast(app.data.appError, 'none'); }, //请求失败
})
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
var that= this;
that.setData({
'currentPage': 1,
})
that.getInformationList( 2);
},

/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
if ( this.data.isEnd) {
return;
}
this.getInformationList();
},

})

猜你喜欢

转载自blog.csdn.net/caoyan0829/article/details/80184158