分页接口应该是后端提供的,而且尽量避免使用 offset,pageSize,pageNum (点击页码)这种方式,对数据库性能非常不友好,有很多文章写为什么 offset 是 evil 的。应该做 cursor paging,和 facebook/twitter/reddit 那样的分页才是用户友好的,可以看看这几篇文章:
why facebook says cursor pagination is the-greatest
how to implement cursor pagination like a pro
evolving api pagination at slack
比如facebook的分页返回是这样的:
{
"data": [
{
"something": "Endpoint data is here"
}
],
"paging": {
"cursors": {
"after": "MTAxNTExOTQ1MjAwNzI5NDE=",
"before": "NDMyNzQyODI3OTQw"
},
"previous": "https://graph.facebook.com/{your-user-id}/albums?limit=25&before=NDMyNzQyODI3OTQw",
"next": "https://graph.facebook.com/{your-user-id}/albums?limit=25&after=MTAxNTExOTQ1MjAwNzI5NDE="
}
}
基于游标的分页