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

  1. crypto gambling usa, gala bingo gift vouchers uk and black jack australia, or
    united kingdom roulette strategy to win big

    Here is my website … how much does casino machine cost (Delilah)

  2. milwausaee casino, new zealandn roulette free online game
    and online casino australia real money 2021, or sun bingo uk

    Also visit my site; how many casinos in elko nv; Frederic,

  3. new zealandn online casinos pokies, new casino non uk and free casino no deposit
    bonus uk, or australia top casino

    Feel free to visit my site; what cards to split blackjack,
    Stephaine,

  4. united statesn online crown casino financials;
    Andre, free spins sign up, online
    trusted casino usa and top 20 online casinos usa, or canadian casino
    guide slot payback info

  5. usa casino no deposit bonus code, canadian slots free play
    and craps gambling uk, or united statesn double win casino hack apk (Joe) free bonus
    codes

  6. no deposit usa online casino, united statesn casino no deposit bonus free money 2021 (Tressa) online and usa slot machine categories, or new zealandn roulette odds payout

  7. best no deposit casinos how to lose in craps – Tracy, new zealand,
    play real money poker online australia and online gambling
    market share uk, or top usa online casinos
    with no deposit bonuses

  8. free pokies canada aristocrat, big red pokie wins united kingdom 2021 and free spins no
    deposit uk low wagering, or free spins no deposit slots united kingdom

    my website :: casino film online bg audio (Timmy)

  9. are casino winnings taxed in australia, united kingdom roulette for vera&john mobile casino (Leanne) phones and free spins bonus usa, or european roulette layout usa

  10. bonanza slot volatility, spin casino australia and online casino uk free
    bonus no deposit, or online are there casinos in ocho rios jamaica (Rudolph) accepting usa

  11. new zealandn casino guide 2021, no deposit online casino united states
    and new zealand gambling sites, or gambling restrictions uk

    Also visit my site play blackjack bonus bets (Shaun)

  12. free online bingo odds formula, Charlene,
    australia, usa real money slots and how to bet on united statesn roulette, or cma uk
    gambling

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>