I wanted a PaginateDataGrid where the user could change the number of
results to be displayed with a simple Jumpmenu widget. Here is what
works for me:
1. Make the jump menu with simple options like this -
limit_options = [('10','10',),
('20','20',),
('30','30',),
('40','40',),
('50','50',)]
The actual href will be created in the template. I did this because I
did not know how to access the Paginate intance from the controller.
2. Replace the normal jumpmenu template with this -
<?python
import cherrypy
def get_limit_href(value):
""" Return a new paginate href with an updated limit value
"""
return
cherrypy.request.paginate.get_href(cherrypy.request.paginate.current_page,
limit=value)
def get_option_default(attrs, value):
""" Return the SELECTED attribute for the matching jumpmenu option
"""
if cherrypy.request.paginate.limit == int(value):
attrs['SELECTED'] = ''
return attrs
?>
<select xmlns:py="http://purl.org/kid/ns#"
name="${name}"
class="${field_class}"
id="${field_id}"
onchange="TG_jumpMenu('parent',this,0)"
py:attrs="attrs"
<optgroup py:for="group, options in grouped_options"
label="${group}"
py:strip="not group"
>
<option py:for="value, desc, attrs in options"
value="${get_limit_href(value)}"
py:attrs="get_option_default(attrs, value)"
py:content="desc"
/>
</optgroup>
</select>
3. Patch your paginate.py with this patch -
Index: paginate.py
===================================================================
--- paginate.py (revision 2346)
+++ paginate.py (working copy)
@@ -139,7 +139,7 @@
self.href_prev = None
self.href_first = None
- def get_href(self, page, order=None, reverse_order=None):
+ def get_href(self, page, order=None, reverse_order=None, limit=None):
if order:
if order == self.order:
if self.reversed:
@@ -156,8 +156,11 @@
else:
order = self.order
reversed = self.reversed
+ if not limit:
+ limit = self.limit
- self.input_values.update(dict(tg_paginate_no=page,
+ self.input_values.update(dict(tg_paginate_limit=limit,
+ tg_paginate_no=page,
tg_paginate_order=order,
tg_paginate_reversed=reversed))
Any suggestions on improving this? Is it possible to access the
Paginate instance from the controller, because if so, the python block
could be removed from the template.
I just put the jumpmenu display() right above the datagrid display() and
the user can choose how many results to see at a time.
Regards,
Aaron
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---