锘?* kkpager V1.3 https://github.com/pgkk/kkpager Copyright (c) 2013 cqzhangkang@163.com Licensed under the GNU GENERAL PUBLIC LICENSE */ var kkpager = { pagerid : 'kkpager', //divID mode : 'link', //妯″紡(link 鎴栬€ click) pno : 1, //褰撳墠椤电爜 total : 1, //鎬婚〉鐮 totalRecords : 0, //鎬绘暟鎹潯鏁 isShowFirstPageBtn : false, //鏄惁鏄剧ず棣栭〉鎸夐挳 isShowLastPageBtn : true, //鏄惁鏄剧ず灏鹃〉鎸夐挳 isShowPrePageBtn : false, //鏄惁鏄剧ず涓婁竴椤垫寜閽 isShowNextPageBtn : false, //鏄惁鏄剧ず涓嬩竴椤垫寜閽 isShowTotalPage : false, //鏄惁鏄剧ず鎬婚〉鏁 isShowCurrPage : false,//鏄惁鏄剧ず褰撳墠椤 isShowTotalRecords : false, //鏄惁鏄剧ず鎬昏褰曟暟 isGoPage : false, //鏄惁鏄剧ず椤电爜璺宠浆杈撳叆妗 isWrapedPageBtns : true, //鏄惁鐢╯pan鍖呰9浣忛〉鐮佹寜閽 isWrapedInfoTextAndGoPageBtn : true, //鏄惁鐢╯pan鍖呰9浣忓垎椤典俊鎭拰璺宠浆鎸夐挳 hrefFormer : '', //閾炬帴鍓嶉儴 hrefLatter : '', //閾炬帴灏鹃儴 gopageWrapId : 'kkpager_gopage_wrap', gopageButtonId : 'kkpager_btn_go', gopageTextboxId : 'kkpager_btn_go_input', lang : { firstPageText : '棣栭〉', firstPageTipText : '棣栭〉', lastPageText : '灏鹃〉', lastPageTipText : '灏鹃〉', prePageText : '涓婁竴椤?, prePageTipText : '涓婁竴椤?, nextPageText : '涓嬩竴椤?, nextPageTipText : '涓嬩竴椤?, totalPageBeforeText : '鍏?, totalPageAfterText : '椤?, currPageBeforeText : '褰撳墠绗?, currPageAfterText : '椤?, totalInfoSplitStr : '/', totalRecordsBeforeText : '鍏?, totalRecordsAfterText : '鏉℃暟鎹?, gopageBeforeText : ' 杞埌', gopageButtonOkText : '纭畾', gopageAfterText : '椤?, buttonTipBeforeText : '绗?, buttonTipAfterText : '椤? }, //閾炬帴绠楁硶锛堝綋澶勪簬link妯″紡锛?鍙傛暟n涓洪〉鐮 getLink : function(n){ //杩欓噷鐨勭畻娉曢€傜敤浜庢瘮濡傦細 //hrefFormer=http://www.xx.com/news/20131212 //hrefLatter=.html //閭d箞棣栭〉锛堢1椤碉級灏辨槸http://www.xx.com/news/20131212.html //绗?椤靛氨鏄痟ttp://www.xx.com/news/20131212_2.html //绗琻椤靛氨鏄痟ttp://www.xx.com/news/20131212_n.html if(n == 1){ return this.hrefFormer + this.hrefLatter; } return this.hrefFormer + '_' + n + this.hrefLatter; }, //椤电爜鍗曞嚮浜嬩欢澶勭悊鍑芥暟锛堝綋澶勪簬mode妯″紡锛?鍙傛暟n涓洪〉鐮 click : function(n){ //杩欓噷鑷繁瀹炵幇 //杩欓噷鍙互鐢╰his鎴栬€卥kpager璁块棶kkpager瀵硅薄 return false; }, //鑾峰彇href鐨勫€硷紙褰撳浜巑ode妯″紡锛?鍙傛暟n涓洪〉鐮 getHref : function(n){ //榛樿杩斿洖'#' return '#'; }, //璺宠浆妗嗗緱鍒拌緭鍏ョ劍鐐规椂 focus_gopage : function (){ var btnGo = $('#'+this.gopageButtonId); $('#'+this.gopageTextboxId).attr('hideFocus',true); btnGo.show(); btnGo.css('left','10px'); $('#'+this.gopageTextboxId).addClass('focus'); btnGo.animate({left: '+=30'}, 50); }, //璺宠浆妗嗗け鍘昏緭鍏ョ劍鐐规椂 blur_gopage : function(){ var _this = this; setTimeout(function(){ var btnGo = $('#'+_this.gopageButtonId); btnGo.animate({ left: '-=25' }, 100, function(){ btnGo.hide(); $('#'+_this.gopageTextboxId).removeClass('focus'); }); },400); }, //璺宠浆杈撳叆妗嗘寜閿搷浣 keypress_gopage : function(){ var event = arguments[0] || window.event; var code = event.keyCode || event.charCode; //delete key if(code == 8) return true; //enter key if(code == 13){ kkpager.gopage(); return false; } //copy and paste if(event.ctrlKey && (code == 99 || code == 118)) return true; //only number key if(code<48 || code>57)return false; return true; }, //璺宠浆妗嗛〉闈㈣烦杞 gopage : function(){ var str_page = $('#'+this.gopageTextboxId).val(); if(isNaN(str_page)){ $('#'+this.gopageTextboxId).val(this.next); return; } var n = parseInt(str_page); if(n < 1) n = 1; if(n > this.total) n = this.total; if(this.mode == 'click'){ this._clickHandler(n); }else{ window.location = this.getLink(n); } }, //涓嶅埛鏂伴〉闈㈢洿鎺ユ墜鍔ㄨ皟鐢ㄩ€変腑鏌愪竴椤电爜 selectPage : function(n){ this._config['pno'] = n; this.generPageHtml(this._config,true); }, //鐢熸垚鎺т欢浠g爜 generPageHtml : function(config,enforceInit){ if(enforceInit || !this.inited){ this.init(config); } var str_first='',str_prv='',str_next='',str_last=''; if(this.isShowFirstPageBtn){ if(this.hasPrv){ str_first = ''+this.lang.firstPageText+''; }else{ str_first = ''+this.lang.firstPageText+''; } } if(this.isShowPrePageBtn){ if(this.hasPrv){ str_prv = ''+this.lang.prePageText+''; }else{ str_prv = ''+this.lang.prePageText+''; } } if(this.isShowNextPageBtn){ if(this.hasNext){ str_next = ''+this.lang.nextPageText+''; }else{ str_next = ''+this.lang.nextPageText+''; } } if(this.isShowLastPageBtn){ if(this.hasNext){ str_last = ''+this.lang.lastPageText+''; }else{ str_last = ''+this.lang.lastPageText+''; } } var str = ''; var dot = '...'; var total_info=''; var total_info_splitstr = ''+this.lang.totalInfoSplitStr+''; if(this.isShowCurrPage){ total_info += this.lang.currPageBeforeText + '' + this.pno + '' + this.lang.currPageAfterText; if(this.isShowTotalPage){ total_info += total_info_splitstr; total_info += this.lang.totalPageBeforeText + ''+this.total + '' + this.lang.totalPageAfterText; }else if(this.isShowTotalRecords){ total_info += total_info_splitstr; total_info += this.lang.totalRecordsBeforeText + ''+this.totalRecords + '' + this.lang.totalRecordsAfterText; } }else if(this.isShowTotalPage){ total_info += this.lang.totalPageBeforeText + ''+this.total + '' + this.lang.totalPageAfterText;; if(this.isShowTotalRecords){ total_info += total_info_splitstr; total_info += this.lang.totalRecordsBeforeText + ''+this.totalRecords + '' + this.lang.totalRecordsAfterText; } }else if(this.isShowTotalRecords){ total_info += this.lang.totalRecordsBeforeText + ''+this.totalRecords + '' + this.lang.totalRecordsAfterText; } total_info += ''; var gopage_info = ''; if(this.isGoPage){ gopage_info = ''+this.lang.gopageBeforeText+''+ ''+ ''+this.lang.gopageAfterText+''; } //鍒嗛〉澶勭悊 if(this.total <= 8){ for(var i=1;i<=this.total;i++){ if(this.pno == i){ str += ''+i+''; }else{ str += ''+i+''; } } }else{ if(this.pno <= 5){ for(var i=1;i<=7;i++){ if(this.pno == i){ str += ''+i+''; }else{ str += ''+i+''; } } str += dot; }else{ str += '1'; str += '2'; str += dot; var begin = this.pno - 2; var end = this.pno + 2; if(end > this.total){ end = this.total; begin = end - 4; if(this.pno - begin < 2){ begin = begin-1; } }else if(end + 1 == this.total){ end = this.total; } for(var i=begin;i<=end;i++){ if(this.pno == i){ str += ''+i+''; }else{ str += ''+i+''; } } if(end != this.total){ str += dot; } } } var pagerHtml = '
'; if(this.isWrapedPageBtns){ pagerHtml += '' + str_first + str_prv + str + str_next + str_last + '' }else{ pagerHtml += str_first + str_prv + str + str_next + str_last; } if(this.isWrapedInfoTextAndGoPageBtn){ pagerHtml += '' + total_info + gopage_info + ''; }else{ pagerHtml += total_info + gopage_info; } pagerHtml += '
'; $("#"+this.pagerid).html(pagerHtml); }, //鍒嗛〉鎸夐挳鎺т欢鍒濆鍖 init : function(config){ this.pno = isNaN(config.pno) ? 1 : parseInt(config.pno); this.total = isNaN(config.total) ? 1 : parseInt(config.total); this.totalRecords = isNaN(config.totalRecords) ? 0 : parseInt(config.totalRecords); if(config.pagerid){this.pagerid = config.pagerid;} if(config.mode){this.mode = config.mode;} if(config.gopageWrapId){this.gopageWrapId = config.gopageWrapId;} if(config.gopageButtonId){this.gopageButtonId = config.gopageButtonId;} if(config.gopageTextboxId){this.gopageTextboxId = config.gopageTextboxId;} if(config.isShowFirstPageBtn != undefined){this.isShowFirstPageBtn=config.isShowFirstPageBtn;} if(config.isShowLastPageBtn != undefined){this.isShowLastPageBtn=config.isShowLastPageBtn;} if(config.isShowPrePageBtn != undefined){this.isShowPrePageBtn=config.isShowPrePageBtn;} if(config.isShowNextPageBtn != undefined){this.isShowNextPageBtn=config.isShowNextPageBtn;} if(config.isShowTotalPage != undefined){this.isShowTotalPage=config.isShowTotalPage;} if(config.isShowCurrPage != undefined){this.isShowCurrPage=config.isShowCurrPage;} if(config.isShowTotalRecords != undefined){this.isShowTotalRecords=config.isShowTotalRecords;} if(config.isWrapedPageBtns){this.isWrapedPageBtns=config.isWrapedPageBtns;} if(config.isWrapedInfoTextAndGoPageBtn){this.isWrapedInfoTextAndGoPageBtn=config.isWrapedInfoTextAndGoPageBtn;} if(config.isGoPage != undefined){this.isGoPage=config.isGoPage;} if(config.lang){ for(var key in config.lang){ this.lang[key] = config.lang[key]; } } this.hrefFormer = config.hrefFormer || ''; this.hrefLatter = config.hrefLatter || ''; if(config.getLink && typeof(config.getLink) == 'function'){this.getLink = config.getLink;} if(config.click && typeof(config.click) == 'function'){this.click = config.click;} if(config.getHref && typeof(config.getHref) == 'function'){this.getHref = config.getHref;} if(!this._config){ this._config = config; } //validate if(this.pno < 1) this.pno = 1; this.total = (this.total <= 1) ? 1: this.total; if(this.pno > this.total) this.pno = this.total; this.prv = (this.pno<=2) ? 1 : (this.pno-1); this.next = (this.pno >= this.total-1) ? this.total : (this.pno + 1); this.hasPrv = (this.pno > 1); this.hasNext = (this.pno < this.total); this.inited = true; }, _getHandlerStr : function(n){ if(this.mode == 'click'){ return 'href="'+this.getHref(n)+'" onclick="return kkpager._clickHandler('+n+')"'; } //link妯″紡锛屼篃鏄粯璁ょ殑 return 'href="'+this.getLink(n)+'"'; }, _clickHandler : function(n){ var res = false; if(this.click && typeof this.click == 'function'){ res = this.click.call(this,n) || false; } return res; } };