FreeMarker pagination macros

I couldn’t find a good set of macros to help navigate through paginated data so I wrote my own. The macro names echo the names of the CakePHP PaginationHelper functions because…well, they’re reasonably sensible.

The macros expect you to add a model attribute called “paginationData” which contains (at least) the following properties:

pageNumber -> The current page number
pageSize -> The number of items in each page
pagesAvailable -> The total number of pages
sortDirection -> The sorting direction (ascending or descending)
sortField -> The field currently sorted by

In my implementation, my paginationData also contains a property called “pageItems” – the actual items in this page.

The macros available to you are:

  • first
    Outputs a link to the first page
  • last
    Outputs a link to the last page
  • next
    Outputs a link to the next page
  • previous
    Outputs a link to the previous page
  • numbers
    Outputs links to other pages. The macro takes a parameter called “maxPages”, which is the maximum number of pages that should be output. There is also a second parameter “separator”, which is the text/html that should be output between each page link.
  • page
    Outputs a link to a particular page. It takes the page number and link text as parameters. If you omit the link text it’ll default to the page number
  • counter
    Outputs the current page number and the total pages e.g. “1 of 20″
  • sort
    Outputs a link to sort by a field. Takes the field to sort by as its first parameter. If field is different to the current sort field, the link will change the sort field but not the sort direction. If the field is the same as the current sort field, the link will change the sort direction. The second parameter is the link text, which defaults to the field name with an upper case first letter. It also takes a parameter called “directions”. This is an an array of two items: the words being used in paginationData.sortDirection to describe the sorting direction of ascending or descending. Default: ["Asc", "Desc"]. So it can compare the current sorting direction and switch to the converse.

Note, ordinarily you’ll probably not need to use the “page” macro.

How to use

In Java, add a model attribute “paginationData” containing the properties I’ve described above. Then, in your view, import the library:

    <#import "pagination.ftl" as pagination />

Call the macros:

    <nav style="float:right;">
        <@pagination.first />
        <@pagination.previous />
        <@pagination.numbers />
        <@pagination.next />
        <@pagination.last />
    </nav>
    <@pagination.counter />

Which’ll give you something like:

For table headings that allow sorting:

    <table>
        <tr>
            <th><@pagination.sort "forename" /></th>
            <th><@pagination.sort "surname" /></th>
            <th><@pagination.sort "email" /></th>
            <th><@pagination.sort "created" "Registration date" /></th>
        </tr>
        ...
    </table>

Download

You can download the pagination macros here.

6,342 thoughts on “FreeMarker pagination macros

  1. united kingdom poker 90 s, mobile poker real money australia and gambling times
    usa, or free 10 no deposit casino uk

    Also visit my web site – what is okie dokie pokie mean (Keisha)

  2. best real money casino united kingdom, gousaos quest free spins
    no 500% deposit bonus, Corrine, and free spins bonus codes uk, or casino chips canada

  3. best online pokie canada, top casino slots uk and no deposit casino bonus usa august 2021, or play slots online for real united states

    Feel free to surf to my web site :: how to beat the roulette table anywhere (Finley)

  4. uk free spins 2021, free chip no deposit usa and casino bonus usa, or canadian online top casino card game apps (Barney) sign up no deposit bonus

  5. bonanza slot volatility, united statesn online pokies paysafe and
    uk gambling bill, or online casino not in uk

    Here is my homepage – saturday morning bingo (Nida)

  6. ausaa ca casino, real money pokies canada app and pokies real money united
    states, or online uk casino woth 24 hours payout

    Visit my web blog … roulette systems (Lukas)

  7. united states online live casino voucher theft (Gerardo), free pokie games
    united kingdom and new zealandn online pokies for real
    money, or online casinos accepting usa

  8. online keno for real money australia, personal gambling licence usa and real money pathfinder 2e gambling, Raymon, online united states,
    or online slots no deposit bonus usa

  9. united statesn free pokies games, best online pokies in canada and how to win china africa bonausaa slot
    machine, or free online poker machine games
    united kingdom

    My web site roulette wheel app android (Nick)

  10. yusaon gold slots, 888 poker login australia and no 5 deposit by phone
    bill (Florene) cash bonus
    casino canada, or big poker tournaments in australia

  11. no deposit wcasino bonus codes – Dwain – australian casino, sweet bonausaa online slot and grand mondial
    casino australia, or age to enter casino in united kingdom

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>