YUI recommends YUI 3.

YUI 2 has been deprecated since 2011. This site acts as an archive for files and documentation.

YUI Library Home

YUI Library Examples: Paginator: Getting started with Paginator

Paginator: Getting started with Paginator

In this example we illustrate the basics of attaching a Paginator to your application. We take a short story by Stephen Crane and divide it up into pages, then use Paginator to display the page navigation.

The Monster

By Stephen Crane

<< first < prev 12345 next > last >>

Little Jim was, for the time, engine Number 36, and he was making the run between Syracuse and Rochester. He was fourteen minutes behind time, and the throttle was wide open. In consequence, when he swung around the curve at the flower-bed, a wheel of his cart destroyed a peony. Number 36 slowed down at once and looked guiltily at his father, who was mowing the lawn. The doctor had his back to this accident, and he continued to pace slowly to and fro, pushing the mower.

Jim dropped the tongue of the cart. He looked at his father and at the broken flower. Finally he went to the peony and tried to stand it on its pins, resuscitated, but the spine of it was hurt, and it would only hang limply from his hand. Jim could do no reparation. He looked again toward his father.

He went on to the lawn, very slowly, and kicking wretchedly at the turf. Presently his father came along with the whirring machine, while the sweet, new grass blades spun from the knives. In a low voice, Jim said, “Pa!”

The doctor was shaving this lawn as if it were a priest’s chin. All during the season he had worked at it in the coolness and peace of the evenings after supper. Even in the shadow of the cherry-trees the grass was strong and healthy. Jim raised his voice a trifle. “Pa!”

The doctor paused, and with the howl of the machine no longer occupying the sense, one could hear the robins in the cherry-trees arranging their affairs. Jim’s hands were behind his back, and sometimes his fingers clasped and unclasped. Again he said, “Pa!” The child’s fresh and rosy lip was lowered.

The doctor stared down at his son, thrusting his head forward and frowning attentively. “What is it, Jimmie?”

“Pa!” repeated the child at length. Then he raised his finger and pointed at the flower-bed. “There!”

“What?” said the doctor, frowning more. “What is it, Jim?”

After a period of silence, during which the child may have undergone a severe mental tumult, he raised his finger and repeated his former word—“There!” The father had respected this silence with perfect courtesy. Afterward his glance carefully followed the direction indicated by the child’s finger, but he could see nothing which explained to him. “I don’t understand what you mean, Jimmie,” he said.

It seemed that the importance of the whole thing had taken away the boy’s vocabulary. He could only reiterate, “There!”

The doctor mused upon the situation, but he could make nothing of it. At last he said, “Come, show me.”

Together they crossed the lawn toward the flower-bed. At some yards from the broken peony Jimmie began to lag. “There!” The word came almost breathlessly.

“Where?” said the doctor.

Jimmie kicked at the grass. “There!” he replied.

The doctor was obliged to go forward alone. After some trouble he found the subject of the incident, the broken flower. Turning then, he saw the child lurking at the rear and scanning his countenance.

The father reflected. After a time he said, “Jimmie, come here.” With an infinite modesty of demeanour the child came forward. “Jimmie, how did this happen?”

The child answered, “Now—I was playin’ train—and—now—I runned over it.”

Start with your content

In this example we'll work with content that is already on the page in its entirety. This is not a requirement. The story has been divided up into a series of <div>s, each containing a "page" worth of the overall content.

1<div id="content"
2    <div> 
3        <p>Little Jim was, for the ...</p> 
4        <p>Jim dropped the tongue of ...</p> 
5    </div> 
6    <div> 
7        <p>He went on to the lawn...</p> 
8        (and so on) 
view plain | print | ?

Some CSS is added to identify the individual page divs, and hide all but the current page.

1#content div { /* hide all pages */ 
2    displaynone
3} 
4#demo .page1 div.page1
5#demo .page2 div.page2
6#demo .page3 div.page3
7#demo .page4 div.page4
8#demo .page5 div.page5 { /* display the current page */ 
9    displayblock
10} 
view plain | print | ?

A container is added above the content for the pagination controls.

1<div id="paging"></div> 
2<div id="content" class="page1"
3    <div class="page1"
4        <p>Little Jim was, for the ...</p> 
5        <p>Jim dropped the tongue of ...</p> 
6    </div> 
7    <div class="page2"
8        <p>He went on to the lawn...</p> 
9        (and so on) 
view plain | print | ?

This allows us to set a class on div#content and the higher specificity of the CSS selector will override the broadly applied display: none.

