terry-bit-io / elm-paginate / Paginate.Custom

Pagination for custom collection types.

Only use this module if you want to paginate something other than a List. This might be useful if you desire an Array or Dict or even LazyList or Stream as your collection, however, you will need to provide a length and slice function to many of the functions here (see Paginate for an implementation example, and for full documentation). Most of the time Paginate is what you will want to use.

The Paginated type


type Paginated a

The Paginated type wraps your custom collection and holds all of the information necessary to track pagination. It does not modify your collection in any way (unless you call Paginate.Custom.map).

Constructing and modifying

init : (a -> Basics.Int) -> Basics.Int -> a -> Paginated a

Create a new paginated collection. You must supply it with a function to get the length of your collection, as well as the desired number of items per page and your custom collection of items to be paginated. The current page is always initialized to 1. The minimum number of items per page is 1. The minimum number of total pages is 1 (even if you pass in an empty collection).

map : (a -> Basics.Int) -> (a -> a) -> Paginated a -> Paginated a

Transform the collection inside the Paginated by providing a function to apply to the wrapped collection. You must supply a length function as the first argument, then the transformation function. This is how you map, filter, sort and update items. If this function changes the length of the collection, the pagination calculations will be updated accordingly. If the newly calculated number of pages is less than the current page, the current page will be set to the new last page.

changeItemsPerPage : (a -> Basics.Int) -> Basics.Int -> Paginated a -> Paginated a

Change the paging size. You must supply a length function as the first argument. The total number of pages will be updated accordingly, and the current page will remain unchanged if possible. If the newly calculated number of pages is less than the current page, the current page will be set to the new last page. The minimum paging size is 1 item per page.

Changing pages

goTo : Basics.Int -> Paginated a -> Paginated a

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

next : Paginated a -> Paginated a

Go to the next page. Has no effect if you are already on the last page.

prev : Paginated a -> Paginated a

Go to the previous page. Has no effect if you are already on the first page.

first : Paginated a -> Paginated a

Go to the first page.

last : Paginated a -> Paginated a

Go to the last page.

Retrieving items

page : (Basics.Int -> Basics.Int -> a -> a) -> Paginated a -> a

Get the "slice" of the wrapped collection for the current page. You must supply a "slice" function as the first argument, which will be with a "from" (inclusive) and a "to" (exclusive). Usually you would call this and pass the result on to your view function.

foldMap : (a -> b) -> Paginated a -> b

Remove the pagination context and run a function on the wrapped collection.

Pager helpers

Functions to help build a "pager" and useful paging data

pager : (Basics.Int -> Basics.Bool -> b) -> Paginated a -> List b

Build a "pager" for your paginated collection. Usually you would use this to render the pager view. The supplied function is given the current page number being iterated over and whether that page is the current page.


type alias PagerOptions a =
{ innerWindow : Basics.Int
, outerWindow : Basics.Int
, pageNumberView : Basics.Int -> Basics.Bool -> a
, gapView : a 
}

PagerOptions is used by the elidedPager function to configure window sizes and output format. See elidedPager for examples of its use. The available options are as follows:

innerWindow

The number of page numbers to display on either side of the current page number. A negative number will be treated as 0.

outerWindow

The number of page numbers to display at the beginning and end of the page numbers. 0 means that the first and last pages will not be displayed. A negative number will be treated as 0.

pageNumberView

How to display the page numbers provided by the pager.

gapView

How to represent the gaps between page windows (if there are any).

elidedPager : PagerOptions b -> Paginated a -> List b

Builds an "elided" pager, which displays a "gap" placeholder in-between the first and last page(s) and the current page, if there are enough pages to justify doing so. This is useful for large collections where the number of pages might be huge and you don't want to display all of the page numbers at once.

renderPageNumberString pageNum isCurrentPage =
    if isCurrentPage then
        ">" ++ String.fromInt pageNum ++ "<"

    else
        String.fromInt pageNum

pagerOptions =
    { innerWindow = 1
    , outerWindow = 1
    , pageNumberView = renderPageNumberString
    , gapView = "..."
    }

paginatedList = fromList 2 (List.range 20) |> goTo 5

elidedPager pagerOptions paginatedList
--> [ "1", "...", "4", ">5<", "6", "...", "10" ]

elidedPager { pagerOptions | innerWindow = 0, outerWindow = 0 } paginatedList
--> [ ">5<" ]

currentPage : Paginated a -> Basics.Int

Get the current page of the Paginated.

itemsPerPage : Paginated a -> Basics.Int

Get the number of items per page.

totalPages : Paginated a -> Basics.Int

Get the total number of pages.

isFirst : Paginated a -> Basics.Bool

Useful to conditionally show a "prev" button.

isLast : Paginated a -> Basics.Bool

Useful to conditionally show a "next" button.