YUI Library Home

YUI Library Examples: Paginator: Rendering controls into multiple containers

Paginator: Rendering controls into multiple containers

In this example, we will add pagination to an ordered list. Some things to note:

  • Pagination controls are added in a <span> as well as in a <p>.
  • All included pagination controls use inline elements, so the containers needn't be block elements.
  • A custom skin treatment has been applied.
  • State changes made to the Paginator propagate to all controls in all containers.

1987 US Billboard Top 40!

Random content with pagination controls embedded inline. Suspendisse vestibulum dignissim quam. Integer vel augue. Phasellus nulla purus, interdum ac, and here they are. < 1 - 10 of the Top 40 > and now back to random content habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.

  1. Walk Like An Egyptian - Bangles

  2. Alone - Heart

  3. Shake You Down - Gregory Abbott

  4. I Wanna Dance With Somebody (Who Loves Me) - Whitney Houston

  5. Nothing's Gonna Stop Us Now - Starship

  6. C'est La Vie - Robbie Nevil

  7. Here I Go Again - Whitesnake

  8. The Way It Is - Bruce Hornsby & The Range

  9. Shakedown - Bob Seger

  10. Livin' On A Prayer - Bon Jovi

< 1 - 10 of the Top 40 >

Example Data

In this example, we'll be working with a data array stored in YAHOO.example.data.top40.

1YAHOO.namespace('example.data').top40 = [ 
2    "Walk Like An Egyptian - Bangles"
3    "Alone - Heart"
4    ... 
5]; 
view plain | print | ?

Start with the content

All of Paginator's UI Components render inline elements, so you can include them almost anywhere. We'll create two container elements in our content, a <span>, nested in a paragraph above the list, and a separate <p> below it.

1<div id="demo"
2    <h2>1987 US Billboard Top 40!</h2> 
3 
4    <p> 
5        Random content with pagination controls embedded inline. 
6        Suspendisse vestibulum dignissim quam. Integer vel augue. 
7        Phasellus nulla purus, interdum ac, and here they are. 
8        <span id="span_container"></span> 
9        and now back to random content habitant morbi tristique 
10        senectus et netus et malesuada fames ac turpis egestas. 
11    </p> 
12 
13    <ol id="content" start="1"
14        <!-- the paginated content will go here --> 
15    </ol> 
16 
17    <id="p_container"></p> 
18</div> 
view plain | print | ?

Add code to update the content region

We'll generate the content by pulling a slice of our data array and wrapping each item in <li> and <p> tags.

