refrakt/paginate
Pagination helpers for list views.
Usage in a handler
import refrakt/paginate
pub fn index(req: Request, ctx: Context) -> Response {
let params = paginate.from_request(req)
let posts = post_repo.list_paginated(ctx.db, params)
let page = paginate.page(posts, params, total_count)
post_views.index_view(page)
|> root_layout.wrap("Posts")
|> wisp.html_response(200)
}
Usage in a repo
pub fn list_paginated(db, params: paginate.Params) -> List(Post) {
let offset = paginate.offset(params)
pog.query("SELECT ... LIMIT $1 OFFSET $2")
|> pog.parameter(pog.int(params.per_page))
|> pog.parameter(pog.int(offset))
...
}
Types
A paginated result.
pub type Page(item) {
Page(
items: List(item),
page: Int,
per_page: Int,
total: Int,
total_pages: Int,
has_previous: Bool,
has_next: Bool,
)
}
Constructors
-
Page( items: List(item), page: Int, per_page: Int, total: Int, total_pages: Int, has_previous: Bool, has_next: Bool, )
Values
pub fn from_request(
req: request.Request(wisp.Connection),
) -> Params
Extract pagination params from query string (?page=2&per_page=10).