Paginator (simple_pagination v0.0.3)
Provides Live SimplePagination for a given query.
This is module is paired with helper components used to help organize and present large sets of data in a user-friendly way. It provides a mechanism for dividing data into smaller, more manageable chunks or pages, and allows users to navigate through these pages using a set of controls such as "next page," "previous page," and page numbers.
In addition to pagination, this module offers features such as sorting, filtering, and search capabilities, allowing users to further refine and customize the data they see.
Let's see a sample index.html.heex
:
<.search_filter_tag paginator={@things} label="Find" />
<.page_tag paginator={@things} delta={1} />
<table>
<thead>
<tr>
<td style="width:15%"><.order_tag label="id" order_by={:id} paginator={@things} /></td>
<td style="width:85%"><.order_tag label="title" order_by={:title} paginator={@things} /></td>
</tr>
</thead>
<%= for t <- @things.data do %>
<tr>
<td><%= t.id %></td>
<td><%= t.title %></td>
</tr>
<% end %>
</table>
Here @things
is a PaginatorState including pagination parameters and resulting dataset.
Not that you can place filters_tag
, page_tag
and order_tag
pretty much anywhere.
All changes are handled via event. The paginate
event must be handled in index.ex
def mount(params, _session, socket),
do: {:ok, assign(socket, things: Things.paginate(params))}
def handle_event("paginate" = event, params, socket),
do: {:noreply, assign(socket, things: Things.paginate(socket.assigns.things, params))}
The Things
context is the place where the initial query is fed to the paginator:any()
def paginate(
attrs,
%PaginatorState{} = pg \ %PaginatorState{
filters: [title: ""],
per_page_items: [5, 25, 0],
order_by: {:asc, :title}
}
) do
list_things_query()
|> maybe_apply_advanced_filtering(attrs)
|> SimplePagination.Paginator.paginate(SimplePagination.Paginator.change(pg, attrs))
end
Check SimplePagination.PaginatorState
struct for more details about pagination parameters.
If complex filtering is required, create and add maybe_apply_advanced_filtering/2
and refine the query as needed.
Link to this section Summary
Functions
this function paginates + filters the given query and returns an updated paginator repo is your App.Repo
Link to this section Functions
change(pg, attrs)
maybe_apply_filters(query, filters)
maybe_apply_order(query, arg2)
maybe_paginate(query, page, per_page_nb)
paginate(query, pg, repo)
this function paginates + filters the given query and returns an updated paginator repo is your App.Repo