Add pagination support

In your JavaScript, create method to handle page change requests.

1YAHOO.util.Event.onDOMReady(function () { 
2 
3// Set up the application under the YAHOO.example namespace 
4var Ex = YAHOO.namespace('example'); 
5 
6Ex.content = YAHOO.util.Dom.get('content'); 
7 
8Ex.handlePagination = function (page) { 
9    // Show the appropriate content for the requested page 
10    Ex.content.className = 'page' + page; 
11}; 
12 
13... 
view plain | print | ?

Create a Paginator

Now create a Paginator instance to handle the UI and state management.

1Ex.paginator = new YAHOO.widget.Paginator({ 
2    rowsPerPage : 1, // one div per page 
3    totalRecords : Ex.content.getElementsByTagName('div').length, 
4    containers : 'paging' // controls will be rendered into this element 
5}); 
view plain | print | ?

Attach the Paginator instance to the method responsible for updating the content by subscribing to its changeRequest event.

1Ex.paginator.subscribe('changeRequest', Ex.handlePagination); 
view plain | print | ?

Paginator will pass its changeRequest subscribers a proposed state object, so we'll need to update the handlePagination method accordingly. Also, Paginator UI interaction only requests a page change, so we need to confirm the change by updating the Paginator's state.

1Ex.handlePagination = function (state) { 
2    // Show the appropriate content for the requested page 
3    Ex.content.className = 'page' + state.page; 
4     
5    // Update the Paginator's state, confirming change 
6    Ex.paginator.setState(state); 
7}; 
view plain | print | ?

Render the Paginator

The last step is to render() the Paginator instance.

1Ex.paginator.render(); 
view plain | print | ?

Full code listing

CSS

1#content { 
2    background: #fff; 
3    border1px solid #ccc; 
4    color: #000
5    font-family: Times New Roman, serif; 
6    padding1em 2em
7} 
8 
9/* hide all pages */ 
10#content div { 
11    displaynone
12} 
13 
14/* display the current page */ 
15#demo .page1 div.page1
16#demo .page2 div.page2
17#demo .page3 div.page3
18#demo .page4 div.page4
19#demo .page5 div.page5 { 
20    displayblock
21} 
view plain | print | ?

HTML

Text content clipped for brevity.

1<div id="demo"
2    <h2 class="first">The Monster</h2> 
3    <p>By Stephen Crane</p> 
4 
5    <div id="paging"></div> 
6 
7    <div id="content" class="page1"
8        <div class="page1"
9            <p>Little Jim was...</p> 
10            <p>Jim dropped the...</p> 
11        </div> 
12        <div class="page2"
13            <p>He went on to...</p> 
14            <p>The doctor was...</p> 
15            <p>The doctor paused...</p> 
16        </div> 
17        <div class="page3"
18            <p>The doctor stared...</p> 
19            <p>“Pa!” repeated the...</p> 
20            <p>“What?” said the...</p> 
21            <p>After a period of...</p> 
22        </div> 
23        <div class="page4"
24            <p>It seemed that the...</p> 
25            <p>The doctor mused...</p> 
26            <p>Together they...</p> 
27            <p>“Where?” said...</p> 
28            <p>Jimmie kicked at...</p> 
29        </div> 
30        <div class="page5"
31            <p>The doctor was...</p> 
32            <p>The father reflected...</p> 
33            <p>The child answered...</p> 
34        </div> 
35    </div> 
36</div> 
view plain | print | ?

JavaScript

1YAHOO.util.Event.onDOMReady(function () { 
2 
3// Set up the application under the YAHOO.example namespace 
4var Ex = YAHOO.namespace('example'); 
5 
6Ex.content    = YAHOO.util.Dom.get('content'); 
7 
8Ex.handlePagination = function (state) { 
9    // Show the appropriate content for the requested page 
10    Ex.content.className = 'page' + state.page; 
11     
12    // Update the Paginator's state, confirming change 
13    Ex.paginator.setState(state); 
14}; 
15 
16// Create the Paginator widget and subscribe to its changeRequest event 
17Ex.paginator = new YAHOO.widget.Paginator({ 
18    rowsPerPage : 1, 
19    totalRecords : Ex.content.getElementsByTagName('div').length, 
20    containers : 'paging' 
21}); 
22 
23Ex.paginator.subscribe('changeRequest', Ex.handlePagination); 
24 
25// Render the Paginator into the configured container(s) 
26Ex.paginator.render(); 
27 
28}); 
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 © 2011 Yahoo! Inc. All rights reserved.

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