服务端
tp的分页设计很优秀
//php:
public function getdata(Request $request)
{
$key = $request->param('key');
$data = Db::table('datatab')->where('type', $key)->order('id' ,'desc')->paginate(20); //每页20
return json($data);
}
小程序端
data: {
current_page: 1, //默认第一页
last_page: 0, //存储总页数
data: [] //存放数据
},
onLoad: function (options) {
wx.showLoading()
let self = this
wx.request({
url: 'https://xxxx',
data:{key:'searchkey'}
success: res => {
/*返回的数据格式如下
current_page: 1
data: (10) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
last_page: 331
per_page: 10
total: 3303
*/
self.setData({ data: res.data.data,last_page:res.data.last_page })
wx.hideLoading()
}
})
},
//页面上拉到底,加载更多
onReachBottom: function () {
console.log('到底了')
let self = this
let { current_page, last_page } = this.data
if (current_page < last_page) {
current_page++
wx.showLoading()
wx.request({
url: 'https://xxxx',
data: { page: current_page,key:'searchkey' },
method: 'GET',
success: res => {
self.setData({ data:self.data.data.concat(res.data.data), current_page })
wx.hideLoading()
}
})
}
}