/*
* jquery pager plugin
* version 1.0 (12/22/2008)
* @requires jquery v1.2.6 or later
*
* example at: http://jonpauldavies.github.com/jquery/pager/pagerdemo.html
*
* copyright (c) 2008-2009 jon paul davies
* dual licensed under the mit and gpl licenses:
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
* read the related blog post and contact the author at http://www.j-dee.com/2008/12/22/jquery-pager-plugin/
*
* this version is far from perfect and doesn't manage it's own state, therefore contributions are more than welcome!
*
* usage: .pager({ pagenumber: 1, pagecount: 15, buttonclickcallback: pagerclicktest });
*
* where pagenumber is the visible page number
* pagecount is the total number of pages to display
* buttonclickcallback is the method to fire when a pager button is clicked.
*
* buttonclickcallback signiture is pagerclicktest = function(pageclickednumber)
* where pageclickednumber is the number of the page clicked in the control.
*
* the included pager.css file is a dependancy but can obviously tweaked to your wishes
* tested in ie6 ie7 firefox & safari. any browser strangeness, please report.
*/
(function ($) {
$.fn.pager = function (options) {
var opts = $.extend({}, $.fn.pager.defaults, options);
return this.each(function () {
// 空出的目标元素,然后渲染器提供的选项
$(this).empty().append(renderpager(parseint(options.pagenumber), parseint(options.pagecount), options.buttonclickcallback));
// specify correct cursor activity
});
};
//渲染和返回器提供的选项
function renderpager(pagenumber, pagecount, buttonclickcallback) {
// setup $pager to hold render
var $pager = $('
');
// add in the previous and next buttons
$pager.append(renderbutton('首页', pagenumber, pagecount, buttonclickcallback)).append(renderbutton('<', pagenumber, pagecount, buttonclickcallback));
// pager currently only handles 10 viewable pages ( could be easily parameterized, maybe in next version ) so handle edge cases
var startpoint = 1;
var endpoint = 10;
if (pagenumber > 5 && pagenumber < 95) {
startpoint = pagenumber - 4;
endpoint = pagenumber + 5;
if (endpoint > pagecount) {
startpoint = pagecount - 9;
endpoint = pagecount;
}
} else if (pagenumber >= 95 && pagenumber < 1000) {
startpoint = pagenumber - 3;
endpoint = pagenumber + 4;
if (endpoint > pagecount) {
startpoint = pagecount - 4;
endpoint = pagecount;
}
} else if (pagenumber >= 1000 && pagenumber < 10000) {
startpoint = pagenumber - 3;
endpoint = pagenumber + 3;
if (endpoint > pagecount) {
startpoint = pagecount - 3;
endpoint = pagecount;
}
}
else if (pagenumber >= 10000) {
startpoint = pagenumber - 2;
endpoint = pagenumber + 2;
if (endpoint > pagecount) {
startpoint = pagecount - 2;
endpoint = pagecount;
}
}
else {
if (endpoint > pagecount) {
endpoint = pagecount;
}
}
if (startpoint < 1) {
startpoint = 1;
}
// loop thru visible pages and render buttons
for (var page = startpoint; page <= endpoint; page++) {
var currentbutton = $('
' + (page) + '
');
page == pagenumber ? currentbutton.addclass('pgcurrent') : currentbutton.click(function () { buttonclickcallback(this.firstchild.data); });
currentbutton.appendto($pager);
}
// render in the next and last buttons before returning the whole rendered control back.
$pager.append(renderbutton('>', pagenumber, pagecount, buttonclickcallback)).append(renderbutton('尾页', pagenumber, pagecount, buttonclickcallback));
$pager.append("
');
pagego.click(function () {
var pageon = $("#jumpto").val();
//定义正则表达式部分
var reg = /^\d+$/;
var re = pageon.match(reg);
if (re == null) {
alert("跳转页数只能为数字!");
$("#jumpto").val(1);
}
else {
if (re < 1) {
re = 1;
}
else if (re > pagecount) {
re = pagecount;
}
buttonclickcallback(re);
}
});
pagego.appendto($pager);
return $pager;
}
// renders and returns a 'specialized' button, ie 'next', 'previous' etc. rather than a page number button
function renderbutton(buttonlabel, pagenumber, pagecount, buttonclickcallback) {
var $button = $('
' + buttonlabel + '
');
var destpage = 1;
// work out destination page for required button type
switch (buttonlabel) {
case "首页":
destpage = 1;
break;
case "<":
destpage = pagenumber - 1;
break;
case ">":
destpage = pagenumber + 1;
break;
case "尾页":
destpage = pagecount;
break;
}
// disable and 'grey' out buttons if not needed.
if (buttonlabel == "首页" || buttonlabel == "<") {
pagenumber <= 1 ? $button.addclass('pgempty') : $button.click(function () { buttonclickcallback(destpage); });
}
else {
pagenumber >= pagecount ? $button.addclass('pgempty') : $button.click(function () { buttonclickcallback(destpage); });
}
return $button;
}
// pager defaults. hardly worth bothering with in this case but used as placeholder for expansion in the next version
$.fn.pager.defaults = {
pagenumber: 1,
pagecount: 1
};
})(jquery);