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.
The Paginator Control allows you to reduce the page size and render time of your site or web application by breaking up large data sets into discrete pages. Paginator addresses the navigation aspect of chunked content, offering a set of controls that it can render into your UI to allow users to navigate through logical sections of local or remote data.
The Paginator Control was introduced with DataTable in version 2.5.0, but it has been broken out for general use as of version 2.6.0.
To use Paginator, include the following source files in the web page:
<!-- Core + Skin CSS --> <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.9.0/build/paginator/assets/skins/sam/paginator.css"> <!-- Dependencies --> <script src="http://yui.yahooapis.com/2.9.0/build/yahoo-dom-event/yahoo-dom-event.js"></script> <script src="http://yui.yahooapis.com/2.9.0/build/element/element-min.js"></script> <!-- Source File --> <script src="http://yui.yahooapis.com/2.9.0/build/paginator/paginator-min.js"></script>
<!-- Core + Skin CSS --> <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/2.9.0/build/paginator/assets/skins/sam/paginator.css"> <!-- Dependencies --> <script src="http://yui.yahooapis.com/2.9.0/build/yahoo-dom-event/yahoo-dom-event.js"></script> <script src="http://yui.yahooapis.com/2.9.0/build/element/element-min.js"></script> <!-- Source File --> <script src="http://yui.yahooapis.com/2.9.0/build/paginator/paginator-min.js"></script>
yui-skin-sam
class name to an element that is a parent of the element
in which the Paginator Control lives. You can usually accomplish this simply by putting the class on the
<body>
tag:
<body class="yui-skin-sam">
<body class="yui-skin-sam">
For more information on skinning YUI components and making use of default skins, see our Understanding YUI Skins article here on the website.
Instead of copying and pasting the filepaths above, try letting the YUI dependency Configurator determine the optimal file list for your desired components; the Configurator uses YUI Loader to write out the full HTML for including the precise files you need for your implementation.
Note: If you wish to include this component via the YUI Loader, its module name is paginator
. (Click here for the full list of module names for YUI Loader.)
Where these files come from: The files included using the text above will be served from Yahoo! servers. JavaScript files are minified, meaning that comments and white space have been removed to make them more efficient to download. To use the full, commented versions or the -debug
versions of YUI JavaScript files, please download the library distribution and host the files on your own server.
Order matters: As is the case generally with JavaScript and CSS, order matters; these files should be included in the order specified above. If you include files in the wrong order, errors may result.
Paginator does not have to render any controls, but it is likely that you will want to. All you need in your markup are container nodes into which the Paginator can render its controls. One Paginator instance can render controls into any number of containers.
<div id="pag1"><!-- pagination controls will go here --></div> <!-- depending on the pagination UI components you choose, you could also use --> <span id="pag2"><!-- and pagination controls will go here --></span> <p id="pag3"><!-- and pagination controls will go here --></p>
<div id="pag1"><!-- pagination controls will go here --></div> <!-- depending on the pagination UI components you choose, you could also use --> <span id="pag2"><!-- and pagination controls will go here --></span> <p id="pag3"><!-- and pagination controls will go here --></p>
Currently, it is not strictly required that the container elements be included in the live DOM, but that may change to support future or custom UI components.
The Paginator class is located at YAHOO.widget.Paginator
. The constructor takes a single argument, an object literal containing configuration values.
var pag = new YAHOO.widget.Paginator({ rowsPerPage : 10, totalRecords : 100, containers : [ idStr1, idStr2 ] // or idStr or elem or [ elem, elem ] }); pag.render();
var pag = new YAHOO.widget.Paginator({ rowsPerPage : 10, totalRecords : 100, containers : [ idStr1, idStr2 ] // or idStr or elem or [ elem, elem ] }); pag.render();
Configuration can be omitted at construction, but these attributes must be set before the Paginator is render()
ed.
var pag = new YAHOO.widget.Paginator(); //... pag.set('rowsPerPage', 10); pag.set('totalRecords', 100); pag.set('containers', containerEl); // OR pag.setState({ rowsPerPage : 10, totalRecords : 100, containers : containerEl }); pag.render();
var pag = new YAHOO.widget.Paginator(); //... pag.set('rowsPerPage', 10); pag.set('totalRecords', 100); pag.set('containers', containerEl); // OR pag.setState({ rowsPerPage : 10, totalRecords : 100, containers : containerEl }); pag.render();
By default, Paginator will render controls into each specified container like this:
This section describes how to customize the Paginator and includes:
Paginator does not directly modify your content. The key to wiring up Paginator to work with your page, application, or widget is to subscribe to its changeRequest
event.
var pag = new YAHOO.widget.Paginator({ rowsPerPage : 10, totalRecords: 100, containers : 'paging' }); var MyApp = { /* application API */ //... handlePagination : function (newState) { // Collect page data using the requested page number var pageContent = MyApp.getContent(newState.page); // Update the content area MyApp.replaceContent(pageContent); // Update the Paginator's state pag.setState(newState); } }; pag.subscribe('changeRequest',MyApp.handlePagination);
var pag = new YAHOO.widget.Paginator({ rowsPerPage : 10, totalRecords: 100, containers : 'paging' }); var MyApp = { /* application API */ //... handlePagination : function (newState) { // Collect page data using the requested page number var pageContent = MyApp.getContent(newState.page); // Update the content area MyApp.replaceContent(pageContent); // Update the Paginator's state pag.setState(newState); } }; pag.subscribe('changeRequest',MyApp.handlePagination);
When users interact with any of the Paginator's controls (or calls are made to the Paginator's public API methods) the Paginator does not immediately update its state and UI. Instead, it broadcasts its changeRequest
event to all subscribers, passing an object describing the requested pagination state.
As a subscriber, your app is then responsible for collecting and displaying the data or content appropriate for that state, then updating the Paginator's state accordingly. The Paginator's UI updates automatically when it's state is changed.
The object provided to changeRequest
subscribers is the output from the Paginator's getState
method. Called without input, getState
returns an object literal in the following form:
{ paginator : (the Paginator instance), page : (int), // the current page records : [ (int), (int} ], // index offsets of first and last records on the current page recordOffset : (int), // index offset of the first record on the current page totalRecords : (int), // current totalRecords value rowsPerPage : (int) // current rowsPerPage value }
{ paginator : (the Paginator instance), page : (int), // the current page records : [ (int), (int} ], // index offsets of first and last records on the current page recordOffset : (int), // index offset of the first record on the current page totalRecords : (int), // current totalRecords value rowsPerPage : (int) // current rowsPerPage value }
If passed an object literal with proposed values for any of these properties (except paginator
and records
), the returned state will contain the full calculated state given the provided modifications and an additional property, before
. The value assigned to before
will be the current state.
// Get the current pagination state var cur = pag.getState(); // Get the proposed state if rowsPerPage were updated and we moved to page 6 var proposed = pag.getState({ rowsPerPage : 15, page : 6 }); // The proposed state contains the current state, stored under 'before' // Assert.areEqual(cur.page, proposed.before.page);
// Get the current pagination state var cur = pag.getState(); // Get the proposed state if rowsPerPage were updated and we moved to page 6 var proposed = pag.getState({ rowsPerPage : 15, page : 6 }); // The proposed state contains the current state, stored under 'before' // Assert.areEqual(cur.page, proposed.before.page);
Paginator does not store transitional states. When users interact with the UI, the changeRequest
event is fired with the proposed state object and immediately forgotten. Until the subscriber directly updates the Paginator's state, the Paginator will remain in its current state.
Some of the more useful methods in Paginator's API are:
changeRequest
with the proposed page.changeRequest
with the proposed rowsPerPage
.changeRequest
with the proposed totalRecords
.Note: the Paginator's state is maintained in its attributes recordOffset
, totalRecords
, and rowsPerPage
. Calling set
directly on these attributes will cause the UI to update if necessary. It will not trigger a changeRequest
.
// Broadcast a changeRequest to subscribers. totalRecords remains unchanged pag.setTotalRecords(100); // totalRecords changed. UI updated. changeRequest will not be fired pag.set('totalRecords',100);
// Broadcast a changeRequest to subscribers. totalRecords remains unchanged pag.setTotalRecords(100); // totalRecords changed. UI updated. changeRequest will not be fired pag.set('totalRecords',100);
A full list of configuration options is included in the API docs. The most common configurations passed at construction are:
YAHOO.widget.Paginator.VALUE_UNLIMITED
if there is no logical limit to the set. (required).false
, the configured containers will be hidden unless there is enough data for at least two pages. true
by default.Additionally, UI Components add their configuration options onto the host Paginator, so their configuration can be done during construction as well.
var pag = new YAHOO.widget.Paginator({ rowsPerPage : 10, totalRecords : 50, containers : [ "pag-above", "pag-below" ], template : "{PageLinks} {RowsPerPageDropdown}", pageLinks : 5, // configure the PageLinks UI Component rowsPerPageOptions : [ 10, 25, 50, 100 ] // configure the RowsPerPageDropdown UI Component
var pag = new YAHOO.widget.Paginator({ rowsPerPage : 10, totalRecords : 50, containers : [ "pag-above", "pag-below" ], template : "{PageLinks} {RowsPerPageDropdown}", pageLinks : 5, // configure the PageLinks UI Component rowsPerPageOptions : [ 10, 25, 50, 100 ] // configure the RowsPerPageDropdown UI Component
In addition to the de facto change events emitted when its attributes are modified (e.g. totalRecordsChange
), Paginator broadcasts the following interesting moments:
This section describes how the Paginator's UI is constructed and configured.
template
configuration attributePaginator is built on a core model and a UI Component framework. The core (Paginator) is responsible for state calculations, and the UI Components are responsible for appropriately representing that state in the DOM. Each UI Component is its own class, built to handle a specific function. Included with the current Paginator release are the following UI Components:
template
configuration attributeThe link between the Paginator core and the UI components is the Paginator's template
configuration attribute.
The template
attribute contains a string of markup mixed with placeholders identifying where in the markup each UI component should reside.
var pag = new YAHOO.widget.Paginator({ rowsPerPage : 50, totalRecords : myData.length, containers : containerEl, template : "{PreviousPageLink} <strong>{CurrentPageReport}</strong> {NextPageLink}" }); pag.render();
var pag = new YAHOO.widget.Paginator({ rowsPerPage : 50, totalRecords : myData.length, containers : containerEl, template : "{PreviousPageLink} <strong>{CurrentPageReport}</strong> {NextPageLink}" }); pag.render();
this would render something like this:
If multiple containers are specified, the same markup will be generated into each container, so avoid including any elements with an id
attribute in your template.
Each UI Component comes with its own set of configuration attributes for added flexibility. These attributes are added to the host Paginator, allowing configuration during the Paginator's construction. For a full list of available configuration attributes, check the respective class definitions in the API docs or the configuration example.
Some more common UI Component configurations are:
The template
mechanism is convenient for common cases, but it is possible to manually place individual UI Components on the page. Each UI Component has a render(id)
method that returns the generated DOM structure for that component. Coupling this with the DOM methods insertBefore
or appendChild
, you can have complete control of where and how many UI Components are added to your page.
var pag = new YAHOO.widget.Paginator({ rowsPerPage : 10, totalRecords : myData.length }); var currentPageDisplay = new YAHOO.widget.Paginator.ui.CurrentPageReport(pag); YAHOO.util.Dom.get('pageDisplay').appendChild( currentPageDisplay.render('pageDisplay') );
var pag = new YAHOO.widget.Paginator({ rowsPerPage : 10, totalRecords : myData.length }); var currentPageDisplay = new YAHOO.widget.Paginator.ui.CurrentPageReport(pag); YAHOO.util.Dom.get('pageDisplay').appendChild( currentPageDisplay.render('pageDisplay') );
Another approach to piecemeal rendering is to use the paginator.renderUIComponent( placeholderElement, idSeed )
method to progressively enhance existing markup. The placeholder element is inspected for a class name in the form of yui-pg-ui-COMPONENT
where COMPONENT is the name of the UI component class you want rendered there. For example, the following code enhances a "last page" link from existing markup:
// Replaces <a href="..." id="last_pg" class="yui-pg-ui-LastPageLink">...</a> // in the markup with the rendered markup from an instance of // YAHOO.widget.Paginator.ui.LastPageLink var paginator = new YAHOO.widget.Paginator({ ... }), placeholder = YAHOO.util.Dom.get('last_pg'); // The second param is a string to help ensure id uniqueness. It's up to you. pag.renderUIComponent(placeholder, 'xyz');
// Replaces <a href="..." id="last_pg" class="yui-pg-ui-LastPageLink">...</a> // in the markup with the rendered markup from an instance of // YAHOO.widget.Paginator.ui.LastPageLink var paginator = new YAHOO.widget.Paginator({ ... }), placeholder = YAHOO.util.Dom.get('last_pg'); // The second param is a string to help ensure id uniqueness. It's up to you. pag.renderUIComponent(placeholder, 'xyz');
Notice the class to be instantiated is inferred from the placeholder's class name. Also notice the containers
configuration is not used in either case, nor is the Paginator render()
ed. When placing UI Components manually, neither is necessary.
The Paginator core and UI Component architecture makes it easy to create new pagination controls. When render()
ing its template
, Pagintor looks for UI Component classes in the YAHOO.widget.Paginator.ui
namespace matching any bracketed placeholders.
// renders an instance of YAHOO.widget.Paginator.ui.FirstPageLink var pag1 = new YAHOO.widget.Paginator({ rowsPerPage : 10, totalRecords : myData.length, containers : 'foo', template : "jump to {FirstPageLink}" }); // Looks for a class YAHOO.widget.Paginator.ui.MyCustomControl to // instantiate and render var pag2 = new YAHOO.widget.Paginator({ rowsPerPage : 10, totalRecords : myData.length, containers : 'bar', template : "jump to {MyCustomControl}" });
// renders an instance of YAHOO.widget.Paginator.ui.FirstPageLink var pag1 = new YAHOO.widget.Paginator({ rowsPerPage : 10, totalRecords : myData.length, containers : 'foo', template : "jump to {FirstPageLink}" }); // Looks for a class YAHOO.widget.Paginator.ui.MyCustomControl to // instantiate and render var pag2 = new YAHOO.widget.Paginator({ rowsPerPage : 10, totalRecords : myData.length, containers : 'bar', template : "jump to {MyCustomControl}" });
New Paginator UI Components should follow some basic guidelines:
YAHOO.widget.Paginator.ui
namespace.init(paginator)
to add any configurations to the Paginator instance.render(id_base)
to create and return the control's DOM structure.render
method will be called once for every configured container, so don't create duplicate ids (the id_base
is to help avoid this).Here is a starting template for new Paginator UI Components.
Don't forget to share your own new components on the YUI developer forums or even contribute them directly to the library with a pull request from your fork of the yui2 project.
Paginator started its life in the DataTable widget in version 2.5.0. Beyond feature enhancements and bug fixes, Paginator has remained the same. It can be used with DataTable in the same fashion now as then.
var tblConfig = { paginator : new YAHOO.widget.Paginator({ rowsPerPage : 25 }), initialRequest : '', selectionMode : 'single' }; var myDataTable = new YAHOO.widget.DataTable(tblId, colDef, myDataSource, tblConfig);
var tblConfig = { paginator : new YAHOO.widget.Paginator({ rowsPerPage : 25 }), initialRequest : '', selectionMode : 'single' }; var myDataTable = new YAHOO.widget.DataTable(tblId, colDef, myDataSource, tblConfig);
About this Section: YUI generally works well with mobile browsers that are based on A-Grade browser foundations. For example, Nokia's N-series phones, including the N95, use a browser based on Webkit — the same foundation shared by Apple's Safari browser, which is found on the iPhone. The fundamental challenges in developing for this emerging class of full, A-Grade-derived browsers on handheld devices are:
There are other considerations, many of them device/browser specific (for example, current versions of the iPhone's Safari browser do not support Flash). The goal of these sections on YUI User's Guides is to provide you some preliminary insights about how specific components perform on this emerging class of mobile devices. Although we have not done exhaustive testing, and although these browsers are revving quickly and present a moving target, our goal is to provide some early, provisional advice to help you get started as you contemplate how your YUI-based application will render in the mobile world.
More Information:
There are no known issues with the rendering or operation of the Paginator on mobile devices.
The YUI Library and related topics are discussed on the on the YUILibrary.com forums.
Also be sure to check out YUIBlog for updates and articles about the YUI Library written by the library's developers.
The YUI Library's public bug tracking and feature request repositories are located on the YUILibrary.com site. Before filing new feature requests or bug reports, please review our reporting guidelines.
be the first to bookmark this page!
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.