ajax - Django pagination: "show all" implementation -
i have page list of objects , 2 buttons: show more , show all. objects loaded ajax, sending itemslistview
page number:
class itemslistview(listview): paginate_by = 2
on initializing page, js variable current_page
set 1
, , js sends ajax request itemslistview
view, gets 2 objects first page , renders them.
when user clicks on show more button, current_page
value increments, , js requests objects next page.
now want implement show all button. example,
if have following list of objects: [1, 2, 3, 4, 5, 6, 7, 8, 9]
, on page load objects 1
, 2
rendered. after user clicks on show more, render objects 3
, 4
. after user clicks on show all, render objects 5
, 6
, 7
, 8
, 9
.
i changed dynamically paginate_orphans
in view:
class itemslistview(listview): paginate_by = 2 paginate_orphans = 0 def get_paginate_orphans(self): show_all = json.loads(self.request.get.get('show_all', 'false')) page_kwarg = self.page_kwarg page = self.kwargs.get(page_kwarg) or self.request.get.get(page_kwarg) or 1 if show_all: return self.get_queryset().count() - (int(page) - 1) * 2 return self.paginate_orphans
and worked, wonder if there more elegant approach solve problem.
doesn't elegant @ :d more obvious ...
def get_queryset(self): show_all = ... page = ... qs = super(itemslistview, self).get_queryset() if show_all: return qs[int(page)*self.paginate_by:] else: return qs
Comments
Post a Comment