YUI recommends YUI3.
YUI 2 has been deprecated since 2011. This site acts as an archive for files and documentation.
This documentation is no longer maintained.
In this example, we will add pagination to an ordered list. Some things to note:
<span>
as well as in a <p>
.Random content with pagination controls embedded inline. Suspendisse vestibulum dignissim quam. Integer vel augue. Phasellus nulla purus, interdum ac, and here they are. and now back to random content habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.
In this example, we'll be working with a data array stored in YAHOO.example.data.top40
.
1 | YAHOO.namespace('example.data').top40 = [ |
2 | "Walk Like An Egyptian - Bangles", |
3 | "Alone - Heart", |
4 | ... |
5 | ]; |
view plain | print | ? |
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 | <p id="p_container"></p> |
18 | </div> |
view plain | print | ? |
We'll generate the content by pulling a slice
of our data array and wrapping each item in <li>
and <p>
tags.
1 | YAHOO.util.Event.onDOMReady(function () { |
2 | |
3 | // Place code in the YAHOO.example namespace |
4 | var Ex = YAHOO.namespace('example'); |
5 | |
6 | Ex.content = YAHOO.util.Dom.get('content'); |
7 | |
8 | Ex.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 | ? |
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.
1 | Ex.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 : "<", |
8 | nextPageLinkLabel : ">", |
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.
1 | Ex.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 | |
16 | Ex.paginator.subscribe('changeRequest', Ex.handlePagination); |
17 | |
18 | Ex.paginator.render(); |
19 | |
20 | // To populate the list initially, call the handler directly passing |
21 | // the Paginator's current state |
22 | Ex.handlePagination(Ex.paginator.getState()); |
view plain | print | ? |
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 | display: inline; |
4 | } |
view plain | print | ? |
1 | YAHOO.util.Event.onDOMReady(function () { |
2 | |
3 | // Place code in the YAHOO.example namespace |
4 | var Ex = YAHOO.namespace('example'); |
5 | |
6 | Ex.content = YAHOO.util.Dom.get('content'); |
7 | |
8 | Ex.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 | |
21 | Ex.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 | |
33 | Ex.paginator.subscribe('changeRequest', Ex.handlePagination); |
34 | |
35 | Ex.paginator.render(); |
36 | |
37 | Ex.handlePagination(Ex.paginator.getState()); |
view plain | print | ? |
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 | ? |
Note: Logging and debugging is currently turned off for this example.
All YUI 2.x users should review the YUI 2.8.2 security bulletin, which discusses a vulnerability present in YUI 2.4.0-2.8.1.
Copyright © 2013 Yahoo! Inc. All rights reserved.