Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #719 (closed enhancement: fixed)

Opened 13 years ago

Last modified 12 years ago

[PATCH] Pagination decorator, easy pagination and sorting

Reported by: Claudio Martinez <martinezc@…> Owned by: anonymous
Priority: normal Milestone: 0.9a6
Component: TurboGears Version:
Severity: normal Keywords:


I've made this decorator to be able to paginate resulsets and dictionaries without doing any work on the controller. You just return a resultset from a Object.select(...) (at this state the query hasn't been made yet) or a dict() and pass its name to the paginate decorator. When you pass a SQLObject ResultSet? the decorator won't fetch all the results, it will use the .limit() .order() functions to do it's work. Which will produce a LIMITed query.

I've also included a new datagrid (PaginateDataGrid?) object with a new template that has automatically adds the page list, but you don't need this to be able to use it.

Pagination and sorting can't be made easier if you use PaginateDataGrid?, you almost have to do nothing but adding the decorator.

It even works if you are getting filtering info from a POST on your controller because it will urlencode all the input_values when you get the hrefs for the links.

When the decorator is used on a controller tg.paginate will be available for the template. tg.paginate contains: tg.paginate.pages - A list of the pages the paginator will show (ex: [4,5,6,7,8]) tg.paginate.current_page - Current page tg.paginate.limit - Current rows per page (you shouldn't need this) tg.paginate.page_count - Number of pages tg.paginate.order - Current column being sorted tg.paginate.reversed - If the current sorting is reversed or not tg.paginate.get_href(x) - Returns the href for a link that will show x page of the list (ex: <a href="tg.paginate.get_href(1)">Page 1</a> would create a link to the first page)

The decorator takes these parameters: @turbogears.paginate(var_name, default_order=, limit=10, max_pages=5, allow_limit_override=False) var_name - Is the variable that will be paginated default_order - Only used when you want to enable sorting, it's the column name being sorted by default limit - Is the number of rows per page max_pages - Is the number of pages that will be returned in tg.paginate.pages (the default of 5 when seeing a page number 64 tg.paginate.pages would be [62,63,64,65,66]

If you are using the PaginateDataGrid? you don't have to worry about most of this stuff.

Check the demo project!


paginate.diff Download (11.3 KB) - added by Claudio Martinez <martinezc@…> 13 years ago.
paginate-test.tar.gz Download (14.7 KB) - added by Claudio Martinez <martinezc@…> 13 years ago.
Demonstration Project
paginate.2.diff Download (11.1 KB) - added by Claudio Martinez <martinezc@…> 13 years ago.
replaced cherrypy.thread_data for cherrypy.request to prevent future unexpected behavior, thanks alberto!

Change History

Changed 13 years ago by Claudio Martinez <martinezc@…>


Changed 13 years ago by Claudio Martinez <martinezc@…>

Demonstration Project

comment:1 Changed 13 years ago by max

Looks nice.

comment:2 Changed 13 years ago by alberto

Nice! Answering one of your comments: # Is there a per request place to dump variables? Yep, cherrypy.request.XXX. ie. cherrypy.request.validation_errors, cherrypy.request.input_values, ...

Changed 13 years ago by Claudio Martinez <martinezc@…>

replaced cherrypy.thread_data for cherrypy.request to prevent future unexpected behavior, thanks alberto!

comment:3 Changed 13 years ago by godoy

Very cool! When will somebody apply it to trunk? ;-) This solves a lot of problems for people creating grids with lots of data. :-)

comment:4 Changed 13 years ago by anonymous

Looks like the decorator module has been updated, for this to keep working replace the: from turbogears.decorator import decorator

for: from turbogears.decorator import simple_decorator as decorator

I was thinking that maybe it would be better to make this an extension... Or should I make a patch for the 1.0 branch?

comment:5 Changed 13 years ago by simon

  • Type changed from defect to enhancement
  • Milestone set to 1.0

I strongly suggest decorator.weak_signature_decorator (see  Announcement).

As far as I am concerned this should be a part of the default installation, it's just a question of where to put it. I will rekindle the discussion.

comment:6 Changed 13 years ago by alberto

I agree it should be part of a default installation, or at least easy to integrate in TG apps. Having a non-paginating datagrid is almost useless for most non-toy projects IMO. The grids have moved to FastData? which is now a separate egg, maybe the patch could be included there?


comment:7 Changed 13 years ago by kevin

  • Milestone changed from 1.0 to 0.9a6

comment:8 Changed 13 years ago by kevin

  • Status changed from new to closed
  • Resolution set to fixed

Committed in [1379]. Thanks!

Note: See TracTickets for help on using tickets.