question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pagination example with page total and page number

See original GitHub issue

I’ve successfully implemented offset/limit based pagination, see below. But I need a page where the user can jump between the various pages, so I need to know the total items, as well as current page. Cursor based pagination doesn’t do this either, as it only knows the last and next page. Can an example be provided for how to do this page type of pagination? Is it possible?

 def resolve_tickets(self, info, **kwargs):
        offset = kwargs.pop('offset', 0)
        limit = kwargs.pop('limit', 10)
        return TicketType.get_query(info).filter_by(**kwargs).offset(offset).limit(limit).all()

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5

github_iconTop GitHub Comments

2reactions
hqmancommented, Feb 15, 2019

Hi @Kazanz Could you show a full example about offset base pagination include type defs ? Thanks!

1reaction
s3rgeymcommented, Jun 19, 2019

I’ve successfully implemented offset/limit based pagination, see below. But I need a page where the user can jump between the various pages, so I need to know the total items, as well as current page. Cursor based pagination doesn’t do this either, as it only knows the last and next page. Can an example be provided for how to do this page type of pagination? Is it possible?

 def resolve_tickets(self, info, **kwargs):
        offset = kwargs.pop('offset', 0)
        limit = kwargs.pop('limit', 10)
        return TicketType.get_query(info).filter_by(**kwargs).offset(offset).limit(limit).all()
class ConnectionField(SQLAlchemyConnectionField):
  """
  Добавляет человеческую постраничную навигацию вместо хуеты с first и after
  TODO: реализовать фильтры
  """

  def __init__(self, type, *args, **kw):
    super(ConnectionField, self).__init__(
        type, *args, page=graphene.Int(), per_page=graphene.Int(), **kw)

  @classmethod
  def get_query(cls, model, info, page=None, per_page=None, **kw):
    q = super(ConnectionField, cls).get_query(model, info, **kw)
    if per_page:
      q = q.limit(per_page)
    if page:
      q = q.offset((page - 1) * per_page)
    return q

Usage:

query {
  contacts (page: 1, perPage: 10) {
    edges {
      node {
        id
        email
      }
    }
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

pagination logic in calculating total of pages - Stack Overflow
total_items / 10 should give you the number of pages. You also need to consider remainder of the division (total_items % 10). So...
Read more >
Pagination: How to get total number of pages? - Laracasts
$results->count() => Get the number of items for the current page. This gives me how many items are there on the current page....
Read more >
JavaScript - calculate number of pages from size of ... - Dirask
In this article, we're going to have a look at how in JavaScript, calculate a number of pages when we know: maximal size...
Read more >
Pagination issue: returning total count and total pages in body
What I have tried: I've tried creating new models to seperate totalcount and totalpages but it has not been successful. Posted ...
Read more >
Pagination - Bootstrap
Documentation and examples for showing pagination to indicate a series of related content exists across multiple pages.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found