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.

9,887 thoughts on “FreeMarker pagination macros”

  1. famous united statesn poker, best australian casino sites and canadian casino sign up no deposit bonus, or free online pokies no
    download united kingdom

    Look into my homepage; top gambling cities in us – Jaclyn -

  2. canadian original free slots, the united states casino video
    and casino chips value uk, or no deposit doubleu casino coupon (Otilia) real money usa

  3. canadian casinos no deposit bonus, online gambling roulette united states and pokies real money australia, or top 5
    do online gambling sites cheat (Lucie) pokies united states

  4. online pokies canada law, united states free online slots and bet365 play new zealandn roulette online uk,
    or new best rated casino sites – Vada, no deposit bonus 2021 uk

  5. netent real money casino apk download (Maria) list usa,
    casinos uk and free online slot machines united kingdom, or canadian online casino with $5 deposit

  6. australia online casino bonus, poker australia tournaments and how old do
    you have to be to get in the casino in vegas (Noe) reviews usa, or $5 min deposit australian casinos

  7. Nice post. I was checking constantly this weblog and I am impressed! Extremely helpful info particularly the last part :) I care for such info much. I used to be seeking this certain information for a very lengthy time. Thanks and best of luck.

  8. top australian online pokies, best casinos in south united states and 1-3 no limit holdem strategy (Harold) deposit free bonus casinos usa, or real online australian pokies

  9. casino in vancouver australia, play free australian poker machines and online poker real earn money by playing games cash app – Barbara – usa 2021, or $1
    deposit bonus casino new zealand

  10. dollar 10 no deposit mobile casino united states, usa online casino reviews and bingo bonus usa welcome, or $10 deposit usa casinos

    my blog post free practice roulette games (Linwood)

  11. best apps to make money games (Annis)
    gambling website canada, play free australian poker machines and
    free united statesn poker money, or can i use td canada trust
    visa debit for online gambling

  12. best canadian when will eagle mountain casino open [Rod] sites, usa no deposit casino bonus list and free online pokies win real money united kingdom, or casino in australia near detroit

  13. when will pokies reopen south united states, usa online casino no deposit gambling in casinos
    pros and cons (Sabine) australian online no deposit casino,
    or new zealandn free chip casino

  14. prepaid visa for online gambling uk, non gamstop casinos uk and online monaco casino table limits – Latia – uk 5 deposit, or new zealandn roulette odds payout

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>