Overview
Paginator is a customized pagination plugin made with jQuery. It's flexible and easy to use.
Live Demo
Feature
- Free
- Support most html contents.
- Support swiping on touch screen device.
- Support modern browser: IE9+, Firefox, Opera, Chrome, and Safari.
- Support auto pagination
Usage
Basic Usage
The HTML code looks like this (demo) :
1. Make sure to include jQuery, jquery.paginator.js, and paginator.css in your HTML file:
<head>
<script type="text/javascript" src="js/jquery-1.9.0.min.js"></script>
<script type="text/javascript" src="js/jquery.paginator.js"></script>
<link rel="stylesheet" href="css/paginator.css">
</head>
2. Create your paginator in the <body> tag. Your page content should be placed inside a <div class="page"> tag, and use a <div class="pages"> tag to wrap all the pages and put it inside a <div class="viewport"> tag.
There are four basic ways to control the paginator: 1) drop down selector, 2) input box, 3) index bar, and 4) swiping on a touch screen device. Swiping is a default, the other three can be implemented as the example below, and you can use css to define their position and style.
The example includes all of the three methods, but they are actually optional.
<div id="paginator1">
<select class="pageSelector"></select><--This is the dropdown selector-->
<div class="pageInputWrapper">Page Number: <input type="text" size="2" class="pageInput"></div><--This is the input box-->
<div class="viewport">
<div class="pages">
<div class="page" title="title1">page content</div>
<div class="page" title="title2">page content</div>
<div class="page" title="title3">page content</div>
</div>
</div>
<div class="pageIndexBar"></div><--This is the index bar-->
</div>
3. Apply it in JavaScript.
<script type="text/javascript">
$(function () {
$('#paginator1').paginator();
});
</script>
The syntax is:
.paginator([options], [callback]).
The options and callback function are both optional, you will find the details below.
Advance Usage
You can use not only <div> tag as each page, but also some other tags, like: <img />, <ul>, <table>, ... (please see the live demo above)
Just easily give them a class="page":
<img class="page" title="title1" />
<ul class="page" title="title2">list content</ul>
<form class="page" title="title3">form content</form>
<table class="page" title="title3">table content</table>
Note: This plugin doesn't solve the iframe dimesion problems on iOS, so iframe is not suggested
Note: The swipe method is not gonna work for a <video> tag which swallows all touch events on an iPad unless you remove the controls attribute; on iPhone or iPod Touch, the <video> tag is just a link to open the QuickTime app to play the video, so, it won't work even you remove the controls attribute.
The paginator allows you to use css to change style of its child elements (demo).
Note: The only exception is the class="pages" element, which is just a wrapper for all the page elements, it should not have left and right border, padding, or margin.
Pagination for a table is also possible. The concept is using <tbody> tags to be pages (demo) :
<table class="pages">
<tbody class="page" title="title1">
<tr>
<td>content</td>
<td>content</td>
<td>content</td>
</tr>
</tbody>
<tbody class="page" title="title2">
<tr>
<td>content</td>
<td>content</td>
<td>content</td>
</tr>
</tbody>
<tbody class="page" title="title3">
<tr>
<td>content</td>
<td>content</td>
<td>content</td>
</tr>
</tbody>
</table>
Options
The first argement of .paginator() is an option object. You can specify options when applying paginator:
$('#paginator1').paginator({autoPagination: true}); // turn on auto pagination
Available options
Option | Type | Default | Description |
---|---|---|---|
autoPagination | boolean | false |
If set to true, the page content elements shoulde be placed inside the <div class="pages"> tag, and each page will be automatically generated. (demo) Ex:
<div class="pages"> Note: The page will be generated according to the viewport height and the height of each element in the <div class="pages"> tag, so make sure:
|
displayNumber | integer | 9 | Set the max page number shoud be visible in the index bar. |
swipe | boolean | true | Allow users to swipe on the touch screen devices. |
APIs
You can use the APIs to control the paginator you just created. Using the same element to call these APIs.
$('#paginator1').paginator('selectPage', 1); // turn to page1
Control the Paginator
Syntax | Description |
---|---|
.paginator('initPaginator') |
Initialize the paginator. This method doens't accept arguments. Code example: Initialize the paginator. $('#paginator1').paginator('initPaginator'); |
.paginator('selectPage', pageId) |
Turn to a page. Code example: Turn to page 1. $('#paginator1').paginator('selectPage', 1); |
.paginator('insertPage', pageId, { content: htmlString, title: textString }) |
Insert a new page into the paginator and initialize it. The third argument is the page object including content and title, the second argument is the page id of the page you are gonna insert. The index is an option. If index is not defined, the new page will be automatically inserted after the last page. If the title is not defined, the page title will be page number. Code example: Insert a new page before page 1. $('#paginator1').paginator('insertPage', 1, { |
.paginator('updatePage', pageId, { content: htmlString, title: textString }) |
Update a page's content and title and initialize the paginator. The third argument is the page object including content and title, and the second argument is the page id of the page you are gonna update. Code example: Update page 1. $('#paginator1').paginator('updatePage', 1, { |
.paginator('removePage', pageId) |
Remove a page and initialize the paginator. Code example: Remove page 1. $('#paginator1').paginator('removePage', 1); |
Event Listener
Syntax | Description |
---|---|
.on('selectPage', handler) |
Called when a page is selected. Like the reguler event, the scope is bound to the selector itself. Code example:
$('#paginator1 .page:eq(0)').on('selectPage', function () { and this will be fired on page1 Note: For a page element only. |
Callback
The callback function will be fired right after the paginator is created. Ex:
$('#paginator1').paginator(function () {
console.log(this);
}); // show the object in console