Skip to content

Instantly share code, notes, and snippets.

@jaibeee
Created July 2, 2014 09:01
Show Gist options
  • Save jaibeee/d6c1d356838a3d58a887 to your computer and use it in GitHub Desktop.
Save jaibeee/d6c1d356838a3d58a887 to your computer and use it in GitHub Desktop.
jQuery.fn.extend({
pagination: function () {
paginatorId = this;
switch (paginatorPosition) {
case 'top':
{
paginatorId.before('<div class="paginator"></div>');
break;
}
case 'bottom':
{
paginatorId.after('<div class="paginator"></div>');
break;
}
case 'both':
{
paginatorId.before('<div class="paginator"></div>');
paginatorId.after('<div class="paginator"></div>');
break;
}
default:
{
paginatorId.after('<div class="paginator"></div>');
}
}
initPaginator();
},
depagination: function () {
$('.paginator').remove();
paginatorId.children().show();
}
});
function initPaginator() {
if (itemsPerPage < 1) itemsPerPage = 5;
allItems = paginatorId.children().length;
if (allItems % itemsPerPage == 0) lastPage = parseInt(allItems / itemsPerPage);
else lastPage = parseInt(allItems / itemsPerPage) + 1;
if ((startPage < 1) || (startPage > lastPage)) startPage = 1;
if (!showIfSinglePage) {
if (lastPage > 1) appendContent(startPage, 1)
} else appendContent(startPage, 1)
}
function appendContent(a, b) {
if (a < 0) {
if (a == -1) a = currentPage - 1;
else a = currentPage + 1
}
currentPage = a;
till = (currentPage - 1) * itemsPerPage;
if (!b) {
paginatorId.fadeOut("medium", function () {
createPaginator();
paginatorId.children().hide();
paginatorId.children().slice(till, itemsPerPage + till).show();
paginatorId.fadeIn("medium");
})
} else {
createPaginator();
paginatorId.children().hide();
paginatorId.children().slice(till, itemsPerPage + till).show();
}
}
function createPaginator() {
jQuery(".paginator").html("");
var a = '';
var b = '';
var c = '';
var d = '';
var e = ' Page ' + currentPage + ' of ' + lastPage + ' Page(s) ';
var f = ' ' + textGoToPage + ' <select onchange="appendContent(this.value);">';
var g = ' ' + textSelectNoItems + ' </select>';
for (var i = 0; i < paginatorValues.length; i++) {
if (itemsPerPage == paginatorValues[i]) g += '<option value="' + paginatorValues[i] + '" selected="selected">' + paginatorValues[i] + '</option>';
else g += '<option value="' + paginatorValues[i] + '">' + paginatorValues[i] + '</option>'
}
g += '';
if (currentPage == 1) {
style = '<a href="' + anchorLink + '" class="inactive" title="First Page">' + firstPageSymbol + '</a>' + separator;
a = b = style;
style = '<a href="' + anchorLink + '" class="inactive" title="Previous Page">' + previousPageSymbol + '</a>' + separator;
a += style;
b += style;
c += style;
d += style;
} else {
style = '<a href="' + anchorLink + '" class="active" onclick="appendContent(1);" title="First Page">' + firstPageSymbol + '</a>' + separator;
a = b = style;
style = '<a href="' + anchorLink + '" class="active" onclick="appendContent(-1);" title="Previous Page">' + previousPageSymbol + '</a>' + separator;
a += style;
b += style;
c += style;
d += style;
}
for (var i = 1; i <= lastPage; i++) {
if (i == currentPage) {
a += '<a href="' + anchorLink + '" class="inactive" title="Page ' + i + '">' + i + '</a>' + separator;
b += '<a href="' + anchorLink + '" class="inactive" title="Page ' + i + '">' + i + '/' + lastPage + '</a>' + separator;
c += '<a href="' + anchorLink + '" class="inactive" title="Page ' + i + '">' + i + '</a>' + separator;
f += '<option value="' + i + '" selected="selected">' + i + '</option>'
} else {
style = '<a href="' + anchorLink + '" class="active" onclick="appendContent(' + i + ');" title="Page ' + i + '">' + i + '</a>' + separator;
a += style;
c += style;
f += '<option value="' + i + '">' + i + '</option>'
}
}
f += '';
if (currentPage == lastPage) {
style = '<a href="' + anchorLink + '" class="inactive" title="Next Page">' + nextPageSymbol + '</a>';
a += style;
b += style;
c += style;
d += style;
style = separator + '<a href="' + anchorLink + '" class="inactive" title="Last Page">' + lastPageSymbol + '</a>';
a += style;
b += style
} else {
style = '<a href="' + anchorLink + '" class="active" onclick="appendContent(-2);" title="Next Page">' + nextPageSymbol + '</a>';
a += style;
b += style;
c += style;
d += style;
style = separator + '<a href="' + anchorLink + '" class="active" onclick="appendContent(' + lastPage + ');" title="Last Page">' + lastPageSymbol + '</a>';
a += style;
b += style
}
switch (paginatorStyle) {
case 1:
style = a;
break;
case 2:
style = b;
break;
case 3:
style = c;
break;
case 4:
style = d;
break;
default:
style = a;
}
if (enablePageOfOption) style += '<span class="inactive" title="Page Information">' + e + '</span>';
if (enableGoToPage) style += '<span class="inactive" title="Select Page">' + f + '</span>';
if (enableSelectNoItems) style += '<span class="inactive" title="Select no. of items per page">' + g + '</span>';
jQuery(".paginator").html(style);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment