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

  1. After study a couple of of the blog posts on your web site now, and I actually like your approach of blogging. I bookmarked it to my bookmark website list and will be checking again soon. Pls check out my web page as nicely and let me know what you think.

  2. how best probability to win roulette (Ulysses) win pokies nz,
    top slot games uk and free $30 pokies united states, or online
    casino new usa

  3. the top online pokies and casinos in united states legit, open casino in australia and canada offshore gambling, or best online pa
    casino promotions (Wilma) new zealand fast
    payouts

  4. gambling mustang money free online pokie game (Vonnie) in usa, chusachansi casino takeover and state gambling revenue australia, or
    new zealandn online gambling sites

  5. best free online casino new zealand, mobile poker real money
    australia and play pokies online canada, or bingo online for money united kingdom

    my blog: gulf coast ms casinos entertainment (Jasmin)

  6. united kingdom casino no deposit bonus codes 2021,
    latest no deposit casino game mobile app (Ian) bonuses usa
    and real money online casino canada app, or registration bonus casino usa

  7. united kingdom online slots royal river casino promotions – Klaus -, australia casino pokies and play
    yukon gold slot machine, or top 5 online
    pokies united states

  8. casino near heiuk field, new zealandn win real money
    games online (Rhoda) casino
    sign up no deposit bonus and online casino uk free bonus
    no deposit, or usa online casinos gambling

  9. united kingdom online slots casino, how to memorize united statesn roulette wheel and new online casinos for real money
    usa, or free no deposit double u casino android hack – Valarie,
    uk 2021

  10. crypto gambling usa, real pokies canada
    and australian casino chips, or new casinos no deposit bonus usa

    My website :: odds of winning roulette 30 times in a
    row – Jed -

  11. casinos in toronto ontario australia, online casino instant withdrawal uk and casinos in united states vancouver,
    or top online pokies and casinos canadian open 2021

    Feel free to surf to my blog … abzorba live blackjack hack apk (Ella)

  12. is online games that make real money (Vernita) poker legal in united states,
    how many the top online pokies and casinos in australia and uk casino free bonus,
    or top online pokies and casinos united statesn league

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>