View Source Torch.Pagination (Torch v5.4.0)

Handles Torch pagination queries and filtering.

Usage

When using the torch.gen.html generators, Torch.Pagination will automatically be added to your generated context module in the following way (using a module called Comment as an example):

defmodule MyApp.MyModule do
  use Torch.Pagination,
    repo: MyApp.Repo,
    model: MyApp.MyContext.Comment,
    name: :comments
end

The following Torch context methods will be created by using Torch.Pagination:

  • public paginate_*/1
  • public filter_config/1
  • private do_paginate_*/2

Customization of filter_config/1 is available as this function is defined as defoverridable for when you prefer to customize the Filtrex filtering configuration.

Required configuration options

  • :repo - the application's Ecto.Repo module (required)
  • :model - the application's schema module (required)
  • :name - the plural name of the collection that needs pagination (required)

Optional configuration options

Additionaly, the following parameters are also supported and can be manually configured and supplied to the use Torch.Pagination call:

  • :page_size - (default: 15) controls how many items are returned per page
  • :pagination_size - (default: 5) controls how many pagination links are displayed in the UI

You may also configure page_size and pagination_distance on app level for app-wide defaults when configuring Torch in the application config/config.exs file:

config :torch,

otp_app: :my_app,
page_size: 20,
pagination_distance: 3

Manual Pagination

If, for some reason, the default Torch.Pagination method results do not support your application's needs, you may manually implement paginate_*/1 yourself in your context module. Your customized paginate_* must return one of the following tuples (typespec Torch.Pagination.t()):

  • {:ok, Torch.Pagination.page()}
  • {:error, any}

Summary

Types

Link to this type

filter_config()

View Source (since 5.1.0)
@type filter_config() :: [Filtrex.Type.Config.t()]
@type page() :: %{
  required(atom()) => [any()],
  page_number: non_neg_integer(),
  page_size: non_neg_integer(),
  total_pages: non_neg_integer(),
  total_entries: non_neg_integer(),
  distance: non_neg_integer(),
  sort_field: String.t(),
  sort_direction: String.t()
}
@type t() :: {:ok, page()} | {:error | any()}