(function($){
var methods = {
init: function(options) {
var o = $.extend({
items: 1,
itemsonpage: 1,
pages: 0,
displayedpages: 3,
edges: 2,
currentpage: 1,
hreftextprefix: '?page=',
hreftextsuffix: '',
prevtext: '上一页',
nexttext: '下一页',
ellipsetext: '…',
cssstyle: 'light-theme',
labelmap: [],
selectonclick: true,
onpageclick: function(pagenumber, event) {
// callback triggered when a page is clicked
// page number is given as an optional parameter
},
oninit: function() {
// callback triggered immediately after initialization
}
}, options || {});
var self = this;
o.pages = o.pages ? o.pages : math.ceil(o.items / o.itemsonpage) ? math.ceil(o.items / o.itemsonpage) : 1;
o.currentpage = o.currentpage - 1;
o.halfdisplayed = o.displayedpages / 2;
this.each(function() {
self.addclass(o.cssstyle + ' simple-pagination').data('pagination', o);
methods._draw.call(self);
});
o.oninit();
return this;
},
selectpage: function(page) {
methods._selectpage.call(this, page - 1);
return this;
},
prevpage: function() {
var o = this.data('pagination');
if (o.currentpage > 0) {
methods._selectpage.call(this, o.currentpage - 1);
}
return this;
},
nextpage: function() {
var o = this.data('pagination');
if (o.currentpage < o.pages - 1) {
methods._selectpage.call(this, o.currentpage + 1);
}
return this;
},
getpagescount: function() {
return this.data('pagination').pages;
},
getcurrentpage: function () {
return this.data('pagination').currentpage + 1;
},
destroy: function(){
this.empty();
return this;
},
drawpage: function (page) {
var o = this.data('pagination');
o.currentpage = page - 1;
this.data('pagination', o);
methods._draw.call(this);
return this;
},
redraw: function(){
methods._draw.call(this);
return this;
},
disable: function(){
var o = this.data('pagination');
o.disabled = true;
this.data('pagination', o);
methods._draw.call(this);
return this;
},
enable: function(){
var o = this.data('pagination');
o.disabled = false;
this.data('pagination', o);
methods._draw.call(this);
return this;
},
updateitems: function (newitems) {
var o = this.data('pagination');
o.items = newitems;
o.pages = methods._getpages(o);
this.data('pagination', o);
methods._draw.call(this);
},
updateitemsonpage: function (itemsonpage) {
var o = this.data('pagination');
o.itemsonpage = itemsonpage;
o.pages = methods._getpages(o);
this.data('pagination', o);
methods._selectpage.call(this, 0);
return this;
},
_draw: function() {
var o = this.data('pagination'),
interval = methods._getinterval(o),
i,
tagname;
methods.destroy.call(this);
tagname = (typeof this.prop === 'function') ? this.prop('tagname') : this.attr('tagname');
var $panel = tagname === 'ul' ? this : $('
').appendto(this);
$panel.prepend('总记录数:'+o.items+'');
// generate prev link
methods._appenditem.call(this, 0, {text: "首页", classes: 'first'});
if (o.prevtext) {
methods._appenditem.call(this, o.currentpage - 1, {text: o.prevtext, classes: 'prev'});
}
// generate start edges
if (interval.start > 0 && o.edges > 0) {
var end = math.min(o.edges, interval.start);
for (i = 0; i < end; i++) {
methods._appenditem.call(this, i);
}
if (o.edges < interval.start && (interval.start - o.edges != 1)) {
$panel.append('' + o.ellipsetext + '');
} else if (interval.start - o.edges == 1) {
methods._appenditem.call(this, o.edges);
}
}
// generate interval links
for (i = interval.start; i < interval.end; i++) {
methods._appenditem.call(this, i);
}
// generate end edges
if (interval.end < o.pages && o.edges > 0) {
if (o.pages - o.edges > interval.end && (o.pages - o.edges - interval.end != 1)) {
$panel.append('' + o.ellipsetext + '');
} else if (o.pages - o.edges - interval.end == 1) {
methods._appenditem.call(this, interval.end++);
}
var begin = math.max(o.pages - o.edges, interval.end);
for (i = begin; i < o.pages; i++) {
methods._appenditem.call(this, i);
}
}
// generate next link
if (o.nexttext) {
methods._appenditem.call(this, o.currentpage + 1, {text: o.nexttext, classes: 'next'});
}
//增加 末页
methods._appenditem.call(this, o.pages + 1, {text: "末页", classes: 'last'});
//增加跳转页面
/*var str_select="";
$panel.append('跳转:'+str_select+'页');*/
var o_select = $panel.find("select");
var self = this;
o_select.change(function(event){
return methods._selectpage.call(self, $(this).val()-1, event);
});
/////////////增加跳转页面/////////////////////
},
_getpages: function(o) {
var pages = math.ceil(o.items / o.itemsonpage);
return pages || 1;
},
_getinterval: function(o) {
return {
start: math.ceil(o.currentpage > o.halfdisplayed ? math.max(math.min(o.currentpage - o.halfdisplayed, (o.pages - o.displayedpages)), 0) : 0),
end: math.ceil(o.currentpage > o.halfdisplayed ? math.min(o.currentpage + o.halfdisplayed, o.pages) : math.min(o.displayedpages, o.pages))
};
},
_appenditem: function(pageindex, opts) {
var self = this, options, $link, o = self.data('pagination'), $linkwrapper = $(''), $ul = self.find('ul');
pageindex = pageindex < 0 ? 0 : (pageindex < o.pages ? pageindex : o.pages - 1);
options = {
text: pageindex + 1,
classes: ''
};
if (o.labelmap.length && o.labelmap[pageindex]) {
options.text = o.labelmap[pageindex];
}
options = $.extend(options, opts || {});
if (pageindex == o.currentpage || o.disabled) {
if (o.disabled) {
$linkwrapper.addclass('disabled');
} else {
//$linkwrapper.addclass('active');
}
$link = $('' + (options.text) + '');
} else {
$link = $('' + (options.text) + '');
$link.click(function(event){
return methods._selectpage.call(self, pageindex, event);
});
}
if (options.classes) {
$link.addclass(options.classes);
if(options.classes=="prev" || options.classes =="next" || options.classes=="first" || options.classes =="last")
{
$link.removeclass("currentpage");
}
}
$linkwrapper.append($link);
if ($ul.length) {
$ul.append($linkwrapper);
} else {
self.append($linkwrapper);
}
},
_selectpage: function(pageindex, event) {
var o = this.data('pagination');
o.currentpage = pageindex;
if (o.selectonclick) {
methods._draw.call(this);
}
return o.onpageclick(pageindex + 1, event);
}
};
$.fn.pagination = function(method) {
// method calling logic
if (methods[method] && method.charat(0) != '_') {
return methods[method].apply(this, array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error('method ' + method + ' does not exist on jquery.pagination');
}
};
})(jquery);