Skip to content
Snippets Groups Projects
Commit 8166139a authored by Daniel Herzog's avatar Daniel Herzog
Browse files

Add minified version. Add README.md

parent c96f22dc
No related branches found
No related tags found
No related merge requests found
# jQuery paginate
## Summary
`jquery-paginate` is a simple [`jquery`](https://jquery.com/) plugin that allows html websites to paginate tables and other kind of containers.
A working example can be found [here]().
## Installation
Install this package as a bower dependecy with:
```
bower install jquery-paginate
```
or download the `jquery-paginate.min.js` file and include it in your website:
```html
<script src='jquery.min.js'></script>
<!-- Add it after your jquery file! -->
<script src='jquery-paginate.min.js'></script>
```
## Basic usage
Imagine the following html table:
```html
<table id="myTable">
<thead>
<tr>
<td>Header 1</td>
<td>Header 2</td>
</tr>
</thead>
<tbody>
<tr>
<td>Row 1.1</td>
<td>Row 1.2</td>
</tr>
<tr>
<td>Row 2.1</td>
<td>Row 2.2</td>
</tr>
<!-- ... -->
</tbody>
</table>
```
You can paginate your table by using the `paginate` method on your selector object:
```js
$('#myTable').paginate({ limit: 10 });
```
A navigation bar will be created under the table, and the table will show 10 elements per page. You can style that navigation bar with the your custom css:
```css
.page-navigation a {
margin: 0 2px;
display: inline-block;
padding: 3px 5px;
color: #ffffff;
background-color: #70b7ec;
border-radius: 5px;
text-decoration: none;
font-weight: bold;
}
.page-navigation a[data-selected] {
background-color: #3d9be0;
}
```
See [this working demo](). You can see the available options in the [Advanced options](#advanced-options) section.
## Advanced options
| Option name | Default value | Description |
|--------|--------|--------|
| `limit` | `20` | Elements shown per page. |
| `initialPage` | `0` | Default selected page, being 0 the first one. |
| `previous` | `true` | *Previous* button, to move to the previous page. |
| `previousText` | `'<'` | Text for *Previous* button. Will be shown only if `previous` is `true`. |
| `next` | `true` | *Next* button, to move to the next page. |
| `nextText` | `'>'` | Text for *Next* button. Will be shown only if `next` is `true`. |
| `first` | `true` | *First* button, to move to first page. |
| `firstText` | `'>'` | Text for *First* button. Will be shown only if `first` is `true`. |
| `last` | `true` | *Last* button, to move to last page. |
| `lastText` | `'>'` | Text for *Last* button. Will be shown only if `last` is `true`. |
| `optional` | `true` | If this option is `true`, then the pagination menu will be added only if the container has more elements than the `limit` value. i.e. Will be added only if there are more than 1 page. |
| `onCreate` | `null` | A callback to be called when the pagination is initialized. Should have the following structure: `function(jquery_table_object) {}` |
| `onSelect` | `null` | A callback to be called when any page is selected. Should have the following structure: `function(jquery_table_object, current_page_index) {}` |
| `childrenSelector` | `'tbody > tr'` | A **jquery selector string** to extract the table children. This can be handy if you are working with *divs* instead of *tables*. |
| `navigationWrapper` | `null` | A **jquery object** to append the navigation bar to it. This can be used to put your navigation bar on a sticky footer, for example. If `null`, then it will be added after the table. |
| `navigationClass` | `'page-navigation'` | A **css class name** applied to the navigation menu bar. Can contain multiple classes names, separated with spaces. |
| `pageToText` | `function(i) { return (i + 1).toString(); }` | A javascript function to transform the current page index (*0...N-1*) to a string, shown in the navigation menu. |
For example, a working example with all options:
```js
$('#myTable').paginate({
limit: 10, // 10 elements per page
initialPage: 1, // Start on second page
previous: true, // Show previous button
previousText: 'Previous page', // Change previous button text
next: true, // Show previous button
nextText: 'Next page', // Change next button text
first: true, // Show first button
firstText: 'First', // Change first button text
last: true, // Show last button
lastText: 'Last', // Change last button text
optional: false, // Always show the navigation menu
onCreate: function(obj) { console.log('Pagination done!'); } // `onCreate` callback
onSelect: function(obj, i) { console.log('Page ' + i + ' selected!'); } // `onSelect` callback
childrenSelector: 'tbody > tr.ugly', // Paginate the rows with the `ugly` class
navigationWrapper: $('div#myNavWrapper'), // Append the navigation menu to the `#myNavWrapper` div
navigationClass: 'my-page-navigation', // New css class added to the navigation menu
pageToText: function(i) { return (i + 1).toString(16); } // Page numbers will be shown on hexadecimal notation
});
```
## Authors
This project has been developed by:
| Avatar | Name | Nickname | Email |
| ------- | ------------- | --------- | ------------------ |
| ![](http://www.gravatar.com/avatar/2ae6d81e0605177ba9e17b19f54e6b6c.jpg?s=64) | Daniel Herzog | Wikiti | [wikiti.doghound@gmail.com](mailto:wikiti.doghound@gmail.com) |
Loading
Loading
@@ -13,8 +13,10 @@
this.obj = obj;
this.options = opts;
 
this._createNavigation();
this._setPage();
if(!this.options.optional || this._totalRows() > this.options.limit) {
this._createNavigation();
this._setPage();
}
 
if(this.options.onCreate) this.options.onCreate(obj);
 
Loading
Loading
@@ -33,39 +35,42 @@
});
},
_createNavigationButtons: function() {
// TODO: Add callbacks to buttons
// Add 'first' button
if(this.options.first)
if(this.options.first) {
this._createNavigationButton(this.options.firstText, {
'data-first': true
});
}
 
// Add 'previous' button
if(this.options.previous)
if(this.options.previous) {
this._createNavigationButton(this.options.previousText, {
'data-direction': -1,
'data-previous': true
});
}
 
// Add page buttons
for(var i = 0; i < this._totalPages(); ++i)
for(var i = 0; i < this._totalPages(); ++i) {
this._createNavigationButton(this.options.pageToText(i), {
'data-page': i
});
}
 
// Add 'next' button
if(this.options.next)
if(this.options.next) {
this._createNavigationButton(this.options.nextText, {
'data-direction': 1,
'data-next': true
});
}
 
// Add 'last' button
if(this.options.last)
if(this.options.last) {
this._createNavigationButton(this.options.lastText, {
'data-last': true
});
}
},
_createNavigationButton: function(text, options) {
this.nav.append($('<a>', $.extend(options, { href: '#', text: text })));
Loading
Loading
@@ -81,22 +86,28 @@
 
paginator.nav.find('a').click(function(e) {
var direction = $(this).data('direction') * 1;
// 'First' button
if($(this).data('first') !== undefined) {
paginator._setPage(0);
}
// Page button
else if ($(this).data('page') !== undefined) {
paginator._setPage($(this).data('page') * 1);
}
// 'Previous' or 'Next' button
else if ($(this).data('previous') !== undefined || $(this).data('next') !== undefined) {
var page = paginator._currentPage() + direction;
if(page >= 0 && page <= paginator._totalPages() - 1) {
paginator._setPage(page);
}
}
// 'Last' button
else if ($(this).data('last') !== undefined) {
paginator._setPage(paginator._totalPages() - 1);
}
 
// Handle callback
if(paginator.options.onSelect) paginator.options.onSelect(paginator.obj, paginator._currentPage());
e.preventDefault();
return false;
Loading
Loading
!function(t){"use strict"
var i=function(){return{obj:null,options:null,nav:null,build:function(t,i){return this.obj=t,this.options=i,this._createNavigation(),this._setPage(),this.options.onCreate&&this.options.onCreate(t),this.obj},_createNavigation:function(){this._createNavigationWrapper(),this._createNavigationButtons(),this._appendNavigation(),this._addNavigationCallbacks()},_createNavigationWrapper:function(){this.nav=t("<div>",{"class":this.options.navigationClass})},_createNavigationButtons:function(){this.options.first&&this._createNavigationButton(this.options.firstText,{"data-first":!0}),this.options.previous&&this._createNavigationButton(this.options.previousText,{"data-direction":-1,"data-previous":!0})
for(var t=0;t<this._totalPages();++t)this._createNavigationButton(this.options.pageToText(t),{"data-page":t})
this.options.next&&this._createNavigationButton(this.options.nextText,{"data-direction":1,"data-next":!0}),this.options.last&&this._createNavigationButton(this.options.lastText,{"data-last":!0})},_createNavigationButton:function(i,a){this.nav.append(t("<a>",t.extend(a,{href:"#",text:i})))},_appendNavigation:function(){this.options.navigationWrapper?this.options.navigationWrapper.append(this.nav):this.obj.after(this.nav)},_addNavigationCallbacks:function(){var i=this
i.nav.find("a").click(function(a){var n=1*t(this).data("direction")
if(void 0!==t(this).data("first"))i._setPage(0)
else if(void 0!==t(this).data("page"))i._setPage(1*t(this).data("page"))
else if(void 0!==t(this).data("previous")||void 0!==t(this).data("next")){var e=i._currentPage()+n
e>=0&&e<=i._totalPages()-1&&i._setPage(e)}else void 0!==t(this).data("last")&&i._setPage(i._totalPages()-1)
return i.options.onSelect&&i.options.onSelect(i.obj,i._currentPage()),a.preventDefault(),!1})},_setPage:function(t){t||(t=this.options.initialPage),this._rows().hide().slice(t*this.options.limit,(t+1)*this.options.limit).show(),this.nav.find("a").removeAttr("data-selected").siblings("a[data-page="+t+"]").attr("data-selected",!0)},_currentPage:function(){return this.nav.find("a[data-selected=true]").data("page")},_totalRows:function(){return this._rows().length},_rows:function(){return this.obj.find(this.options.childrenSelector)},_totalPages:function(){return Math.floor(this._totalRows()/this.options.limit)}}}
t.fn.paginate=function(a){switch(a){default:return i().build(this,t.extend({},t.fn.paginate.defaults,a))}},t.fn.paginate.defaults={limit:20,initialPage:0,previous:!0,previousText:"<",next:!0,nextText:">",first:!0,firstText:"<<",last:!0,lastText:">>",optional:!0,onCreate:null,onSelect:null,childrenSelector:"tbody > tr",navigationWrapper:null,navigationClass:"page-navigation",pageToText:function(t){return(t+1).toString()}}}(jQuery)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment