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

  1. 888 poker new zealand app, casino no deposit bonus crypto casino; Jeannie, uk and
    largest casino in australia 2021, or how much
    does canada spend on pokies

  2. 888 poker login united states, australia slot machine games
    and no deposit bonus uk poker, or dice poker rules michaud toys

    Take a look at my website – lake mead casino chips –
    Paula -

  3. independent online casino online 400 bonus,
    Shannon, usa,
    best payout online casino usa and $5 deposit online casino canada, or buy bingo game australia

  4. best online poker tournaments uk, slot machine australia and gambling issues in australia, or the top online pokies and casinos
    in australia day

    Also visit my website – how busy is san manuel casino right now [Gidget]

  5. united statesn online casinos real money, best canadian no deposit casinos and new zealandn roulette royale – free casino mod apk download [Larry] slots
    no download, or online slots united states reddit

  6. all slots online nz casino, roulette cake (Kyle) layout usa and usa
    online casinos free chip, or united statesn free slots
    no download

  7. new bingo site usa, best double win casino cheats (Ronald)
    app new zealand and royal panda casino canada, or the top online pokies and
    casinos in united states

  8. 5dollar deposit casino united states, 888 casino canada promo code and
    juki slot machine, or ignition poker withdrawal united states

    Stop by my web blog :: how many big spins are left 2022 (Billy)

  9. best online casino bc australia, no deposit sign up bonus casinos usa and
    how bingo getting to know you – Lela, cheat
    pokie machines canada, or best usa online slot sites

  10. top online pokies and casinos united statesn rockets,
    new usa gambling sites and canadian online is the orleans casino open (Sherman) that accepts paypal, or best online casino sites
    canada

  11. top online pokies and casinos united states information, online
    casinos canada no deposit bonus and online casino groups usa,
    or $5 deposit pokies canada

    Feel free to surf to my page: oasis bingo (Buck)

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>