-
Notifications
You must be signed in to change notification settings - Fork 422
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
Support customized pagination in class-based views #65
Conversation
…te, the default per_page ought to actually implement pagination instead of forcing an ugly 'Page 1 of 1' at the botton of the table.
…ion can be customized.
This looks good to me. My only reservation is whether the variables should be prefixed with |
Agreed. Since that gets passed straight to the config and is used internally, I think just changing the CBV's nomenclature will suffice. |
Returns pagination options: True for standard pagination (default), | ||
False for no pagination, and a dictionary for custom pagination. | ||
""" | ||
if isinstance(self.table_pagination, int): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about if isinstance(self.table_pagination, bool):
? Then the next line doesn't need bool()
.
…y checks, keep pagination set to true by default.
Returns pagination options: True for standard pagination (default), | ||
False for no pagination, and a dictionary for custom pagination. | ||
""" | ||
return self.table_pagination or True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd probably just leave this as return self.table_pagination
.
Customization of a table's pagination is performed in the tables.paginate() method, through a set of arguments passed in to it (klass, per_page, and page) along with extra _args and *_kwargs getting shuttled to the provided Pagination klass. These values are given to tables.paginate() from the RequestConfig class, through a dictionary instantiated as 'paginate', allowing pagination to be customized.
Previously, there was no easy way to populate the paginate dictionary from the class based views, since people using the new views don't instantiate RequestConfig themselves. By implementing the SingleTableMixin.get_paginate() method, I've exposed the paginate dictionary to the SingleTableView.as_view() function, so people can use the standard class based plug in work flow.
get_paginate() behaves like the other get_ methods within the view, uses duck typing to match dictionary-like objects (if for some reason people get really fancy), and acts the way RequestConfig expects it to:
SingleTableView.as_view(template_name="cool_tables.html", model=Coolness, table_class=CoolTable)
SingleTableView.as_view(template_name="cool_tables.html", model=Coolness, table_class=CoolTable, paginate=True)
SingleTableView.as_view(template_name="cool_tables.html", model=Coolness, table_class=CoolTable, paginate=False)
SingleTableView.as_view(template_name="cool_tables.html", model=Coolness, table_class=CoolTable, paginate=dict(per_page=42, klass=SuperPaginator, page=some_start_page, extra=will_get_passed_to_super_paginator)
I've written this because django-tables2 seems preferable to other ordered paginated table plugins, gives you tons of control over styling, is actually maintained, and bradleyayers is awesomely responsive; however I'm dealing with large datasets and class-based views, so I wanted to implement it with an ajaxified Paginator. I'm toying with a modified version of the standard tables.html file that plays nicely with django-endless-pagination. If I succeed, I'll throw that up into a push request, perhaps with logic in the djangotables2.RenderTableNode class to pick it if django-endless-pagination is discovered. From there it should be pretty easy to add defaults for all the major pagination plugins that inherit from django's Paginator.