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

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.

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

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

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.

Create a Paginator

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

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

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.

Render the Paginator

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

Full code listing



Text content clipped for brevity.


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