Skip to content

Commit 2117a55

Browse files
simonwclaude
andcommitted
Query list footer shows the position on every page
The cursor does not record how far into the list it is, so HTML Next links now carry ?_start= and the footer reads "Showing 21-40 of 87". The last page keeps the footer without a Next link. The JSON API's next_url is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 0f4400b commit 2117a55

4 files changed

Lines changed: 37 additions & 7 deletions

File tree

‎datasette/static/homepage.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
// A new search starts again from the first page
5757
params.delete("page");
5858
params.delete("_next");
59+
params.delete("_start");
5960
const qs = params.toString();
6061
return form.action + (qs ? "?" + qs : "");
6162
};

‎datasette/templates/_query_list_results.html‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@
5050
</li>
5151
{% endfor %}
5252
</ul>
53-
{% if next_url %}
53+
{% if next_url or start %}
5454
<nav class="page-position page-position-footer" aria-label="Query pagination">
55-
<span class="page-position-text">Showing <strong>{{ queries|length }}</strong> of <strong>{{ "{:,}".format(total) }}</strong></span>
56-
<span class="page-position-links"><a class="page-position-next" href="{{ next_url }}" rel="next">
55+
<span class="page-position-text">Showing <strong>{{ "{:,}".format(start + 1) }}–{{ "{:,}".format(start + queries|length) }}</strong> of <strong>{{ "{:,}".format(total) }}</strong></span>
56+
{% if next_url %}<span class="page-position-links"><a class="page-position-next" href="{{ next_url }}" rel="next">
5757
Next
5858
<svg aria-hidden="true" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m9 18 6-6-6-6"/></svg>
59-
</a></span>
59+
</a></span>{% endif %}
6060
</nav>
6161
{% endif %}
6262
{% elif filters.q %}

‎datasette/views/stored_queries.py‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ async def get(self, request):
6969
def _query_list_url(path, query_string, *, set_args=None, remove_args=None):
7070
set_args = set_args or {}
7171
remove_args = set(remove_args or ())
72-
skip = set(set_args) | remove_args | {"_next", "_fragment"}
72+
skip = set(set_args) | remove_args | {"_next", "_fragment", "_start"}
7373
pairs = [
7474
(key, value)
7575
for key, value in parse_qsl(query_string, keep_blank_values=True)
@@ -117,16 +117,26 @@ async def get(self, request):
117117
include_private=True,
118118
)
119119
query_list_path = self.query_list_path(database)
120+
# The cursor does not say how far into the list it is, so HTML pages
121+
# carry the position in ?_start= for the "Showing 21-40 of 87" footer
122+
try:
123+
start = max(int(request.args.get("_start") or 0), 0)
124+
except ValueError:
125+
start = 0
126+
if not request.args.get("_next"):
127+
start = 0
120128
next_url = None
121129
if page.next:
122130
pairs = [
123131
(key, value)
124132
for key, value in parse_qsl(
125133
request.query_string, keep_blank_values=True
126134
)
127-
if key not in ("_next", "_fragment")
135+
if key not in ("_next", "_fragment", "_start")
128136
]
129137
pairs.append(("_next", page.next))
138+
if format_ == "html":
139+
pairs.append(("_start", str(start + len(page.queries))))
130140
next_url = self.ds.absolute_url(
131141
request,
132142
f"{request.path}?{urlencode(pairs)}",
@@ -270,6 +280,7 @@ async def facet_items(items):
270280
data.update(
271281
{
272282
"total": total,
283+
"start": start,
273284
"database_chips": database_chips,
274285
"all_queries_href": all_queries_href,
275286
"actor_id": actor_id,

‎tests/test_queries.py‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import json
22
import re
33
from html import unescape
4+
from urllib.parse import urlsplit
45

56
import pytest
67
import sqlite_utils
@@ -3947,11 +3948,28 @@ async def test_query_list_fragment_and_pagination():
39473948
assert len(soup.select("li.query-row")) == 20
39483949
nav = soup.select_one("nav.page-position-footer")
39493950
assert " ".join(nav.select_one(".page-position-text").text.split()) == (
3950-
"Showing 20 of 25"
3951+
"Showing 1–20 of 25"
39513952
)
39523953
next_href = nav.select_one("a.page-position-next")["href"]
39533954
assert "q=Demo" in next_href
39543955
assert "_next=" in next_href
3956+
assert "_start=20" in next_href
3957+
3958+
# The second page knows where it is, and is the last page
3959+
path = urlsplit(next_href)
3960+
response = await ds.client.get(path.path + "?" + path.query, actor={"id": "root"})
3961+
soup = Soup(response.text, "html.parser")
3962+
assert len(soup.select("li.query-row")) == 5
3963+
nav = soup.select_one("nav.page-position-footer")
3964+
assert " ".join(nav.select_one(".page-position-text").text.split()) == (
3965+
"Showing 21–25 of 25"
3966+
)
3967+
assert nav.select_one("a.page-position-next") is None
3968+
# The JSON API's own next link is unchanged
3969+
data = (
3970+
await ds.client.get("/data/-/queries.json?_size=20", actor={"id": "root"})
3971+
).json()
3972+
assert "_start" not in data["next_url"]
39553973

39563974
# The live search fetches just the summary and results
39573975
response = await ds.client.get(

0 commit comments

Comments
 (0)