Painless pagination for Ecto queries.
Leaf Through is available in Hex and can be installed
by adding leaf_through
to your list of dependencies in mix.exs
:
def deps do
[{:leaf_through, "~> x.y.z"}]
end
Configure the repo used by leaf_through:
config :leaf_through,
repo: YourApp.Repo
Add LeafThrough to your repo.ex
:
defmodule YourApp.Repo do
use Ecto.Repo, otp_app: :your_app
import LeafThrough
end
If you want to define the per page size (default is 10), add the following to your config:
config :leaf_through,
per_page: 5
Let's assume you've created a User
schema which has a first_name
, last_name
, username
, and an email_address
. Here's how to get the first page of results that are sorted by the user's last name:
@users = User
|> order_by(desc: :last_name)
|> paginate(1)
Here's how to add pagination links to one of your Phoenix templates:
# In the template's view add
import LeafThrough.Html
# In the template file add
<%= raw(leaf_through(@users)) %>