/**
* Created by Hope on 2014/12/28.
*/
(function ($) {
$.fn.extendPagination = function (options) {
var defaults = {
//pageId:'',
totalCount: '',
showPage: '10',
limit: '5',
callback: function () {
return false;
}
};
$.extend(defaults, options || {});
if (defaults.totalCount == '') {
//alert('总数不能为空!');
$(this).empty();
return false;
} else if (Number(defaults.totalCount) <= 0) {
//alert('总数要大于0!');
$(this).empty();
return false;
}
if (defaults.showPage == '') {
defaults.showPage = '10';
} else if (Number(defaults.showPage) <= 0)defaults.showPage = '10';
if (defaults.limit == '') {
defaults.limit = '5';
} else if (Number(defaults.limit) <= 0)defaults.limit = '5';
var totalCount = Number(defaults.totalCount), showPage = Number(defaults.showPage),
limit = Number(defaults.limit), totalPage = Math.ceil(totalCount / limit);
if (totalPage > 0) {
var html = [];
html.push('
');
$(this).html(html.join(''));
// if (totalPage > showPage) $(this).find('ul.pagination li.next').prev().removeClass('hidden');
var pageObj = $(this).find('ul.pagination'), preObj = pageObj.find('li.previous'),
currentObj = pageObj.find('li').not('.previous,.disabled,.next'),
nextObj = pageObj.find('li.next');
function loopPageElement(minPage, maxPage) {
var tempObj = preObj.next();
for (var i = minPage; i <= maxPage; i++) {
if (minPage == 1 && (preObj.next().attr('class').indexOf('hidden')) < 0 && (preObj.next().attr('class').indexOf('disabled')) < 0)
preObj.next().addClass('hidden');
else if (minPage > 1 && (preObj.next().attr('class').indexOf('hidden')) > 0 && (preObj.next().attr('class').indexOf('disabled')) < 0)
preObj.next().removeClass('hidden');
if (maxPage == totalPage && (nextObj.prev().attr('class').indexOf('hidden')) < 0 && (preObj.next().attr('class').indexOf('disabled')) < 0)
nextObj.prev().addClass('hidden');
else if (maxPage < totalPage && (nextObj.prev().attr('class').indexOf('hidden')) > 0 && (preObj.next().attr('class').indexOf('disabled')) < 0)
nextObj.prev().removeClass('hidden');
var obj = tempObj.next().find('a');
if (!isNaN(obj.html()))obj.html(i);
tempObj = tempObj.next();
}
}
function callBack(curr) {
defaults.callback(curr, limit, totalCount);
}
currentObj.click(function (event) {
event.preventDefault();
var currPage = Number($(this).find('a').html()), activeObj = pageObj.find('li[class="active"]'),
activePage = Number(activeObj.find('a').html());
$("#pageNum").val(currPage);
if (currPage == activePage) return false;
if (totalPage > showPage) {
var maxPage = currPage, minPage = 1;
if (($(this).prev().attr('class'))
&& ($(this).prev().attr('class').indexOf('disabled')) >= 0) {
if(currPage >1)
minPage = currPage - 1;
maxPage = minPage + showPage - 1;
loopPageElement(minPage, maxPage);
} else if (($(this).next().attr('class'))
&& ($(this).next().attr('class').indexOf('disabled')) >= 0) {
if (totalPage - currPage >= 1) maxPage = currPage + 1;
else maxPage = totalPage;
if (maxPage - showPage > 0) minPage = (maxPage - showPage) + 1;
loopPageElement(minPage, maxPage)
}
}
activeObj.removeClass('active');
$.each(currentObj, function (index, thiz) {
if ($(thiz).find('a').html() == currPage) {
$(thiz).addClass('active');
callBack(currPage);
}
});
$("#YD-wrap").mCustomScrollbar("scrollTo","top");
});
preObj.click(function (event) {
event.preventDefault();
var activeObj = pageObj.find('li[class="active"]'), activePage = Number(activeObj.find('a').html());
if (activePage <= 1) return false;
if (totalPage > showPage) {
var maxPage = activePage, minPage = 1;
if ((activeObj.prev().prev().attr('class'))
&& (activeObj.prev().prev().attr('class').indexOf('disabled')) >= 0) {
minPage = activePage - 1;
if (minPage > 1) minPage = minPage - 1;
maxPage = minPage + showPage - 1;
loopPageElement(minPage, maxPage);
}
}
$("#pageNum").val(activePage - 1);
$.each(currentObj, function (index, thiz) {
if ($(thiz).find('a').html() == (activePage - 1)) {
activeObj.removeClass('active');
$(thiz).addClass('active');
callBack(activePage - 1);
}
});
});
nextObj.click(function (event) {
event.preventDefault();
var activeObj = pageObj.find('li[class="active"]'), activePage = Number(activeObj.find('a').html());
if (activePage >= totalPage) return false;
if (totalPage > showPage) {
var maxPage = activePage, minPage = 1;
if ((activeObj.next().next().attr('class'))
&& (activeObj.next().next().attr('class').indexOf('disabled')) >= 0) {
maxPage = activePage + 2;
if (maxPage > totalPage) maxPage = totalPage;
minPage = maxPage - showPage + 1;
loopPageElement(minPage, maxPage);
}
}
$("#pageNum").val(activePage + 1);
$.each(currentObj, function (index, thiz) {
if ($(thiz).find('a').html() == (activePage + 1)) {
activeObj.removeClass('active');
$(thiz).addClass('active');
callBack(activePage + 1);
}
});
});
}
};
})(jQuery);