A flexible and robust pagination library for Elixir/Phoenix applications that integrates seamlessly with Ecto.
- Simple Integration: Easily paginate Ecto queries with minimal configuration
- Flexible Rendering: HTML helpers with Tailwind CSS styling by default
- Customizable: Support for custom templates and styling
- Internationalization: Built-in i18n support via Gettext
- Performance Optimized: Smart counting strategies for different query types
The package is not yet available on Hex.pm. To install directly from GitHub, add to your dependencies:
def deps do
[
{:pagination_ex, "~> 0.1.0"}
]
end
Configure PaginationEx in your application's config file:
# In config/config.exs or environment-specific config files
config :pagination_ex,
repo: MyApp.Repo, # Required
per_page: 25, # Optional, defaults to 30
per_group: 500, # Optional, defaults to 1000
gettext_module: MyApp.Gettext, # Optional for internationalization
router_helpers: MyAppWeb.Router.Helpers # Optional for route generation
# In your controller
def index(conn, params) do
pagination =
MyApp.Posts
|> PaginationEx.new(params)
render(conn, "index.html", pagination: pagination)
end
# In your template
<%= for post <- @pagination.entries do %>
<div class="post">
<h2><%= post.title %></h2>
<p><%= post.content %></p>
</div>
<% end %>
<div class="pagination">
<%= PaginationEx.paginate(@conn, :post_path) %>
</div>
For processing large datasets in batches:
# This fetches all items in batches of the configured size
all_items = PaginationEx.in_groups(MyApp.Items)
# Process all items
Enum.each(all_items, fn item ->
# Process each item
end)
Create a custom template module:
defmodule MyApp.CustomPaginationTemplate do
use PhoenixHTMLHelpers
import PaginationEx.HTML, only: [translate: 1, build_url: 3]
def render_pagination(conn, path, pagination) do
# Your custom rendering logic here
end
end
Then use it in your templates:
<%= PaginationEx.paginate(@conn, :post_path, template_module: MyApp.CustomPaginationTemplate) %>
The Core
module handles the pagination logic and query execution.
Key functions:
new/3
: Creates a new pagination struct from an Ecto queryin_groups/2
: Retrieves all records in batches of specified size
The HTML
module renders pagination controls in templates.
Key functions:
paginate/3
: Renders complete pagination controlspage_links/3
: Generates numbered page linksprevious_path/3
andnext_path/4
: Creates previous/next navigation links
The Config
module handles configuration retrieval.
The pagination result is a struct with the following fields:
%PaginationEx.Core{
entries: [%Post{}, %Post{}, ...], # The current page's items
total_entries: 59, # Total number of items
page_number: 2, # Current page number
per_page: 10, # Items per page
pages: 6, # Total number of pages
query: #Ecto.Query<...> # The original query
}
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
- Wait for the review
And voilà! Your ice cream is ready ✨
- Rômulo Silva (Tomate) - Alchemist & Developer Github
- Paulo Igor (Pigor) - Alchemist & Developer Github
- Mateus Linhares (Mateus) - Alchemist & Developer Github
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.