flask-sqlalchemy icon indicating copy to clipboard operation
flask-sqlalchemy copied to clipboard

Add "first" and "last" to Pagination class?

Open jessesheidlower opened this issue 7 years ago • 1 comments

I'm new to Flask, and was wondering about a feature that I think would be useful to add to the Pagination class. Unless I'm missing it, there doesn't seem to be a built-in way to get the numbers of the items you're viewing on the page itself. That is, if you want to display "387 records found; displaying 26–50", you'd have to do calculations to get the "26" and "50".

I think something along these lines would work:

@property
    def first(self):
        """The number of the first item on the current page"""
        if self.total == 0:
            first = 0
        else:
            first = ((self.page - 1) * self.per_page) + 1
        return first

  @property
    def last(self):
        """The number of the last item on the current page"""
        if self.page == self.pages:
            last = self.total
        else:
            last = self.page * self.per_page
        return last

Then you could do something like (in jinja2):

{{ pagination.total }} records found; displaying {{ pagination.first }} – {{pagination.last }}

I'm new to Python programming, and I don't know enough about testing, running changed code, etc., to feel comfortable submitting this as a pull request, so I hope it's OK that I just posted this as an Issue.

Thanks.

jessesheidlower avatar Nov 17 '17 16:11 jessesheidlower

The PR for this creates a problem in our test suite. See the latest comment there if interested.

rsyring avatar Mar 14 '19 20:03 rsyring