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

  1. australian online casino min deposit $10, online poker in australia legal
    and how to bet on united statesn tibia coins roulette [Rosita], or slots frenzy

  2. If youu wis forr tto imnprove yourr familiarit jst keep visitying
    thiis wweb ite annd bbe updated with thhe mst recent information posted here.
    xnxx

  3. the top online pokies and casinos in united states 60fps,
    free pokies in united states and best online hallmark casino bonus codes 2021 (Audra) united kingdom fast payouts,
    or canada online slots

  4. united statesn free pokies games, slot machine bases canada and new casino sites uk no deposit bonus, or online casino guide uk

    my website what are the rules of playing roulette – Garry,

  5. bingo slots uk, online odessa casino (Sharyn) no deposit bonus free spins
    usa and free spins no deposit uk low wagering, or free
    spins goukos quest

  6. top 10 online casinos in canada, free spins no deposit casinos usa blackjack
    billy blood sweat and beer (Jesus) united kingdom online pokies 2021, or win real money online
    free usa

  7. all new zealandn poker rules, slot payback info united states and betway poker australia,
    or canadian no deposit casino 2021

    my web site blackjack speed fill blacktop filler sds (Juliane)

  8. is internet poker legal in australia, play free slots united states and free $30
    pokies united states, or best usa poker apps

    Feel free to surf to my web-site – who owns the flamingo casino (Isis)

  9. best online pokies united states casino, is gambling illegal in the uk and new zealandn free slots no download, or
    el torero slot usa

    My site how to play craps step by step (Rudolf)

  10. can i use td australia visa debit for online gambling, united kingdom roulette
    free download and new gambling legislation united states, or are gambling winnings earned income usa

    Here is my web site … how do you win playing blackjack (Brigitte)

  11. online poker australia no deposit bonus, gambling in ontario australia and craps
    betting usa, or bouka spins no deposit bonus 2021

    Review my blog post; what are the odds of roulette – Williemae,

  12. real money poker sites canada, free spins no deposit
    casinos canada and united statesn poker 95 download, or united states live dutch online casino no deposit bonus (Ezra)

  13. top new united statesn online casino, good gambling sites uk and united kingdom online poker news,
    or slot machine sales uk

    Look at my site: open to casinos (Theron)

  14. european online casinos real money usa players free spins, no deposit bonus on sign up
    usa and where does native united statesn hilton casino lac leamy spa (Jack) money go, or fastest withdrawal online casino canada
    no deposit bonus

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>