YUI Library Home

Site Search (YDN & YUIBlog):

# 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.

View example in new window.

## The Monster

By Stephen Crane

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.

<div id="content"> <div> <p>Little Jim was, for the ...</p> <p>Jim dropped the tongue of ...</p> </div> <div> <p>He went on to the lawn...</p> (and so on)

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

#content div { /* hide all pages */ display: none; } #demo .page1 div.page1, #demo .page2 div.page2, #demo .page3 div.page3, #demo .page4 div.page4, #demo .page5 div.page5 { /* display the current page */ display: block; }

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

<div id="paging"></div> <div id="content" class="page1"> <div class="page1"> <p>Little Jim was, for the ...</p> <p>Jim dropped the tongue of ...</p> </div> <div class="page2"> <p>He went on to the lawn...</p> (and so on)

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.

YAHOO.util.Event.onDOMReady(function () { // Set up the application under the YAHOO.example namespace var Ex = YAHOO.namespace('example'); Ex.content = YAHOO.util.Dom.get('content'); Ex.handlePagination = function (page) { // Show the appropriate content for the requested page Ex.content.className = 'page' + page; }; ...

## Create a Paginator

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

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

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

Ex.paginator.subscribe('changeRequest', Ex.handlePagination);

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.

Ex.handlePagination = function (state) { // Show the appropriate content for the requested page Ex.content.className = 'page' + state.page; // Update the Paginator's state, confirming change Ex.paginator.setState(state); };

## Render the Paginator

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

Ex.paginator.render();

## Full code listing

### CSS

#content { background: #fff; border: 1px solid #ccc; color: #000; font-family: Times New Roman, serif; padding: 1em 2em; } /* hide all pages */ #content div { display: none; } /* display the current page */ #demo .page1 div.page1, #demo .page2 div.page2, #demo .page3 div.page3, #demo .page4 div.page4, #demo .page5 div.page5 { display: block; }

### HTML

Text content clipped for brevity.

<div id="demo"> <h2 class="first">The Monster</h2> <p>By Stephen Crane</p> <div id="paging"></div> <div id="content" class="page1"> <div class="page1"> <p>Little Jim was...</p> <p>Jim dropped the...</p> </div> <div class="page2"> <p>He went on to...</p> <p>The doctor was...</p> <p>The doctor paused...</p> </div> <div class="page3"> <p>The doctor stared...</p> <p>“Pa!” repeated the...</p> <p>“What?” said the...</p> <p>After a period of...</p> </div> <div class="page4"> <p>It seemed that the...</p> <p>The doctor mused...</p> <p>Together they...</p> <p>“Where?” said...</p> <p>Jimmie kicked at...</p> </div> <div class="page5"> <p>The doctor was...</p> <p>The father reflected...</p> <p>The child answered...</p> </div> </div> </div>

### JavaScript

YAHOO.util.Event.onDOMReady(function () { // Set up the application under the YAHOO.example namespace var Ex = YAHOO.namespace('example'); Ex.content = YAHOO.util.Dom.get('content'); Ex.handlePagination = function (state) { // Show the appropriate content for the requested page Ex.content.className = 'page' + state.page; // Update the Paginator's state, confirming change Ex.paginator.setState(state); }; // Create the Paginator widget and subscribe to its changeRequest event Ex.paginator = new YAHOO.widget.Paginator({ rowsPerPage : 1, totalRecords : Ex.content.getElementsByTagName('div').length, containers : 'paging' }); Ex.paginator.subscribe('changeRequest', Ex.handlePagination); // Render the Paginator into the configured container(s) Ex.paginator.render(); });

## 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.

### YUI Logger Output:

Note: Logging and debugging is currently turned off for this example.

Reload with logging
and debugging enabled.

### Paginator Examples:

- Getting started with Paginator
- Rendering controls into multiple containers
- Manually rendering Paginator UI Components
- Configuring the Paginator
- Client-side Pagination (included with examples for DataTable Control)
- Server-side Pagination and Sorting for Dynamic Data (included with examples for DataTable Control)
- Complex Example of Multiple Features (included with examples for DataTable Control)

### More Paginator Resources:

- User's Guide (external)
- API Documentation

Copyright © 2010 Yahoo! Inc. All rights reserved.

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