I'm having a problem getting AjaxPaginatedGrid to work. The demo in
the toolbox works perfectly, but no matter how closely I emulate the
example source, I cannot get my implementation to paginate. It
initially fills the grid correctly but doesn't show any other pages
but the first, with no response from any of the controls. This is with
TGPaginate 0.1a0.dev-r663 under TG 1.0.2.2.
Here's the code I'm having problems with:
class Root(controllers.RootController):
example_list = [(x,x) for x in range(10,200)]
@expose(format='json')
@paginate('example_list')
def lister(self, *args, **kw):
return dict(example_list = self.example_list)
@expose(template='project.templates.list')
def list(self, *args, **params):
grid = AjaxPaginatedGrid(
name='list_grid',
url='lister',
var_name='example_list',
fields=[('C1', lambda x: x[0]),
('C2', lambda x: x[1])]
)
return dict(grid=grid)
I can pass pagination parameters directly to the lister method and it
returns the expected subset of results. I've tried it with a Kid
template because I thought the problem might lie with Genshi, but the
behaviour is identical for both: the APG reports 0/0 pages and only
holds the first 10 items
What am I missing? Are there additional JS dependencies that
AjaxPaginatedGrid isn't bringing in by default that are being linked
in by the other example widgets?
Any help would be greatly appreciated.
-alex23
You are more than likely importing the paginate decorator from
turbogears. TGPaginate has reimplemented the paginate decorator to
allow multiple paginations. You need to import paginate from
tgpaginate.controllers.
If you inspect the Ajax requests, you'll see that the pagination
parameters being passed say things like 'tg_paginate_%s_no=5' instead of
'tg_paginate_no=5' - this is the multiple pagination thing happening,
and it is handled in TGPaginate's paginate decorator.
However, I'm also having problems with the AjaxPaginatedGrid.
Firstly, I'm using SQLAlchemy, and TGPaginate only supports SQLObject.
Have any patches been submitted to support SA yet? Looking at the code,
the changes are fairly trivial, and I'm going to try to add SA support
myself, but I'm not too confident in my ability to do it properly.
That aside, I've discovered what seems to be a pretty big problem with
TGPaginate.
In the AjaxPaginatedGrid kid template, we have:
<a py:if="col.get_option('sortable', False) and getattr(tg, 'paginate',
False)" href="${tg.paginate[value.var_name].get_href(1, col.name,
col.get_option('reverse_order', False))}">${col.title}</a>
This will create the links to sort the grid on a column, if the
tg.paginate attribute is set.
Howeever, this attribute is set by the paginate() decorator, which is no
longer applied to the HTML request that renders the widget - with the
AjaxPaginatedGrid, the JSON request is the one that gets decorated wih
paginate, as shown in your code:
> class Root(controllers.RootController):
>
> example_list = [(x,x) for x in range(10,200)]
>
> @expose(format='json')
> @paginate('example_list')
> def lister(self, *args, **kw):
> return dict(example_list = self.example_list)
>
> @expose(template='project.templates.list')
> def list(self, *args, **params):
> grid = AjaxPaginatedGrid(
> name='list_grid',
> url='lister',
> var_name='example_list',
> fields=[('C1', lambda x: x[0]),
> ('C2', lambda x: x[1])]
> )
> return dict(grid=grid)
So, when the widget gets rendered, no tg.paginate attribute will be set,
and the sorting links will never be created.
Am I wrong in my analysis?
Has anybody used the AjaxPaginatedGrid successfully, with sorting by
columns and everything, because this problem, if I am right, would seem
to indicate that it's not possible?
Cheers,
-Jonathan
Ah, _thank you_, that was exactly the issue. Could the toolbox example
perhaps be updated to refer explicitly to tgpaginate.paginate?
> Has anybody used the AjaxPaginatedGrid successfully, with sorting by
> columns and everything, because this problem, if I am right, would seem
> to indicate that it's not possible?
I'm sorry but I haven't gotten that far, I'll try and take a look at
my set up this weekend.
Cheers!
-alex23