关于分页问题

分页应该前端做还是后端做呢?是看项目大小吗?还是随意?相对来说后端容易些还是前端容易些?

vuejs
78 views
Comments
登录后评论
Sign In
·

分页接口应该是后端提供的,而且尽量避免使用 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="
  }
}

基于游标的分页

·

stuck_out_tongue_closed_eyes 偷偷看了黑客说的页面分页,就是 cursor paging sunglasses