I have a page with a list of objects and two buttons: show more and show all. The objects are loaded by AJAX, sending to a ItemsListView
a page number:
class ItemsListView(ListView):
paginate_by = 2
On initializing the page, JS variable current_page
is set to 1
, and JS sends an AJAX request to ItemsListView
view, gets 2 objects for the first page and renders them.
When a user clicks on show more button, current_page
value increments, and JS requests the objects for the next page.
Now I want to implement show all button. For example,
If we have the following list of objects: [1, 2, 3, 4, 5, 6, 7, 8, 9]
, on the page load objects 1
and 2
will be rendered. After a user clicks on show more, it will render objects 3
and 4
. After a user clicks on show all, it will render objects 5
, 6
, 7
, 8
and 9
.
I changed dynamically paginate_orphans
in my 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 it worked, but I wonder if there is a more elegant approach to solve this problem.