* pagination 分頁插件
* @version 1.3
* @author xuhaiyang
* @調用方法
* $(selector).pagination();
*
* */
/**
* ========================================
* 知識點說明:
* a(表達式)&&b(表達式) :
* 計算表達式a(也可以是函數)的運算結果
* 如果爲true,執行表達式b(或函數),並返回b的結果
* 如果爲false,返回a的結果
*
* a(表達式)||b(表達式):
* 計算表達式a(也可以是函數)的運算結果,
* 如果爲false,執行表達式b(或函數),並返回b的結果
* 如果爲true,返回a的結果
*
* 轉換規則:
* 對象爲true,
* 非0數字爲true,0爲false
* 非空字符串爲true,空爲false
* 其他爲false
*
* ========================================
*/
;(function(factory){
if(typeof define === "function" && (define.amd || define.cmd)&& !jQuery){
//AMD或CMD
denfine(["jquery"],factory);
}else if(typeof module === 'object' && module.exports){
// Node/CommonJS
module.exports = function(root , jquery){
if ( jQuery === undefined ) {
if ( typeof window !== 'undefined' ) {
jQuery = require('jquery');
} else {
jQuery = require('jquery')(root);
}
}
factory(jQuery);
return jQuery;
};
}else{
//Browser globals
factory(jQuery);
}
}(function($){
//配置參數
var defaults = {
totalData:0, //數據總條數
showData:0, //每頁顯示的條數
pageCount : 9, //總頁數,默認爲9
current: 1, //當前第幾頁
prevCls : 'prev', //上一頁class
nextCls : 'next', //下一頁class
prevContent : '<', //上一頁內容
nextContent : '>', //下一頁內容
activeCls : 'active', //當前頁選中狀態
coping: false, //首頁和尾頁
isHide : false, //當前頁數爲0頁或者1頁時不顯示分頁
homePage : '', //首頁節點內容
endPage : '', //尾頁節點內容
keepShowPN : false, //是否一直顯示上一頁下一頁
count:3, //當前頁前後分頁個數
jump:false, //跳轉到指定頁數
jumpIptCls : 'jump-ipt',//跳轉文本框內容
jumpBtnCls :'jump-btn', //跳轉按鈕
jumpBtn : '跳轉', //跳轉按鈕文本
callBack:function(){} //回調方法
};
var Pagination = function(element,options){
//全局變量
var opts = options,//配置
current,//當前頁
$document = $(document),//容器
$obj = $(element);//容器
/**
* 設置總頁數
* @param 整形 page 頁碼
* @return opts.pageCount 總頁數配置
*/
this.setPageCount = function(page){
return opts.pageCount = page;
};
/**
* 獲取總頁數
* 如果配置了總條數和每頁顯示條數,將會自動計算總頁數並略過總頁數配置,
* 反之
* @return 整形 p 總頁數
*/
this.getPageCount = function(){
return opts.totalData || opts.showData ? Math.ceil(parseInt(opts.totalData) / opts.showData) : opts.pageCount;
};
/**
* 獲取當前頁
* @return 整形 current 當前第幾頁
*/
this.getCurrent = function(){
return current;
};
/**
* 填充數據
* @param 整形 index 頁碼
*/
this.filling = function(index){
var html = "";
current = index || opts.current;//當前頁碼
var pageCount = this.getPageCount();
if( opts.keepShowPN || current > 1 ){
html += '<a href="javascript:;" class="'+opts.prevCls+'">'+opts.prevContent+'</a>';
}else{
if(opts.keepShowPN == false){
$obj.find('.'+ opts.prevCls) && $obj.find('.' + opts.prevCls).remove();
}
}
if(current >= opts.count + 2 && current != 1 && pageCount != opts.count){
var home = opts.coping && opts.homePage ? opts.homePage : '1';
html += opts.coping ? '<a href="javascript:;" data-page="1">'+home+'</a><span>...</span>' : '';
}
var end = current + opts.count;
var start = '';
//修復到最後一頁時比第一頁少顯示兩頁
//修復到最後一頁時比第一頁少顯示兩頁
start = current === pageCount ? current - opts.count - 2 : current - opts.count;
((start > 1 && current < opts.count) || current == 1) && end++;
(current > pageCount - opts.count && current >= pageCount) && start++;
for (;start <= end; start++) {
if(start <= pageCount && start >= 1){
if(start != current){
html += '<a href="javascript:;" data-page="'+start+'">'+ start +'</a>';
}else{
html += '<span class="'+opts.activeCls+'">'+start+'</span>';
}
}
}
if(current + opts.count < pageCount && current >= 1 && pageCount > opts.count){
var end = opts.coping && opts.endPage ? opts.endPage : pageCount;
html += opts.coping ? '<span>...</span><a href="javascript:;" data-page="'+pageCount+'">'+end+'</a>' : '';
}
if(opts.keepShowPN || current < pageCount){//下一頁
html += '<a href="javascript:;" class="'+opts.nextCls+'">'+opts.nextContent+'</a>'
}else{
if(opts.keepShowPN == false){
$obj.find('.'+opts.nextCls) && $obj.find('.'+opts.nextCls).remove();
}
}
html += opts.jump ? '<input type="text" class="'+opts.jumpIptCls+'"><a href="javascript:;" class="'+opts.jumpBtnCls+'">'+opts.jumpBtn+'</a>' : '';
$obj.empty().html(html);
};
//綁定事件
this.eventBind = function(){
var that = this;
var pageCount = that.getPageCount();//總頁數
var index = 1;
$obj.off().on('click','a',function(){
if($(this).hasClass(opts.nextCls)){
if($obj.find('.'+opts.activeCls).text() >= pageCount){
$(this).addClass('disabled');
return false;
}else{
index = parseInt($obj.find('.'+opts.activeCls).text()) + 1;
}
}else if($(this).hasClass(opts.prevCls)){
if($obj.find('.'+opts.activeCls).text() <= 1){
$(this).addClass('disabled');
return false;
}else{
index = parseInt($obj.find('.'+opts.activeCls).text()) - 1;
}
}else if($(this).hasClass(opts.jumpBtnCls)){
if($obj.find('.'+opts.jumpIptCls).val() !== ''){
index = parseInt($obj.find('.'+opts.jumpIptCls).val());
}else{
return;
}
}else{
index = parseInt($(this).data('page'));
}
that.filling(index);
typeof opts.callback === 'function' && opts.callback(that);
});
//輸入跳轉的頁碼
$obj.on('input propertychange','.'+opts.jumpIptCls,function(){
var $this = $(this);
var val = $this.val();
var reg = /[^\d]/g;
if (reg.test(val)) {
$this.val(val.replace(reg, ''));
}
(parseInt(val) > pageCount) && $this.val(pageCount);
if(parseInt(val) === 0){//最小值爲1
$this.val(1);
}
});
//回車跳轉指定頁碼
$document.keydown(function(e){
if(e.keyCode == 13 && $obj.find('.'+opts.jumpIptCls).val()){
var index = parseInt($obj.find('.'+opts.jumpIptCls).val());
that.filling(index);
typeof opts.callback === 'function' && opts.callback(that);
}
});
};
//初始化
this.init = function(){
this.filling(opts.current);
this.eventBind();
if(opts.isHide && this.getPageCount() == '1' || this.getPageCount() == '0') $obj.hide();
};
this.init();
};
$.fn.pagination = function(parameter,callback){
if(typeof parameter == 'function'){//重載
callback = parameter;
parameter = {};
}else{
parameter = parameter || {};
callback = callback || function(){}
}
var options = $.extend({},defaults,parameter);
return this.each(function(){
var pagination = new Pagination(this,options);
callback(pagination);
});
};
}));