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.

1,027 thoughts on “FreeMarker pagination macros

  1. australian online casino min deposit $10, usa no deposit casino bonus list and casino reviews usa, or usa
    real money slots no deposit bonus

    my blog post – play blackjack card game (Joy)

  2. new usa online casinos no deposit bonuses, where can united statesns play roulette online and united kingdom
    online poker news, or online roulette for real money usa

    Also visit my webpage ada blackjack atlas obscura (Darin)

  3. best online casino bc australia, betsoft no deposit bonus united
    kingdom stand-alone casinos are usually small and are often called – Quyen, canadian roulette
    app, or free spins no wagering requirements usa

  4. best casino bonus new zealand, tiger gaming poker uk
    and arcade slot machines for sale uk, or casino in united state of australia

    Feel free to surf to my web-site; buy blackjack table top
    (Margret)

  5. free online casinos australia, free chip no deposit usa and
    casino in johannesburg south africa (Marie) chips
    value uk, or are slot machines illegal in united
    kingdom

  6. new zealandn no deposit bonus casinos, australia casino pokies and strategies online
    casino (Christiane)
    slots free bonus no deposit uk, or big poker tournaments in australia

  7. best casino bonuses 2021 usa, new online casino canada no deposit play and earn money app
    ios; Esperanza, online
    gambling laws new zealand, or casino tax recovery united
    kingdom

  8. united states no deposit pokies, titan casino bonus code (Tobias) in montana usa and casino
    kingdom usa, or biggest pokie win in united states

  9. canadian online bingo games, spin usa fernley casino – Elizabeth – and all new zealandn casino no deposit bonus, or $5
    min deposit new zealandn casinos

  10. major poker in canada, free spins no deposit casinos canada and bug bausaai
    slots, or canadian no deposit casino wizard table
    games; Carlota,
    2021

  11. australia merlot wine slot, best casino games to win big
    money (Joe) jack milwaukee and bet365 united statesn roulette guide uk, or free cash bonus no deposit casino canada

  12. uk casino no deposit bonus no wagering, casino age limit uk and
    canadian online casino that accepts paypal, or can i use td canada trust visa debit for online
    3 player gambling card games, Jerrell,

  13. online what episode does booth start gambling again (Karolyn) legal in united states,
    united kingdom roulette free download and online
    craps australia, or win real money online casino for free usa

  14. are pokies open in new zealand, no deposit online casino united states and casinos how to get gems in casino cooking fever (Debora) saskatchewan united states, or new usa live casino

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>