1YAHOO.util.Event.onDOMReady(function () { 
2 
3// Place code in the YAHOO.example namespace 
4var Ex = YAHOO.namespace('example'); 
5 
6Ex.content = YAHOO.util.Dom.get('content'); 
7 
8Ex.handlePagination = function (index,count) { 
9    // Gather the content for the requested page 
10    var recs = Ex.data.top40.slice(index, index + count); 
11 
12    // Update the content UI 
13    Ex.content.start = index + 1; 
14    Ex.content.innerHTML = '<li><p>'+recs.join('</p></li><li><p>')+'</p></li>'
15}; 
16 
17... 
view plain | print | ?

Add pagination

Create a Paginator, identifying the two containers span_container and p_container. For fun, we use a custom template and configure the included UI Components for extra customization.

1Ex.paginator = new YAHOO.widget.Paginator({ 
2    rowsPerPage : 10, 
3    totalRecords : Ex.data.top40.length, 
4    containers : ['span_container','p_container'], 
5 
6    template : "{PreviousPageLink} {CurrentPageReport} {NextPageLink}"
7    previousPageLinkLabel : "&lt;"
8    nextPageLinkLabel : "&gt;"
9    pageReportTemplate : "{startRecord} - {endRecord} of the Top {totalRecords}" 
10}); 
view plain | print | ?

Attach the content handler to the Paginator's changeRequest event and make the appropriate changes in the handler to use the Paginator's passed state data. render() the Paginator and, in this case, call the content generation code directly to initialize the list.

1Ex.handlePagination = function (state) { 
2    // Gather the content for the requested page 
3    var startIndex = state.recordOffset, 
4        recs = Ex.data.top40.slice(startIndex, startIndex + state.rowsPerPage); 
5 
6    // Update the content UI 
7    Ex.content.start = startIndex + 1; 
8    Ex.content.innerHTML = '<li><p>'+recs.join('</p></li><li><p>')+'</p></li>'
9 
10    // Confirm state change with the Paginator 
11    Ex.paginator.setState(state); 
12}; 
13 
14... 
15 
16Ex.paginator.subscribe('changeRequest', Ex.handlePagination); 
17 
18Ex.paginator.render(); 
19 
20// To populate the list initially, call the handler directly passing 
21// the Paginator's current state 
22Ex.handlePagination(Ex.paginator.getState()); 
view plain | print | ?

Add style

For this example, we've given the UI Components some special visual treatment. Outside of this, there is one CSS override that was necessary for the <span> container.

Though the elements themselves are inline elements, the container is styled as display: block by Sam skin. To keep the <span> from breaking the normal rendering of the enclosing <p>, we add the following CSS:

1/* override some skin styles */ 
2.yui-skin-sam span.yui-pg-container { 
3    displayinline
4} 
view plain | print | ?

Full code listing

JavaScript

1YAHOO.util.Event.onDOMReady(function () { 
2 
3// Place code in the YAHOO.example namespace 
4var Ex = YAHOO.namespace('example'); 
5 
6Ex.content = YAHOO.util.Dom.get('content'); 
7 
8Ex.handlePagination = function (state) { 
9    // Gather the content for the requested page 
10    var startIndex = state.recordOffset, 
11        recs = Ex.data.top40.slice(startIndex, startIndex + state.rowsPerPage); 
12 
13    // Update the content UI 
14    Ex.content.start = startIndex + 1; 
15    Ex.content.innerHTML = '<li><p>'+recs.join('</p></li><li><p>')+'</p></li>'
16 
17    // Confirm state change with the Paginator 
18    Ex.paginator.setState(state); 
19}; 
20 
21Ex.paginator = new YAHOO.widget.Paginator({ 
22    rowsPerPage : 10, 
23    totalRecords : Ex.data.top40.length, 
24    containers : ['span_container','p_container'], 
25 
26    template : "{PreviousPageLink} {CurrentPageReport} {NextPageLink}"
27    previousPageLinkLabel : "<"
28    nextPageLinkLabel : ">"
29    pageReportTemplate : "{startRecord} - {endRecord} of the Top {totalRecords}" 
30}); 
31 
32 
33Ex.paginator.subscribe('changeRequest', Ex.handlePagination); 
34 
35Ex.paginator.render(); 
36 
37Ex.handlePagination(Ex.paginator.getState()); 
view plain | print | ?

CSS

1/* override some skin styles */ 
2.yui-skin-sam span.yui-pg-container { 
3    display: inline; 
4
5.yui-skin-sam .yui-pg-current { 
6    margin: 0; 
7
8.yui-skin-sam #demo .yui-pg-container a:link, 
9.yui-skin-sam #demo .yui-pg-container a:active, 
10.yui-skin-sam #demo .yui-pg-container a:visited, 
11.yui-skin-sam #demo .yui-pg-container a:hover, 
12.yui-skin-sam #demo .yui-pg-container span.yui-pg-previous, 
13.yui-skin-sam #demo .yui-pg-container span.yui-pg-next { 
14    background: #fde; 
15    color: #f3c; 
16    text-decoration: none; 
17    border: 3px solid #f9c; 
18    padding: 0 3px; 
19    font-size: 130%; 
20    font-weight: bold; 
21
22.yui-skin-sam #demo .yui-pg-container span.yui-pg-previous, 
23.yui-skin-sam #demo .yui-pg-container span.yui-pg-next { 
24    background: #eee; 
25    color: #a6a6a6; 
26    border: 3px double #ccc; 
27
28.yui-skin-sam #demo .yui-pg-container a:hover { 
29    background: #f9c; 
30    color: #fff; 
31
32 
33/* demo specific styles */ 
34#demo h2 { 
35    border: none; 
36    border-bottom: 1ex solid #aaa; 
37    color: #333; 
38    font-size: 1.5em; 
39    line-height: 65%; 
40    margin-top: 0; 
41
42#content { 
43    margin: 0 0 0 4em; 
44    padding-top: 1em; 
45
46#content li { 
47    color: #f6c; 
48    font: bold italic 200%/.5 Arial, sans-serif; 
49    padding: 1px 0; 
50    margin: 0; 
51
52#content li p { 
53    color: #555; 
54    font: normal 50% Arial, sans-serif; 
55    margin: 0; 
56    line-height: 2; 
57
58
59#p_container { 
60    text-align: center; 
61
view plain | print | ?

Configuration for This Example

You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings