The Wayback Machine - https://web.archive.org/web/20211213033354/https://github.com/pallets/flask-sqlalchemy/issues/567
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jessesheidlower opened this issue Nov 17, 2017 · 1 comment
Open

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

jessesheidlower opened this issue Nov 17, 2017 · 1 comment

Comments

@jessesheidlower
Copy link

@jessesheidlower jessesheidlower commented Nov 17, 2017

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.

@rsyring
Copy link
Contributor

@rsyring rsyring commented Mar 14, 2019

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

@rsyring rsyring removed this from the 2.4 milestone Mar 15, 2019
@rsyring rsyring added this to the 2.x milestone Mar 15, 2019
@davidism davidism removed this from the 2.x milestone May 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment