Skip to content

Commit 80fae05

Browse files
tukyjieter
authored andcommitted
table_page_range appends ellipsis for LazyPaginator if not on last page (#707)
1 parent 4962816 commit 80fae05

4 files changed

Lines changed: 28 additions & 5 deletions

File tree

django_tables2/paginators.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class UserListView(SingleTableView):
5656

5757
def __init__(self, object_list, per_page, look_ahead=None, **kwargs):
5858
self._num_pages = None
59+
self._final_num_pages = None
5960
if look_ahead is not None:
6061
self.look_ahead = look_ahead
6162

@@ -91,8 +92,13 @@ def page(self, number):
9192
else:
9293
# This is the last page.
9394
self._num_pages = number
95+
# For rendering purposes in `table_page_range`, we have to remember the final count
96+
self._final_num_pages = number
9497
return Page(objects, number, self)
9598

99+
def is_last_page(self, number):
100+
return number == self._final_num_pages
101+
96102
def _get_count(self):
97103
raise NotImplementedError
98104

django_tables2/templatetags/django_tables2.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from django.utils.http import urlencode
1212

1313
import django_tables2 as tables
14+
from django_tables2.paginators import LazyPaginator
1415
from django_tables2.utils import AttributeDict
1516

1617
register = template.Library()
@@ -267,6 +268,8 @@ def table_page_range(page, paginator):
267268
ret = [1, "..."] + list(ret)[2:]
268269
if num_pages not in ret:
269270
ret = list(ret)[:-2] + ["...", num_pages]
271+
if isinstance(paginator, LazyPaginator) and not paginator.is_last_page(page.number):
272+
ret.append("...")
270273
return ret
271274

272275

docs/pages/pagination.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@ set `SingleTableView.table_pagination = False`
3232
Lazy pagination
3333
~~~~~~~~~~~~~~~
3434

35-
The default `~django.core.paginators.Paginator` want to count the number of items,
35+
The default `~django.core.paginators.Paginator` wants to count the number of items,
3636
which might be an expensive operation for large QuerySets.
3737
In those cases, you can use `.LazyPaginator`, which does not perform a count,
38-
but also does not know what the total amount of pages will be.
38+
but also does not know what the total amount of pages will be, until you've hit
39+
the last page.
3940

4041
The `.LazyPaginator` does this by fetching `n + 1` records where the number of records
4142
per page is `n`. If it receives `n` or less records, it knows it is on the last page,
42-
preventing rendering of the 'next' button.
43+
preventing rendering of the 'next' button and further "..." ellipsis.
4344
Usage with `SingleTableView`::
4445

4546
class UserListView(SingleTableView):

tests/test_templatetags.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,10 +286,23 @@ def test_table_page_range_num_pages_equals_page_range_plus_one(self):
286286
table_page_range(paginator.page(7), paginator), [1, "...", 4, 5, 6, 7, 8, 9, 10, 11]
287287
)
288288

289-
def test_table_page_range_lazy(self):
289+
def test_table_page_range_lazy_beginning(self):
290290
paginator = LazyPaginator(range(1, 1000), 10)
291291

292292
self.assertEqual(table_page_range(paginator.page(1), paginator), range(1, 3))
293+
294+
def test_table_page_range_lazy_middle(self):
295+
paginator = LazyPaginator(range(1, 1000), 10)
296+
293297
self.assertEqual(
294-
table_page_range(paginator.page(10), paginator), [1, "...", 4, 5, 6, 7, 8, 9, 10, 11]
298+
table_page_range(paginator.page(10), paginator),
299+
[1, "...", 4, 5, 6, 7, 8, 9, 10, 11, "..."],
300+
)
301+
302+
def test_table_page_range_lazy_last_page(self):
303+
paginator = LazyPaginator(range(1, 1000), 10)
304+
305+
self.assertEqual(
306+
table_page_range(paginator.page(100), paginator),
307+
[1, "...", 93, 94, 95, 96, 97, 98, 99, 100],
295308
)

0 commit comments

Comments
 (0)