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,344 thoughts on “FreeMarker pagination macros

  1. top 10 casino in canada, play pokies australia no money required
    and online moses lake wa casino (Pauline) uk 5 deposit,
    or online casino not in uk

  2. free online roulette uk, best online slot sites new
    zealand and age to go to casino are pokies open in nsw today (Janina) new zealand,
    or usa online casino ratings

  3. best online poker usa real money, alea casino (Maximo) coquitlam bc united kingdom and do you have to pay
    tax on casino winnings in canada, or real money poker apps united states

  4. new 2021 casino uk, bouka spins no deposit bonus and best online casino usa 2021, or online real casino
    usa

    Feel free to visit my site … inventore roulette russa (Dwayne)

  5. casino amusements united kingdom, bingo australia promo codes and united kingdom best online
    casino, or fastest withdrawal online casino canada no deposit bonus

    my web-site; paroli roulette strategy [Arnette]

  6. milwaukee casino iowa, aristocrat lightning link pokies united
    kingdom versions and nz casino online bgo (Elmer) online, or new casino uk

  7. slots no wagering uk, $150 free no deposit casino australia and craps betting usa, or canada
    online slots

    My web blog :: casinos wv open (Marcel)

  8. top bingo site usa, winner slots uk and are online casino with
    first free spin, Ernesto,
    winnings taxable usa, or best australia pokies

  9. united kingdom casino deposit free no spin, legitimate online pokies australia and canadian pokies winners, or new casino bonus uk

    Also visit my web blog how to play blackjack the right way (Chance)

  10. free australian pokies wheres the gold, new zealandn online casinos
    no deposit and new zealandn free slots no download,
    or united kingdom poker machines online free

    My web page :: states in india where gambling
    is legal (Jamila)

  11. 150 free spins australia, susanville casino and no registration casino usa, or are
    the pokies open at crown casino (Scott) blackjacks homepage australia

  12. 5dollar deposit casino united states, slot free spins no deposit uk and
    blackjack canada online, or niagara falls usa casino

    Feel free to surf to my webpage :: caesars palace craps table
    minimum (Ervin)

  13. new bingo site usa, online casinos canada no deposit bonus and
    best what is good about online gambling (Malcolm)
    slot new zealand, or new 2021 usa casinos online

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>