Added the page size options to the pagination navigation bar.
This commit is contained in:
parent
fc2e89ca1a
commit
e5d0feadbc
@ -131,7 +131,7 @@ class BaseReportView(generic.ListView):
|
||||
data = super(BaseReportView, self).get_context_data(**kwargs)
|
||||
data["period"] = self.period
|
||||
data["subject"] = self.subject
|
||||
data["pagination_links"] = self.pagination.links
|
||||
data["pagination"] = self.pagination
|
||||
return data
|
||||
|
||||
|
||||
|
@ -21,9 +21,9 @@ First written: 2020/7/1
|
||||
{% endcomment %}
|
||||
|
||||
{# The pagination, if any #}
|
||||
{% if pagination_links %}
|
||||
{% if pagination.is_paged %}
|
||||
<ul class="pagination">
|
||||
{% for link in pagination_links %}
|
||||
{% for link in pagination.links %}
|
||||
{% 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 %}">
|
||||
<a class="page-link" href="{{ link.url }}">{{ link.title }}</a>
|
||||
@ -34,5 +34,15 @@ First written: 2020/7/1
|
||||
</li>
|
||||
{% endif %}
|
||||
{% 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>
|
||||
{% endif %}
|
@ -142,12 +142,14 @@ class UrlBuilder:
|
||||
urllib.parse.quote(self.name),
|
||||
urllib.parse.quote(self.value))
|
||||
|
||||
DEFAULT_PAGE_SIZE = 10
|
||||
|
||||
|
||||
class Pagination:
|
||||
"""The pagination.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The request
|
||||
current_url (str): The current URL
|
||||
records (list[Model]): All the records
|
||||
page_no (int): The specified page number
|
||||
page_size (int): The specified number of records per page
|
||||
@ -159,7 +161,6 @@ class Pagination:
|
||||
of range or is redundant.
|
||||
|
||||
Attributes:
|
||||
current_url (bool): The current request URL
|
||||
is_reversed (bool): Whether we should display the last
|
||||
page first
|
||||
page_size (int): The page size.
|
||||
@ -169,8 +170,10 @@ class Pagination:
|
||||
records (list[Model]): The records in the current page.
|
||||
links (list[Link]): The navigation links in the pagination
|
||||
bar.
|
||||
page_size_options(list[PageSizeOptions]): The page size
|
||||
options
|
||||
"""
|
||||
current_url = None
|
||||
_current_url = None
|
||||
is_reversed = False
|
||||
page_size = None
|
||||
total_pages = None
|
||||
@ -178,15 +181,14 @@ class Pagination:
|
||||
page_no = None
|
||||
records = None
|
||||
|
||||
DEFAULT_PAGE_SIZE = 10
|
||||
|
||||
def __init__(self, current_url, records, page_no,
|
||||
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.page_size = page_size \
|
||||
if page_size is not None \
|
||||
else self.DEFAULT_PAGE_SIZE
|
||||
else DEFAULT_PAGE_SIZE
|
||||
self.total_pages = int(
|
||||
(len(records) - 1) / self.page_size) + 1
|
||||
self.is_paged = self.total_pages > 1
|
||||
@ -212,7 +214,7 @@ class Pagination:
|
||||
def links(self):
|
||||
"""Returns the navigation links of the pagination bar."""
|
||||
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 = []
|
||||
# The previous page
|
||||
link = self.Link()
|
||||
@ -235,7 +237,8 @@ class Pagination:
|
||||
if not self.is_reversed:
|
||||
link.url = str(base_url)
|
||||
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:
|
||||
link.is_active = True
|
||||
self._links.append(link)
|
||||
@ -290,7 +293,7 @@ class Pagination:
|
||||
link.url = str(base_url)
|
||||
else:
|
||||
link.url = str(base_url.clone().add_param(
|
||||
"page", str(self.total_pages)))
|
||||
"page", str(self.total_pages)))
|
||||
else:
|
||||
link.url = str(base_url.clone().add_param(
|
||||
"page", str(self.page_no + 1)))
|
||||
@ -313,6 +316,33 @@ class Pagination:
|
||||
is_active = 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):
|
||||
"""The error thrown when the specified page number is out of
|
||||
|
Loading…
Reference in New Issue
Block a user