/* kkpager v1.3 https://github.com/pgkk/kkpager copyright (c) 2013 cqzhangkang@163.com licensed under the gnu general public license */ function kkpager() { this.pagerid = 'kkpager'; //divid this.mode = 'link'; //模式(link 或者 click) this.pno = 1; //当前页码 this.total = 1; //总页码 this.totalrecords = 0; //总数据条数 this.isshowfirstpagebtn = true; //是否显示首页按钮 this.isshowlastpagebtn = true; //是否显示尾页按钮 this.isshowprepagebtn = true; //是否显示上一页按钮 this.isshownextpagebtn = true; //是否显示下一页按钮 this.isshowtotalpage = true; //是否显示总页数 this.isshowcurrpage = true;//是否显示当前页 this.isshowtotalrecords = false; //是否显示总记录数 this.isgopage = true; //是否显示页码跳转输入框 this.iswrapedpagebtns = true; //是否用span包裹住页码按钮 this.iswrapedinfotextandgopagebtn = true; //是否用span包裹住分页信息和跳转按钮 this.hrefformer = ''; //链接前部 this.hreflatter = ''; //链接尾部 this.gopagewrapid = 'kkpager_gopage_wrap'; this.gopagebuttonid = 'kkpager_btn_go'; this.gopagetextboxid = 'kkpager_btn_go_input'; this.lang = { firstpagetext: '首页', firstpagetiptext: '首页', lastpagetext: '尾页', lastpagetiptext: '尾页', prepagetext: '上一页', prepagetiptext: '上一页', nextpagetext: '下一页', nextpagetiptext: '下一页', totalpagebeforetext: '共', totalpageaftertext: '页', currpagebeforetext: '当前第', currpageaftertext: '页', totalinfosplitstr: '/', totalrecordsbeforetext: '共', totalrecordsaftertext: '条数据', gopagebeforetext: ' 转到', gopagebuttonoktext: '确定', gopageaftertext: '页', buttontipbeforetext: '第', buttontipaftertext: '页' }; //链接算法(当处于link模式),参数n为页码 this.getlink = function (n) { //这里的算法适用于比如: //hrefformer=http://www.xx.com/news/20131212 //hreflatter=.html //那么首页(第1页)就是http://www.xx.com/news/20131212.html //第2页就是http://www.xx.com/news/20131212_2.html //第n页就是http://www.xx.com/news/20131212_n.html if (n == 1) { return this.hrefformer + this.hreflatter; } return this.hrefformer + '_' + n + this.hreflatter; }; //页码单击事件处理函数(当处于mode模式),参数n为页码 this.click = function (n) { //这里自己实现 //这里可以用this或者kkpager访问kkpager对象 return false; }; //获取href的值(当处于mode模式),参数n为页码 this.gethref = function (n) { //默认返回'#' return '#'; }; //跳转框得到输入焦点时 this.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); }; //跳转框失去输入焦点时 this.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); }; //跳转输入框按键操作 this.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; }; //跳转框页面跳转 this.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); } }; //下拉选择某一页码 this.selectpage = function (n) { }; this.setpage = function(n){ this._config['pno'] = n; this.generpagehtml(this._config,true); }; this.settotalpage = function(t){ this._config['total'] = t; }; this.settotalcount = function(t){ this._config['totalrecords'] = t; }; this.render = function(){ this.generpagehtml(this._config, true); }; //生成控件代码 this.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 = '