Pagination support!!!
This commit is contained in:
parent
3f677fceee
commit
a64c64e828
5 changed files with 298 additions and 12 deletions
|
|
@ -2,6 +2,7 @@ from flask import Blueprint, render_template, abort, request
|
|||
|
||||
import crudkit
|
||||
|
||||
from crudkit.api._cursor import decode_cursor, encode_cursor
|
||||
from crudkit.ui.fragments import render_table
|
||||
|
||||
bp_listing = Blueprint("listing", __name__)
|
||||
|
|
@ -89,14 +90,19 @@ def init_listing_routes(app):
|
|||
{"when": {"field": "complete", "is": True}, "class": "table-success"},
|
||||
{"when": {"field": "complete", "is": False}, "class": "table-danger"}
|
||||
]
|
||||
spec["limit"] = 15
|
||||
spec["offset"] = (page_num - 1) * 15
|
||||
limit = int(request.args.get("limit", 15))
|
||||
cursor = request.args.get("cursor")
|
||||
key, backward = decode_cursor(cursor)
|
||||
|
||||
service = crudkit.crud.get_service(cls)
|
||||
rows = service.list(spec)
|
||||
window = service.seek_window(spec | {"limit": limit}, key=key, backward=backward, include_total=True)
|
||||
|
||||
table = render_table(rows, columns=columns, opts={"object_class": model, "row_classes": row_classes})
|
||||
|
||||
return render_template("listing.html", model=model, table=table)
|
||||
table = render_table(window.items, columns=columns, opts={"object_class": model, "row_classes": row_classes})
|
||||
return render_template("listing.html", model=model, table=table, pagination={
|
||||
"limit": window.limit,
|
||||
"total": window.total,
|
||||
"next_cursor": encode_cursor(window.last_key, list(window.order.desc), backward=False),
|
||||
"prev_cursor": encode_cursor(window.first_key, list(window.order.desc), backward=True),
|
||||
})
|
||||
|
||||
app.register_blueprint(bp_listing)
|
||||
Loading…
Add table
Add a link
Reference in a new issue