Skip to content

Commit

Permalink
Fix page number issue when changing page size (#782)
Browse files Browse the repository at this point in the history
Co-authored-by: Amin Alaee <[email protected]>
  • Loading branch information
numberbee7070 and aminalaee authored Jul 12, 2024
1 parent 8acef5b commit 5174973
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions sqladmin/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ def next_page(self) -> PageControl:

raise RuntimeError("Next page not found.")

def resize(self, page_size: int) -> Pagination:
self.page = (self.page - 1) * self.page_size // page_size + 1
self.page_size = page_size
return self

def add_pagination_urls(self, base_url: URL) -> None:
# Previous pages
for p in range(self.page - min(self.max_page_controls, 3), self.page):
Expand Down
2 changes: 1 addition & 1 deletion sqladmin/templates/sqladmin/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ <h3 class="card-title">{{ model_view.name_plural }}</h3>
</a>
<div class="dropdown-menu">
{% for page_size_option in model_view.page_size_options %}
<a class="dropdown-item" href="{{ request.url.include_query_params(pageSize=page_size_option) }}">
<a class="dropdown-item" href="{{ request.url.include_query_params(pageSize=page_size_option, page=pagination.resize(page_size_option).page) }}">
{{ page_size_option }} / Page
</a>
{% endfor %}
Expand Down
11 changes: 11 additions & 0 deletions tests/test_pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,14 @@ def test_multi_page_unequal_previous_and_next() -> None:
]

assert pagination.page_controls == page_controls


def test_resize_pagination() -> None:
pagination = Pagination(rows=[], page=3, page_size=5, count=20)
assert pagination.resize(100).page == 1

pagination = Pagination(rows=[], page=3, page_size=5, count=20)
assert pagination.resize(1).page == 11

pagination = Pagination(rows=[], page=3, page_size=5, count=20)
assert pagination.resize(8).page == 2

0 comments on commit 5174973

Please sign in to comment.