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

  1. Undeniably believe that that you stated. Your favourite justification seemed to be at the web the simplest factor to keep in mind of. I say to you, I definitely get irked while other people consider worries that they just do not know about. You controlled to hit the nail upon the top and also defined out the entire thing with no need side-effects , people could take a signal. Will probably be again to get more. Thanks

  2. what casinos allow 18 year olds to gamble (Valeria) australia no
    deposit bonus, first casino in united states and free usa cash bingo, or newest online casino australia

  3. best canadian casino sites, real money slots free spins usa and 21dukes casino login, or canadian casino guide 2021 review

    Feel free to visit my web-site; gambling casinos in pa, Leatha,

  4. usa casino no deposit bonus code, uk casino uk and pokies real money united states, or united statesn casino
    regulation

    My homepage :: craps dice game python (Rickie)

  5. bousaa spins no deposit code, united kingdom pub pokies and united kingdom online pokies that accept paysafe, or
    new zealandn casino gambling craps tips

    Here is my blog: bingo play free mod apk – Royal -

  6. poker sites that accept paypal in united states, web gambling usa and online casino uk casino
    club, or top online pokies and casinos canadian free games

    Look at my web site … bubble craps max odds (Lisa)

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>