Skip to content
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ I want to select rows in a paginated table for export to TSV w/o storing state i

## TODO:

- [ ] Pagination controls
- [x] Pagination controls
- [ ] Export selections to TSV
11 changes: 6 additions & 5 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ def get_table_rows(
def index():
target_template = "index.html"

nbr_table_rows_per_page = int(request.args.get("rows", 10))
selected_page_nbr = int(request.args.get("page", 1))
selected_limit_nbr = int(request.args.get("limit", 10))
search = request.args.get("search", None)
selected_rows = set()

Expand All @@ -73,23 +73,24 @@ def index():
case _:
return abort(400)

nbr_table_rows_per_page = int(request.form.get("rows", 10))
selected_limit_nbr = int(request.form.get("limit", 10))
selected_page_nbr = int(request.form.get("page", 1))
search = request.form.get("search", None)
selected_rows = set(request.form.getlist("selected-rows"))

db_data, nbr_total = get_table_rows(db, selected_page_nbr, nbr_table_rows_per_page, search)
db_data, nbr_total = get_table_rows(db, selected_page_nbr, selected_limit_nbr, search)

if (selected_page_nbr * nbr_table_rows_per_page) > nbr_total:
if (selected_page_nbr * selected_limit_nbr) > nbr_total:
selected_page_nbr = 1

available_pages = math.ceil(nbr_total / nbr_table_rows_per_page)
available_pages = math.ceil(nbr_total / selected_limit_nbr)

return render_template(
target_template,
db_data=db_data,
selected_rows=selected_rows,
nbr_pages=available_pages,
nbr_limit=selected_limit_nbr,
selected_page=selected_page_nbr,
)

Expand Down
49 changes: 19 additions & 30 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
<!DOCTYPE html>
<head>
<title>Hello world</title>
<script
src="https://unpkg.com/htmx.org@1.9.6"
integrity="sha384-FhXw7b6AlE/jyjlZH5iHa/tTe9EpJ1Y55RjcgPbjeWMskSxZt1v9qkxLJWNJaGni"
crossorigin="anonymous"></script>
<script src="https://unpkg.com/hyperscript.org@0.9.12"></script>
<script src="https://unpkg.com/htmx.org@1.9.10/dist/htmx.min.js"></script>
<style>

.row-selector {
Expand All @@ -32,31 +28,24 @@
</head>
<body>
<div id="result"></div>
<form
method="post"
hx-post="{{url_for('index')}}"
hx-target="#table-container"
hx-trigger="change from #page-navigation-controls"
>
<div id="search-controls">
<label for="search">Search table: </label>
<input
hx-trigger="keyup changed delay:500ms, search"
hx-post="{{url_for('index')}}"
hx-target="#table-container"
name="search"
type="search">
</div>
<div id="table-container">
{% include "table.html" %}
</div>
<span id="export-tsv-controls">
<button
type="button"
hx-post="{{url_for('create_tsv_report')}}"
hx-target="#report">Export to TSV</button>
</span>
</form>
<div id="search-controls">
<label for="search">Search table: </label>
<input
hx-trigger="keyup changed delay:500ms"
hx-post="{{url_for('index')}}"
hx-target="#table-container"
name="search"
type="search">
</div>
<div id="table-container">
{% include "table.html" %}
</div>
<span id="export-tsv-controls">
<button
type="button"
hx-post="{{url_for('create_tsv_report')}}"
hx-target="#report">Export to TSV</button>
</span>
<div id="report"></div>
</body>
</html>
15 changes: 12 additions & 3 deletions templates/table.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<label for="limit">Items per page: </label>
<select name="limit">
<option value="25">25</option>
<option value="50">50</option>
<option value="100">100</option>
<option value="10" {% if nbr_limit == 10 %}selected{% endif %}>10</option>
<option value="25" {% if nbr_limit == 25 %}selected{% endif %}>25</option>
<option value="50" {% if nbr_limit == 50 %}selected{% endif %}>50</option>
<option value="100" {% if nbr_limit == 100 %}selected{% endif %}>100</option>
</select>
<span id="page-navigation-controls">
<button type="button"
Expand Down Expand Up @@ -64,3 +65,11 @@
{% for selected_row_id in selected_rows %}
<input type="hidden" name="selected-rows" value="{{selected_row_id}}">
{% endfor %}
<script>
var limit_field = document.querySelector("select[name='limit']");
var page_field = document.querySelector("select[name='page']");
limit_field.addEventListener("change", function () {
page_field.value = 1;
console.log("limit changed");
});
</script>