Skip to content
Pagination in yesod applications
Haskell
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
example Add example application
src/Yesod Behavior-neutral cleanup
test Implement a basic test suite
.gitignore Add stack.yml, default .gitignore
LICENSE bring files over from goodies repo
README.md Add example application
Setup.hs
circle.yml Add a circle.yml
stack.yaml Update to latest LTS snapshot
yesod-paginator.cabal Add example application

README.md

Yesod paginator

Handle a database query and/or array-math to paginate a list and produce a pagination widget suitable for Bootstrap.

Usage

Paginate directly out of the database:

getPageR :: Handler Html
getPageR = do
    (things, widget) <- runDB $ selectPaginated 10 [] []

    defaultLayout $ do
        [whamlet|
            $forall thing <- things
                ^{showThing $ snd thing}

            ^{widget}
            |]

Or an existing list in memory:

getPageR :: Handler Html
getPageR = do
    things' <- getAllThings

    (things, widget) <- paginate 10 things'

    defaultLayout $ do
        [whamlet|
            $forall thing <- things
                ^{showThing thing}

            ^{widget}
            |]

Just provide the pagination widget:

getPageR :: Handler Html
getPageR = do
    cur <- getCurrentPage

    let limit = 10

    (items, total) <- runSphinxSearch "query" limit

    defaultLayout $ do
        [whamlet|
          $forall thing <- things
              ^{showThing thing}

          ^{defaultWidget cur limit total}
          |]

Customization

getPageR :: Handler Html
getPageR = do
    (things, widget) <- selectPaginatedWith myWidget 10 [] []

    defaultLayout $ do
        -- ...

    where
        myWidget :: PageWidget App
        myWidget = paginationWidget $ PageWidgetConfig
            { prevText     = "Newer"
            , nextText     = "Older"
            , pageCount    = 7
            , ascending    = False
            , showEllipsis = False
            , listClasses  = ["pagination", "pagination-centered"]
            }

Example and Tests

Run a local example:

stack build --flag yesod-paginator:example
stack exec yesod-paginator-example

Then open http://localhost:3000 in the browser.

To run the tests, execute stack test

Something went wrong with that request. Please try again.