Added the page size options to the pagination navigation bar.

This commit is contained in:
依瑪貓 2020-07-08 23:40:36 +08:00
parent fc2e89ca1a
commit e5d0feadbc
3 changed files with 53 additions and 13 deletions

View File

@ -131,7 +131,7 @@ class BaseReportView(generic.ListView):
data = super(BaseReportView, self).get_context_data(**kwargs) data = super(BaseReportView, self).get_context_data(**kwargs)
data["period"] = self.period data["period"] = self.period
data["subject"] = self.subject data["subject"] = self.subject
data["pagination_links"] = self.pagination.links data["pagination"] = self.pagination
return data return data

View File

@ -21,9 +21,9 @@ First written: 2020/7/1
{% endcomment %} {% endcomment %}
{# The pagination, if any #} {# The pagination, if any #}
{% if pagination_links %} {% if pagination.is_paged %}
<ul class="pagination"> <ul class="pagination">
{% for link in pagination_links %} {% for link in pagination.links %}
{% if link.url is not None %} {% if link.url is not None %}
<li class="page-item {% if link.is_active %} active {% endif %}{% if not link.is_small_screen %} d-none d-md-inline {% endif %}"> <li class="page-item {% if link.is_active %} active {% endif %}{% if not link.is_small_screen %} d-none d-md-inline {% endif %}">
<a class="page-link" href="{{ link.url }}">{{ link.title }}</a> <a class="page-link" href="{{ link.url }}">{{ link.title }}</a>
@ -34,5 +34,15 @@ First written: 2020/7/1
</li> </li>
{% endif %} {% endif %}
{% endfor %} {% endfor %}
<li class="page-item active d-none d-md-inline">
<div class="page-link dropdown-toggle" data-toggle="dropdown">
{{ pagination.page_size }}
</div>
<div class="dropdown-menu">
{% for option in pagination.page_size_options %}
<a class="dropdown-item {% if pagination.page_size == option.size %} active {% endif %}" href="{{ option.url }}">{{ option.size }}</a>
{% endfor %}
</div>
</li>
</ul> </ul>
{% endif %} {% endif %}

View File

@ -142,12 +142,14 @@ class UrlBuilder:
urllib.parse.quote(self.name), urllib.parse.quote(self.name),
urllib.parse.quote(self.value)) urllib.parse.quote(self.value))
DEFAULT_PAGE_SIZE = 10
class Pagination: class Pagination:
"""The pagination. """The pagination.
Args: Args:
request (HttpRequest): The request current_url (str): The current URL
records (list[Model]): All the records records (list[Model]): All the records
page_no (int): The specified page number page_no (int): The specified page number
page_size (int): The specified number of records per page page_size (int): The specified number of records per page
@ -159,7 +161,6 @@ class Pagination:
of range or is redundant. of range or is redundant.
Attributes: Attributes:
current_url (bool): The current request URL
is_reversed (bool): Whether we should display the last is_reversed (bool): Whether we should display the last
page first page first
page_size (int): The page size. page_size (int): The page size.
@ -169,8 +170,10 @@ class Pagination:
records (list[Model]): The records in the current page. records (list[Model]): The records in the current page.
links (list[Link]): The navigation links in the pagination links (list[Link]): The navigation links in the pagination
bar. bar.
page_size_options(list[PageSizeOptions]): The page size
options
""" """
current_url = None _current_url = None
is_reversed = False is_reversed = False
page_size = None page_size = None
total_pages = None total_pages = None
@ -178,15 +181,14 @@ class Pagination:
page_no = None page_no = None
records = None records = None
DEFAULT_PAGE_SIZE = 10
def __init__(self, current_url, records, page_no, def __init__(self, current_url, records, page_no,
page_size, is_reversed=False): page_size, is_reversed=False):
self.current_url = current_url self._current_url = current_url
self._base_url = UrlBuilder(current_url).del_param("page")
self.is_reversed = is_reversed self.is_reversed = is_reversed
self.page_size = page_size \ self.page_size = page_size \
if page_size is not None \ if page_size is not None \
else self.DEFAULT_PAGE_SIZE else DEFAULT_PAGE_SIZE
self.total_pages = int( self.total_pages = int(
(len(records) - 1) / self.page_size) + 1 (len(records) - 1) / self.page_size) + 1
self.is_paged = self.total_pages > 1 self.is_paged = self.total_pages > 1
@ -212,7 +214,7 @@ class Pagination:
def links(self): def links(self):
"""Returns the navigation links of the pagination bar.""" """Returns the navigation links of the pagination bar."""
if self._links is None: if self._links is None:
base_url = UrlBuilder(self.current_url).del_param("page") base_url = UrlBuilder(self._current_url).del_param("page")
self._links = [] self._links = []
# The previous page # The previous page
link = self.Link() link = self.Link()
@ -235,7 +237,8 @@ class Pagination:
if not self.is_reversed: if not self.is_reversed:
link.url = str(base_url) link.url = str(base_url)
else: else:
link.url = str(base_url.clone().add_param("page", "1")) link.url = str(base_url.clone().add_param(
"page", "1"))
if self.page_no == 1: if self.page_no == 1:
link.is_active = True link.is_active = True
self._links.append(link) self._links.append(link)
@ -313,6 +316,33 @@ class Pagination:
is_active = False is_active = False
is_small_screen = False is_small_screen = False
@property
def page_size_options(self):
base_url = UrlBuilder(self._current_url).del_param(
"page").del_param("page-size")
return [self.PageSizeOption(
x, str(base_url.clone().add_param("page-size", str(x)))
if x != DEFAULT_PAGE_SIZE else str(base_url))
for x in [10, 100, 200]]
class PageSizeOption:
"""A page size option.
Args:
size (int): The page size.
url (str): The URL of this page size.
Attributes:
size (int): The page size.
url (str): The URL for this page size.
"""
size = None
url = None
def __init__(self, size, url):
self.size = size
self.url = url
class PageNoOutOfRangeException(Exception): class PageNoOutOfRangeException(Exception):
"""The error thrown when the specified page number is out of """The error thrown when the specified page number is out of