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

  1. casino amusements united kingdom, best rated online pokies new zealand how to play big and small casino game
    (Tyrone) top 20 online casinos usa, or online casino new usa

  2. top casino online germania (Hugh) bingo site usa,
    australia casino pokies and poker stars uk app, or best casino sites in united states

  3. are there casinos in montreal australia, no deposit required online
    who owns circa casino (Christiane) usa and roulette australia rules,
    or casino united states age

  4. mobile slots no deposit bonus usa, 10 dollar minimum deposit usa online casino 2021 and 100
    no deposit bonus codes 2021 canada, or free money online casino united
    kingdom

    Also visit my web blog :: how big is a craps table (Diane)

  5. how do poker machines work in australia, online pokies free spins on sign up australia and all united statesn 10 cent bingo,
    or northern lights renaissance aruba casino games, Mireya, uk

  6. best way to win united kingdom roulette, new
    zealandn express zahlung online casino and age of gods slots uk, or do casinos use chips (Laverne) in united
    states online

  7. online pokies canada law, slot machine bases canada and best
    jumba casino free spins (Silvia) site canada, or united
    states slots machines

  8. best australia casino no deposit bonus, chusachansi casino takeover
    and online slots australia paypal, or australian top online casino

    Feel free to visit my page; do casinos ever lose
    money (Emil)

  9. united kingdom banks gambling for resurrection [Slapp.com.br] news,
    online bingo uk no deposit and best casino in new zealand, or usa online casino
    no deposit bonus codes 2021

  10. top casino maximum roulette bet in vegas (Willard)
    canada for real money, free pokie games united kingdom and raging bull casino canada,
    or top ten slots usa

  11. best payout online slots canada, how to win on pokie machines usa and united statesn pokies companies, or united states online 7 feathers casino winners – Stacie,
    news

  12. free slot machine games united kingdom, usa blackjack online free and free bingo games united states, or las vegas
    brand casino quality playing cards (Morgan) usa
    casino $100

  13. united statesn roulette online fast, free slot machine games united states and black jack australia, or
    canada online slots

    Also visit my web blog: is gambling ethical (Carmine)

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>