jschomay/elm-paginate/1.1.0

NOTE: this package is not compatible with Elm 0.19.1

Paginate

Separate the presentation-domain concerns of pagination from the business-domain of your data.

The Pager type

type Pager

The Pager type holds all of the information necessary to track your pagination without directly affecting your data.

Constructing and updating

init : Int -> Int -> Pager

Create a new pager. Pass it the desired number of items per page and total number of items to be paginated. The current page is always initialized to 1. The number of items per page will be set to 1 if you provide 0 or a negative number.

  init 10 (List.length allMyThings)
      |> currentPage
  -- (equals 1)
update : Int -> Int -> Pager -> Pager

Update a pager by passing in the new items per page and new total number of items to be paginated. You would want to do this any time the number of items in the data you are paginating changes or if you want a different page size. If the newly calculated number of pages is less than the current page, the current page will be set to the new last page.

Changing pages

goTo : Int -> Pager -> Pager

Set the current page directly. If the specified page is "out of bounds" of the pager, it will be set to the first or last page accordingly.

first : Pager -> Pager

Go to the first page.

last : Pager -> Pager

Go to the last page.

Paginating

page : Pager -> List a -> List a

Provide a list and you will get the "slice" of that list according to the pager's state.

page (goTo 3 <| init 10 100) (List.range 1 100)
-- (equals [21, 22, 23, 24, 25, 26, 27, 28, 29 30])

Accessors

currentPage : Pager -> Int

Get the current page of the Pager

itemsPerPage : Pager -> Int

Get the number of items per page

totalPages : Pager -> Int

Get the total number of pages

isFirst : Pager -> Bool

Useful to conditionally show a "prev" button.

isLast : Pager -> Bool

Useful to conditionally show a "next" button.

Rendering

toList : (Int -> Bool -> a) -> Pager -> List a

Turn your pager into a list. Usually you will do this when you want to render a view for your pager, but it could be a list of anything. The supplied function is given the current page number being iterated over and whether that page is the current page of the pager or not.

init 1 3
    |> next
    |> toList ((,))
-- (equals [(1,False),(2,True),(3,False)])