/*
|
* author:mengly
|
* date:2014-2-2,
|
* todo:目前的格式比较接近于bootstrap,需要以后修改
|
*
|
*/
|
(function($,window){
|
$.fn.bootpag = function(options){
|
|
var $owner = this,
|
settings = $.extend({
|
total: 0,
|
page: 1,
|
maxVisible: 10,
|
leaps: true,
|
href: 'javascript:void(0);',
|
hrefVariable: '{{number}}',
|
next: '>',
|
prev: '<',
|
callback:function(num){return false;}
|
},
|
$owner.settings || {},
|
options || {});
|
|
if(settings.total <= 0)
|
return this;
|
|
if(!settings.maxVisible){
|
settings.maxVisible = settings.total;
|
}
|
|
$owner.settings = settings;
|
|
function renderPage($bootpag, page){
|
$bootpag.empty();
|
$owner.trigger('page', page);
|
create($bootpag,page);
|
}
|
|
|
function href(c){
|
|
return settings.href.replace(settings.hrefVariable, c);
|
}
|
function getstart(total,max,page){
|
if(page>total)
|
return -1;
|
if(total<2 || max<=2||total<=max)
|
return 2;
|
var tmv=max-2;
|
var htmv=parseInt(tmv/2);
|
if((page-1)<htmv)
|
return 2;
|
if((total-page)<htmv)
|
return (total-tmv)>2?(total-tmv):2;
|
return page-htmv;
|
|
}
|
return this.each(function(){
|
var $bootpag, lp, me = $(this);
|
p=[];
|
if(settings.total<=1||settings.maxVisible<=1)
|
return;
|
var start=getstart(settings.total,settings.maxVisible,settings.page);
|
// if(start<=1)
|
// return;
|
if(start<0)
|
return;
|
if(start<2)
|
start=2;
|
var total=settings.total;
|
var maxVisible=settings.maxVisible;
|
var page=settings.page;
|
p.push('<a class="prev" rev="'+((page-1)>0?1:(page-1))+'" href="'+href(((page-1)>0?(page-2):0))+'">'+settings.prev+'</a>');
|
p.push('<a class="1" rev="1" href="'+href(0)+'">'+1+'</a>');
|
if(start-1>1)
|
p.push('...');
|
|
var i = start
|
for (; i <=Math.min(start+settings.maxVisible-3,settings.total-1); i++) {
|
p.push('<a class="1" rev="'+i+'" href="'+href(i-1)+'">'+i+'</a>');
|
};
|
if(total-i>0)
|
p.push('...');
|
p.push('<a class="1" rev="'+settings.total+'" href="'+href(settings.total-1)+'">'+settings.total+'</a>');
|
p.push('<a class="next" rev="'+((page+1)>total?total:(page+1))+'" href="'+href(((page+1)>total?(page):total-1))+'">'+settings.next+'</a>');
|
jQuery(p.join('')).appendTo(me);
|
me.find('a[rev="'+page+'"]').addClass('current');
|
me.on('click','a',function(){
|
var me1=$(this);
|
var callback=settings.callback;
|
callback(me1.attr('rev'));
|
});
|
});
|
}
|
})(jQuery,window);
|