项目作者: elixirdrops

项目描述 :
Ecto和Phoenix的分页。
高级语言: Elixir
项目地址: git://github.com/elixirdrops/kerosene.git
创建时间: 2016-03-08T16:03:26Z
项目社区:https://github.com/elixirdrops/kerosene

开源协议:MIT License

下载


Kerosene

Pagination for Ecto and Phoenix.

Installation

The package is available in Hex, the package can be installed as:

Add kerosene to your list of dependencies in mix.exs:

  1. def deps do
  2. [{:kerosene, "~> 0.9.0"}]
  3. end

Add Kerosene to your repo.ex:

  1. defmodule MyApp.Repo do
  2. use Ecto.Repo,
  3. otp_app: :testapp,
  4. adapter: Ecto.Adapters.Postgres
  5. use Kerosene, per_page: 2
  6. end

Usage

Start paginating your queries

  1. def index(conn, params) do
  2. {products, kerosene} =
  3. Product
  4. |> Product.with_lowest_price
  5. |> Repo.paginate(params)
  6. render(conn, "index.html", products: products, kerosene: kerosene)
  7. end

Add view helpers to your view

  1. defmodule MyApp.ProductView do
  2. use MyApp.Web, :view
  3. import Kerosene.HTML
  4. end

Generate the links using the view helpers

  1. <%= paginate @conn, @kerosene %>

Kerosene provides a list of themes for pagination. By default it uses bootstrap. To use some other, add to config/config.exs:

  1. config :kerosene,
  2. theme: :foundation

If you need reduced number of links in pagination, you can use simple mode option, to display only Prev/Next links:

  1. config :kerosene,
  2. mode: :simple

Building apis or SPA’s, no problem Kerosene has support for Json.

  1. defmodule MyApp.ProductView do
  2. use MyApp.Web, :view
  3. import Kerosene.JSON
  4. def render("index.json", %{products: products, kerosene: kerosene, conn: conn}) do
  5. %{data: render_many(products, MyApp.ProductView, "product.json"),
  6. pagination: paginate(conn, kerosene)}
  7. end
  8. def render("product.json", %{product: product}) do
  9. %{id: product.id,
  10. name: product.name,
  11. description: product.description,
  12. price: product.price}
  13. end
  14. end

You can also send in options to paginate helper look at the docs for more details.

Contributing

Please do send pull requests and bug reports, positive feedback is always welcome.

Acknowledgement

I would like to Thank

License

Please take a look at LICENSE.md