Pager Tutorial - Markup-driven

Documentation for a historical release of Infusion: 1.3
Please view the Infusion Documentation site for the latest documentation.
If you're looking for Fluid Project coordination, design, communication, etc, try the Fluid Project Wiki.

Pager Tutorial - Markup-driven

Full Renderer support has been added to the Pager, providing a data-driven mode – this is the recommended way of using the Pager. For a tutorial describing how to use the data-driven Pager, see Pager Tutorial. This tutorial describes a more complex scenario where you supply your own markup to the Pager.



Step 1: Prepare your markup

This tutorial shows how to use the Pager in an entirely mark-up driven approach. This means that you will provide all the markup yourself instead of having the Pager generate it.

The Pager requires certain minimum markup from you:

  1. The entire pager must be contained within a container element.

  2. Within that container, there are one or two 'pager bar' elements, each containing:

    • any number of page links

    • optionally, a 'next' element and a 'previous' element

So, with some styles, you might want something that looks like this:

Let's assume that you're starting with HTML file that produces something like the image above (but simplified, so that this tutorial doesn't get too verbose):

<ul> <li><a href="#">&lt; previous</a></li> <li><a href="#">1</a></li> <li><a href="#">2</a></li> <li><a href="#">3</a></li> <li><a href="#">next &gt;</a></li> </ul> <table> <tr> <th>Album</th> <th>Tracks</th> <th>Country</th> <th>Year</th> </tr> <tr> <td>Actress: Birth Of The New York Dolls</td> <td>11</td> <td>Italy</td> <td>2000</td> </tr> <tr> <td>After The Storm</td> <td>8</td> <td>England</td> <td>1985</td> </tr> <tr> <td>Back In The U.S.A.</td> <td>19</td> <td>Italy</td> <td>1991</td> </tr> </table> <ul> <li><a href="#">&lt; previous</a></li> <li><a href="#">1</a></li> <li><a href="#">2</a></li> <li><a href="#">3</a></li> <li><a href="#">next &gt;</a></li> </ul>

The Pager component needs to know about the 'container' of your markup. We'll wrap a <div> around the whole thing, and give it a unique ID:

<div id="my-pager"> <ul> <li><a href="#">&lt; previous</a></li> <li><a href="#">1</a></li> <li><a href="#">2</a></li> ... </div>

We also need to identify the various parts to the Pager. We'll do this by attaching the default CSS class names that the Pager will recognize.

The Pager recognizes several default class names:

  • flc-pager-top for the container of the link bar above the content

  • flc-pager-bottom for the container of the link bar below the content

  • flc-pager-pageLink for any link inside a link bar

  • flc-pager-next for the 'next' link

  • flc-pager-previous for the 'previous' link

We'll consider the <ul> elements to be the containers for the top and bottom link bars, and the <li> elements themselves to be the links:

<div id="my-pager"> <ul class="flc-pager-top"> <li class="flc-pager-previous"><a href="#">&lt; previous</a></li> <li class="flc-pager-pageLink"><a href="#">1</a></li> <li class="flc-pager-pageLink"><a href="#">2</a></li> <li class="flc-pager-pageLink"><a href="#">3</a></li> <li class="flc-pager-next"><a href="#">next &gt;</a></li> </ul> ... <ul class="flc-pager-bottom"> <li class="flc-pager-previous"><a href="#">&lt; previous</a></li> <li class="flc-pager-pageLink"><a href="#">1</a></li> <li class="flc-pager-pageLink"><a href="#">2</a></li> <li class="flc-pager-pageLink"><a href="#">3</a></li> <li class="flc-pager-next"><a href="#">next &gt;</a></li> </ul> </div>

You may have noticed that we haven't touched the <table> elements between the top and bottom link bars. That's because when used in a markup driven way, the Pager honestly couldn't care what you've got in there. The Pager is only concerned with the link bars, and what happens when you click on those links. And in fact, the Pager doesn't really care what you do when the links are clicked - it simply tells you which link was activated and hands control over to you to act accordingly. More on that soon, but for now, we need to be able to know which link was activated. We'll do this by adding a 'value' to each numbered <li> element:

<div id="my-pager"> <ul id="pager-top" class="flc-pager-top"> <li class="flc-pager-previous"><a href="#">&lt; previous</a></li> <li value="1" class="flc-pager-pageLink"><a href="#">1</a></li> <li value="2" class="flc-pager-pageLink"><a href="#">2</a></li> <li value="3" class="flc-pager-pageLink"><a href="#">3</a></li> <li class="flc-pager-next"><a href="#">next &gt;</a></li> </ul> ... <ul id="pager-bottom" class="flc-pager-bottom"> <li class="flc-pager-previous"><a href="#">&lt; previous</a></li> <li value="1" class="flc-pager-pageLink"><a href="#">1</a></li> <li value="2" class="flc-pager-pageLink"><a href="#">2</a></li> <li value="3" class="flc-pager-pageLink"><a href="#">3</a></li> <li class="flc-pager-next"><a href="#">next &gt;</a></li> </ul> </div>

That's all - these are the only changes you need to make to your HTML.


Step 2: Write the script

You'll need to create a file, say search-results.js, to contain your initialization script - the script you write to apply the Pager to your search results mark-up.

In this file, write a function that initializes the Pager:

var demo = demo || {}; (function ($, fluid) { demo.initPager = function () { fluid.pager("#my-pager"); }; })(jQuery, fluid);

This function creates the Pager component, with your <div> as the container.

At the bottom of your HTML file, you will call this initialization function in a <script> tag:

<script type="text/javascript"> demo.initPager(); </script>

BUT: doing this alone is not sufficient. As mentioned above, the Pager is mark-up driven. As far as your script is concerned, this means that the Pager itself doesn't know what to do when a page link is clicked. All the Pager can do is call a function that you provide. You have to write that function.

The onModelChange Callback

Since the Pager doesn't actually know anything about your data, you have to take care of displaying the different pages of data yourself. The onModelChange function is used for this purpose.

The pageWillChange function is a function that you write and provide to the Pager, using the options parameter, as follow:

var myOnModelChangeFn = function (newModel, oldModel) { ... }; var options = { listeners: { onModelChange: myOnModelChangeFn } }; demo.initPager = function () { fluid.pager("#my-pager", options); };

When a page link is activated, the Pager will call your function. It's up to you to write a function that will replace the content of the Pager with the correct page of data. How you do that is entirely up to your application, and the nature of your data. The newModel structure which you are passed describes the pager's model, which means its current paging state - for this example, all you are interested in is the member named pageIndex which is the 0-based index of the page the user has just selected. More details on the Pager's model and general API is at Pager API.

In the scenario of this tutorial, you're creating a web front-end for your CD library, and using the Pager to chunk up search results. In this case, you would have to write a function that accepts the page number and in response, updates the page display with the relevant list of CDs from your search results.


Step 3: Add the script to your HTML

You'll need to add your initialization script, along with the Fluid library, to you HTML file. In the header of the file, link to the Javascript files with <script> tags:

<script type="text/javascript" src="infusion-1.2/InfusionAll.js"></script> <script type="text/javascript" src="search-results.js"></script>

NOTE that the InfusionAll.js file is minified - all of the whitespace has been removed, so it isn't really human-readable. If you're using the source distribution and you want to be able to debug the code, you'll want to include each of the required files individually. This would look like this:

<script type="text/javascript" src="infusion-1.2/lib/jquery/core/js/jquery.js"></script> <script type="text/javascript" src="infusion-1.2/lib/jquery/ui/js/ui.core.js"></script> <script type="text/javascript" src="infusion-1.2/framework/core/js/jquery.keyboard-a11y.js"></script> <script type="text/javascript" src="infusion-1.2/framework/core/js/Fluid.js"></script> <script type="text/javascript" src="infusion-1.2/framework/core/js/DataBinding.js"></script> <script type="text/javascript" src="infusion-1.2/components/pager/js/Pager.js"></script> <script type="text/javascript" src="search-results.js"></script>

But all of these individual files are not necessary to make it work - the InfusionAll.js file has everything you need.


Step 4: Apply styles

The Pager automatically adds CSS classes to elements in your mark-up at these 'interesting moments.' This provides you the opportunity to create styles for how you want the UI to change at these time.

There are two main classes that you will want to create styles for:

If we add a stylesheet with these styles, then when looking at the first page of search results, the page links will look like this: