Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix page number issue when changing page size #782

Merged
merged 6 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading