/** * pagination.js 1.5.1 * a jquery plugin to provide simple yet fully customisable pagination. * @version 1.5.1 * @author mss * @url https://github.com/maxiaoxiang/jquery-plugins * * @调用方法 * $(selector).pagination(option, callback); * -此处callback是初始化调用,option里的callback是点击页码后调用 * * -- example -- * $(selector).pagination({ * ... // 配置参数 * callback: function(api) { * console.log('点击页码调用该回调'); //切换页码时执行一次回调 * } * }, function(){ * console.log('初始化'); //插件初始化时调用该方法,比如请求第一次接口来初始化分页配置 * }); */ ; (function(factory) { if (typeof define === "function" && (define.amd || define.cmd) && !jquery) { // amd或cmd define(["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: 9, //每页显示的条数 pagecount: 10, //总页数,默认为9 current: 1, //当前第几页 prevcls: 'prev', //上一页class nextcls: 'next', //下一页class prevcontent: '', //上一页内容 nextcontent: '', //下一页内容 activecls: 'active', //当前页选中状态 coping: false, //首页和尾页 ishide: false, //当前页数为0页或者1页时不显示分页 homepage: '', //首页节点内容 endpage: '', //尾页节点内容 keepshowpn: false, //是否一直显示上一页下一页 mode: 'unfixed', //分页模式,unfixed:不固定页码数量,fixed:固定页码数量 count: 4, //mode为unfixed时显示当前选中页前后页数,mode为fixed显示页码总数 jump: false, //跳转到指定页数 jumpiptcls: 'jump-ipt', //文本框内容 jumpbtncls: 'jump-btn', //跳转按钮 jumpbtn: '跳转', //跳转按钮文本 callback: function(api) {} //回调 }; var pagination = function(element, options) { //全局变量 var opts = options, //配置 current, //当前页 $document = $(document), $obj = $(element); //容器 /** * 设置总页数 * @param {int} page 页码 * @return opts.pagecount 总页数配置 */ this.setpagecount = function(page) { return opts.pagecount = page; }; /** * 获取总页数 * 如果配置了总条数和每页显示条数,将会自动计算总页数并略过总页数配置,反之 * @return {int} 总页数 */ this.getpagecount = function() { return opts.totaldata && opts.showdata ? math.ceil(parseint(opts.totaldata) / opts .showdata) : opts.pagecount; }; /** * 获取当前页 * @return {int} 当前页码 */ this.getcurrent = function() { return current; }; /** * 填充数据 * @param {int} 页码 */ //重写 tolocalestring date.prototype.tolocalestring = function() { return this.getfullyear() + "-" + (this.getmonth() + 1) + "-" + this.getdate(); }; this.filling = function(index) { if (index > opts.pagecount) { index = 1; } /* if (window.location.pathname == '/search/search.html') { search(index); } else { qbxw(index); } */ var url = "https://" + window.location.host; var current = index; var elementid = ""; if (window.location.pathname == '/search/search.html') { var word = geturlparam("word"); elementid = "info_tab_search"; url = url + "/api/directive/search?&count=10&showparamters=true&word=" + word + "&pageindex=" + current+"&orderfield=checkdate&ordertype=desc" } else { var categoryid = geturlparam("id"); elementid = "info_tab0"; url = url +"/api/directive/contentlist?&count=10&showparamters=true&categoryid=" + categoryid + "&containchild=true&pageindex=" + current +"&orderfield=checkdate&ordertype=desc"; } $.getjson(url,function(result) { var newsarr = result.page.list; var info_tab0 = document.getelementbyid(elementid); info_tab0.innerhtml = ""; var html = ""; for (var i = 0; i < newsarr.length; i++) { var title = newsarr[i].title; var description = newsarr[i].description; var url = newsarr[i].url; var cover = newsarr[i].cover; var checkdate = newsarr[i].checkdate; var unixtimestamp = new date(checkdate); checkdate = unixtimestamp.tolocalestring(); if (cover == null) { cover = ""; } var strtitle =title.substring(0,1); var arrstr = ["【", "「", "《", "("]; if(arrstr.indexof(strtitle) > -1 || ismobile() ){ html = html + "

" + title + "

" + checkdate + "

"; }else{ html = html + "

" + title + "

" + checkdate + "

"; } } info_tab0.innerhtml = html; public(index, current, result.page.totalpage); }); }; // function search(index) { // var current = index; // var word = geturlparam("word"); // var url = "https://" + window.location.host; // $.getjson(url + "/api/directive/search?&count=10&showparamters=true&word=" + word + // "&pageindex=" + current, // function(result) { // var newsarr = result.page.list; // var info_tab0 = document.getelementbyid("info_tab_search"); // info_tab0.innerhtml = ""; // var html = ""; // for (var i = 0; i < newsarr.length; i++) { // var title = newsarr[i].title; // var description = newsarr[i].description; // var url = newsarr[i].url; // var cover = newsarr[i].cover; // var publishdate = newsarr[i].publishdate; // var unixtimestamp = new date(publishdate); // publishdate = unixtimestamp.tolocalestring(); // if (cover == null) { // cover = ""; // } // html = html + "

" + // title + "

" + publishdate + "

"; // } // info_tab0.innerhtml = html; // public(index, current, result.page.totalpage); // }); // }; //function qbxw(index) { //}; function public(index, current, totalpage) { var html = ''; current = parseint(index) || parseint(opts.current); //当前页码 var pagecount = totalpage; //获取的总页数 opts.pagecount = totalpage; $("#hiddencount").val(totalpage); switch (opts.mode) { //配置模式 case 'fixed': //固定按钮模式 html += '' + opts .prevcontent + ''; if (opts.coping) { var home = opts.coping && opts.homepage ? opts.homepage : '1'; html += '' + home + ''; } var start = current > opts.count - 1 ? current + opts.count - 1 > pagecount ? current - (opts.count - (pagecount - current)) : current - 2 : 1; var end = current + opts.count - 1 > pagecount ? pagecount : start + opts .count; for (; start <= end; start++) { if (start != current) { html += '' + start + ''; } else { html += '' + start + ''; } } if (opts.coping) { var _end = opts.coping && opts.endpage ? opts.endpage : pagecount; html += '' + _end + ''; } html += '' + opts .nextcontent + ''; break; case 'unfixed': //不固定按钮模式 if (opts.keepshowpn || current > 1) { //上一页 html += '' + opts .prevcontent + ''; } 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 ? '' + home + '' : ''; } var start = (current - opts.count) <= 1 ? 1 : (current - opts.count); var end = (current + opts.count) >= pagecount ? pagecount : (current + opts .count); for (; start <= end; start++) { if (start <= pagecount && start >= 1) { if (start != current) { html += '' + start + ''; } else { html += '' + start + ''; } } } if (current + opts.count < pagecount && current >= 1 && pagecount > opts .count) { var end = opts.coping && opts.endpage ? opts.endpage : pagecount; html += opts.coping ? '' + end + '' : ''; } if (opts.keepshowpn || current < pagecount) { //下一页 html += '' + opts .nextcontent + ''; } else { if (opts.keepshowpn == false) { $obj.find('.' + opts.nextcls) && $obj.find('.' + opts.nextcls) .remove(); } } break; case 'easy': //简单模式 break; default: } html += opts.jump ? '

跳至

' + opts.jumpbtn + '' : ''; $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) $this.val(1); //最小值为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(); } else { $obj.show(); } }; 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); }); }; function geturlparam(name) { var reg = new regexp("(^|&)" + name + "=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if (r != null) { return decodeuricomponent(r[2]); } return ''; } }));