下拉刷新
下拉刷新,简单的来说就是在我们的页面上第一次点进来的数据,然后还有数据未加载出来,下拉刷新,就是向再次接口请求了数据,
把页面上已经请求到的数据和刷新后又请求出来的数据拼接到一起,
可以利用展开运算符,然后页面进行了再次渲染,有了新的数据,这就是下拉刷新。
1)一般用户的程序中下拉刷新是被禁止的,我们可以先在要刷新页面的Json中添加 “enablePullDownRefresh”:true,
2)配置后Json 就开始具体的代码操作
data{
lister: [ ], //第一次请求到页面的数据
}
3)onPullDownRefresh中写入具体的操作
onPullDownRefresh: function () {
let that = this;
//加载数据中的的Laoding
wx.showLoading({
title: '加载中...',
})
//再次向接口请求的数据
app.http.goods_lists({
cid: that.options.cid
}, 'get').then(res => {
// 数据请求出来后关闭Llading
wx.hideLoading()
let {
data: {
message: {
goods
}
}
} = res
//定义res 数组 里面的数据是consloe.log(res)下的data下的message下goods
//利用展开运算符将之前保留的数据和又请求到的数据进行新的存储
that.setData({
// 用展开运算符 保存数据shopList
lister: [...goods, ...that.data.lister]
})
wx.stopPullDownRefresh(); //数据请求成功后就停止下拉
wx.hideLoading() //隐藏动画
},
上拉加载更多
上拉加载更多我们可以把它看作是一个分页器,只是把分页器的按钮变成了下拉,
1)首先我们先写一个对象用来存取从接口中请求来我们所需要的数据,例如我写的一个项目中,
data{
lister: [ ], //第一次请求到页面的数据
newsquery: {
query: "",
cid: "",
pagesize: 10, // 每页的条数
pagenum: 1, // 当前页数
},
totalnum: "", // 总条数
totalsize: 3, // 总页数
}
在 onReachBottom写具体的操作步骤
onReachBottom: function () {
that.totalsize = Math.ceil(res.data.message.total / that.data.newsquery.pagesize)
console.log(this.totalsize, this.data.newsquery.pagenum);
// 当前总条数 和 当前每页的条数
if (this.totalsize == this.data.newsquery.pagenum) {
wx.showToast({
title: '没有更多数据',
})
//弹出提示框
} else {
// 页数增加
this.data.newsquery.pagenum++
app.http.goods_lists({
cid: that.options.cid
}, 'get').then(res => {
// 数据请求出来后关闭Llading
wx.hideLoading()
let {
data: {
message: {
goods
}
}
} = res
//定义res 数组 里面的数据是consloe.log(res)下的data下的message下goods
//利用展开运算符将之前保留的数据和又请求到的数据进行新的存储
that.setData({
// 用展开运算符 保存数据shopList
lister: [...goods, ...that.data.lister]
})
}
wx.hideLoading() // 数据请求成功后隐藏请提示
},
以上就是我的上拉加载,下拉刷新内容
如果在参考的过程中有问题,还请各位码友指出