I’m working on a web application in web2py. I want to introduce pagination to it but I have no database in my application. It isn’t needed. I will have a list of up to 100 items but could be as little as 50. I want to generate the number of pages needed and want to show 10 per page. Can anyone point me in the right direction? Can’t make any sense of the web2py tutorial on it.
Share
T = total items
I = items-per-page
P = page
Let T=100, I=10, page=0 (initial page)
Pythonic Psuedocode:
This should get you started. Test, debug and tweak as needed.
Let T=322, I=50, page=6
So, this logic all goes in the controller, where Items_Per_page is hardcoded on the server or received as a GET/POST variable when you click on a link or submit button. You’ll need to do the latter if you want the user to be able to select different amounts of items per page. Page is definitely received from the request as a GET/POST var (I would recommend GET as it’s easier in this case). T, total number of items, is easy — just say “len(data)” where data is an iterable.
Controller:
So that’s the controller. Now the view has access to data, start, end and total, and also prev and next.
I just realized the post is titled and tagged with web2py … so ignore any statements about your intended framework, here’s a web2py solution (untested, probably needs tweaks). Also, I use the x = value_if_true if expression else value_if_false which is not available before python 2.6 I think, so you might have to refactor.