/**
* 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 + " " +
title + " " + checkdate + " " +
// title + " " + publishdate + "
跳至
页
' + 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 ''; } }));