Javascript jQuery分页插件-Can';t单击";“下一步”;在第一页
这与以下jQuery分页代码有关: 已经对原始代码进行了一些定制,包括所做的更改 以下是现在更新的代码:Javascript jQuery分页插件-Can';t单击";“下一步”;在第一页,javascript,jquery,pagination,Javascript,Jquery,Pagination,这与以下jQuery分页代码有关: 已经对原始代码进行了一些定制,包括所做的更改 以下是现在更新的代码: /** *这个jQuery插件在所选元素中显示分页链接。 * *此插件至少需要jQuery 1.4.2 * *@作者加布里埃尔·伯克(伯克*at*d-scribe*dot*de) *@version 2.2 *@param{int}maxentries要分页的条目数 *@param{Object}选择几个选项(有关文档,请参阅自述) *@return{Object}jQuery对象 */
/**
*这个jQuery插件在所选元素中显示分页链接。
*
*此插件至少需要jQuery 1.4.2
*
*@作者加布里埃尔·伯克(伯克*at*d-scribe*dot*de)
*@version 2.2
*@param{int}maxentries要分页的条目数
*@param{Object}选择几个选项(有关文档,请参阅自述)
*@return{Object}jQuery对象
*/
(函数($){
/**
*@class用于计算分页值
*/
$.PaginationCalculator=函数(maxentries,opts){
this.maxentries=maxentries;
this.opts=opts;
};
$.extend($.PaginationCalculator.prototype{
/**
*计算最大页数
*@方法
*@返回{Number}
*/
numPages:function(){
返回Math.ceil(每页this.maxentries/this.opts.items);
},
/**
*计算分页链接的起点和终点,具体取决于
*当前页面和数值显示项。
*@returns{Array}
*/
getInterval:函数(当前页面){
var ne_half=Math.floor(this.opts.num_display_entries/2);
var np=this.numPages();
var upper\u limit=np-this.opts.num\u display\u条目;
var start=current_page>ne_half?Math.max(Math.min(current_page-ne_half,上限),0:0;
var end=current_page>ne_half?Math.min(当前_page+ne_half+(this.opts.num_display_entries%2),np):Math.min(this.opts.num_display_entries,np);
返回{start:start,end:end};
}
});
//初始化分页呈现器的jQuery对象容器
$.PaginationRenders={};
/**
*@class用于呈现分页链接的默认呈现器
*/
$.PaginationRenders.defaultRenderer=函数(maxentries,opts){
this.maxentries=maxentries;
this.opts=opts;
this.pc=new$.PaginationCalculator(maxentries,opts);
};
$.extend($.PaginationRenders.defaultRenderer.prototype{
/**
*用于生成单个链接的帮助函数(如果是当前页面,则为span标记)
*@param{Number}page_id新项目的页面id
*@param{Number}当前页面
*@param{Object}appends为新项选择选项:文本和类
*@returns{jQuery}包含链接的jQuery对象
*/
createLink:函数(页面id、当前页面、附录选项){
var lnk,np=this.pc.numPages();
page\u id=page\u id 0&&this.opts.num\u edge\u entries>0)
{
end=Math.min(this.opts.num\u edge\u条目,interval.start);
appendRange(片段,当前_页,0,end,{classes:'sp'});
if(this.opts.num\u edge\u entries0)
{
if(np-this.opts.num\u edge\u entries>interval.end&&this.opts.eliple\u text)
{
$(“”+this.opts.ellipse_text+“”)appendTo(fragment);
}
begin=Math.max(np-this.opts.num\u edge\u条目,interval.end);
appendRange(片段,当前_页,begin,np,{classes:'ep'});
}
//生成“下一步”-链接
currentNextPage=Math.ceil(当前页面/7)*7;
append(this.createLink(currentNextPage,current_page,{text:this.opts.next_text,class:“next”,rel:“next”}));
$('a',片段)。单击(eventHandler);
返回片段;
}
});
//扩展jQuery
$.fn.pagination=函数(maxentries,opts){
opts=$.extend({
每页项目:7,
显示条目数:35,
当前页面:1,
数值\u边\u项:0,
链接到:“#”,
上一个文本:“”,
椭圆_文本:“…”,
上一个节目总是:真的,
下一个节目总是:真的,
渲染器:“defaultRenderer”,
如果显示单个页面,则显示:true,
加载第一页:真,
回调:函数(){return false;}
},选择|{});
var containers=这个,
渲染器、链接、当前页面;
/**
*这是分页链接的事件处理函数。
*@param{int}page_id新页码
*/
函数分页ClickHandler(evt){
var链接,
新建当前页面=$(evt.target).data('page\u id'),
continuePropagation=选择页面(新建页面和当前页面);
如果(!继续复制){
evt.stopPropagation();
}
返回继续复制;
}
/**
*这是用于内部事件处理程序的实用程序函数。
*它在分页容器对象上设置新的当前页面,
*为分页链接和调用生成新的HTMl片段
*回调函数。
*/
功能选择页面(新建当前页面){
//更新所有容器的链接显示
容器数据(“当前页面”,新的当前页面);
links=renderer.getLinks(新建当前页面,分页ClickHandler);
容器。空();
链接。附加到(容器);
//调用回调并传播事件,如果
/**
* This jQuery plugin displays pagination links inside the selected elements.
*
* This plugin needs at least jQuery 1.4.2
*
* @author Gabriel Birke (birke *at* d-scribe *dot* de)
* @version 2.2
* @param {int} maxentries Number of entries to paginate
* @param {Object} opts Several options (see README for documentation)
* @return {Object} jQuery Object
*/
(function($){
/**
* @class Class for calculating pagination values
*/
$.PaginationCalculator = function(maxentries, opts) {
this.maxentries = maxentries;
this.opts = opts;
};
$.extend($.PaginationCalculator.prototype, {
/**
* Calculate the maximum number of pages
* @method
* @returns {Number}
*/
numPages:function() {
return Math.ceil(this.maxentries/this.opts.items_per_page);
},
/**
* Calculate start and end point of pagination links depending on
* current_page and num_display_entries.
* @returns {Array}
*/
getInterval:function(current_page) {
var ne_half = Math.floor(this.opts.num_display_entries/2);
var np = this.numPages();
var upper_limit = np - this.opts.num_display_entries;
var start = current_page > ne_half ? Math.max( Math.min(current_page - ne_half, upper_limit), 0 ) : 0;
var end = current_page > ne_half?Math.min(current_page+ne_half + (this.opts.num_display_entries % 2), np):Math.min(this.opts.num_display_entries, np);
return {start:start, end:end};
}
});
// Initialize jQuery object container for pagination renderers
$.PaginationRenderers = {};
/**
* @class Default renderer for rendering pagination links
*/
$.PaginationRenderers.defaultRenderer = function(maxentries, opts) {
this.maxentries = maxentries;
this.opts = opts;
this.pc = new $.PaginationCalculator(maxentries, opts);
};
$.extend($.PaginationRenderers.defaultRenderer.prototype, {
/**
* Helper function for generating a single link (or a span tag if it's the current page)
* @param {Number} page_id The page id for the new item
* @param {Number} current_page
* @param {Object} appendopts Options for the new item: text and classes
* @returns {jQuery} jQuery object containing the link
*/
createLink:function(page_id, current_page, appendopts){
var lnk, np = this.pc.numPages();
page_id = page_id<0?0:(page_id<np?page_id:np-1); // Normalize page id to sane value
appendopts = $.extend({text:page_id+1, classes:""}, appendopts||{});
var weekamount;
weekamount = Math.ceil(appendopts.text / 7) * 7;
weekdaysamount = weekamount - 7;
if(appendopts.text > 7) {
appendopts.text = appendopts.text - weekdaysamount;
}
if(page_id == current_page){
lnk = $("<span class='current'>" + appendopts.text + "</span>");
}
else
{
lnk = $("<a>" + appendopts.text + "</a>")
.attr('href', this.opts.link_to.replace(/__id__/,page_id));
}
if(appendopts.classes){ lnk.addClass(appendopts.classes); }
if(appendopts.rel){ lnk.attr('rel', appendopts.rel); }
lnk.addClass('page' + page_id);
lnk.data('page_id', page_id);
return lnk;
},
// Generate a range of numeric links
appendRange:function(container, current_page, start, end, opts) {
var i;
for(i=start; i<end; i++) {
this.createLink(i, current_page, opts).appendTo(container);
}
},
getLinks:function(current_page, eventHandler) {
var begin, end,
interval = this.pc.getInterval(current_page),
np = this.pc.numPages(),
fragment = $("<div class='pagination'></div>");
// Generate "Previous"-Link
currentPrevPageCalc = Math.ceil(current_page / 7) * 7;
currentPrevPage = currentPrevPageCalc - 14;
fragment.append(this.createLink(currentPrevPage, current_page, {text:this.opts.prev_text, classes:"prev",rel:"prev"}));
// Generate starting points
if (interval.start > 0 && this.opts.num_edge_entries > 0)
{
end = Math.min(this.opts.num_edge_entries, interval.start);
this.appendRange(fragment, current_page, 0, end, {classes:'sp'});
if(this.opts.num_edge_entries < interval.start && this.opts.ellipse_text)
{
$("<span>"+this.opts.ellipse_text+"</span>").appendTo(fragment);
}
}
// Generate interval links
this.appendRange(fragment, current_page, interval.start, interval.end);
// Generate ending points
if (interval.end < np && this.opts.num_edge_entries > 0)
{
if(np-this.opts.num_edge_entries > interval.end && this.opts.ellipse_text)
{
$("<span>"+this.opts.ellipse_text+"</span>").appendTo(fragment);
}
begin = Math.max(np-this.opts.num_edge_entries, interval.end);
this.appendRange(fragment, current_page, begin, np, {classes:'ep'});
}
// Generate "Next"-Link
currentNextPage = Math.ceil(current_page / 7) * 7;
fragment.append(this.createLink(currentNextPage, current_page, {text:this.opts.next_text, classes:"next",rel:"next"}));
$('a', fragment).click(eventHandler);
return fragment;
}
});
// Extend jQuery
$.fn.pagination = function(maxentries, opts){
opts = $.extend({
items_per_page:7,
num_display_entries:35,
current_page:1,
num_edge_entries:0,
link_to:"#",
prev_text:"<<",
next_text:">>",
ellipse_text:"...",
prev_show_always:true,
next_show_always:true,
renderer:"defaultRenderer",
show_if_single_page:true,
load_first_page:true,
callback:function(){return false;}
},opts||{});
var containers = this,
renderer, links, current_page;
/**
* This is the event handling function for the pagination links.
* @param {int} page_id The new page number
*/
function paginationClickHandler(evt){
var links,
new_current_page = $(evt.target).data('page_id'),
continuePropagation = selectPage(new_current_page);
if (!continuePropagation) {
evt.stopPropagation();
}
return continuePropagation;
}
/**
* This is a utility function for the internal event handlers.
* It sets the new current page on the pagination container objects,
* generates a new HTMl fragment for the pagination links and calls
* the callback function.
*/
function selectPage(new_current_page) {
// update the link display of a all containers
containers.data('current_page', new_current_page);
links = renderer.getLinks(new_current_page, paginationClickHandler);
containers.empty();
links.appendTo(containers);
// call the callback and propagate the event if it does not return false
var continuePropagation = opts.callback(new_current_page, containers);
return continuePropagation;
}
// -----------------------------------
// Initialize containers
// -----------------------------------
current_page = parseInt(opts.current_page, 10);
containers.data('current_page', current_page);
// Create a sane value for maxentries and items_per_page
maxentries = (!maxentries || maxentries < 0)?1:maxentries;
opts.items_per_page = (!opts.items_per_page || opts.items_per_page < 0)?1:opts.items_per_page;
if(!$.PaginationRenderers[opts.renderer])
{
throw new ReferenceError("Pagination renderer '" + opts.renderer + "' was not found in jQuery.PaginationRenderers object.");
}
renderer = new $.PaginationRenderers[opts.renderer](maxentries, opts);
// Attach control events to the DOM elements
var pc = new $.PaginationCalculator(maxentries, opts);
var np = pc.numPages();
containers.off('setPage').on('setPage', {numPages:np}, function(evt, page_id) {
if(page_id >= 0 && page_id < evt.data.numPages) {
selectPage(page_id);
}
});
containers.off('prevPage').on('prevPage', function(evt){
var current_page = $(this).data('current_page');
if (current_page > 0) {
selectPage(current_page - 1);
}
return false;
});
containers.off('nextPage').on('nextPage', {numPages:np}, function(evt){
var current_page = $(this).data('current_page');
if(current_page < evt.data.numPages - 1) {
selectPage(current_page + 1);
}
return false;
});
containers.off('currentPage').on('currentPage', function(){
var current_page = $(this).data('current_page');
selectPage(current_page);
return false;
});
// When all initialisation is done, draw the links
links = renderer.getLinks(current_page, paginationClickHandler);
containers.empty();
if(np > 1 || opts.show_if_single_page) {
links.appendTo(containers);
}
// call callback function
if(opts.load_first_page) {
opts.callback(current_page, containers);
}
}; // End of $.fn.pagination block
})(jQuery);
function handlePaginationClick(new_page_index, pagination_container) {
console.log(new_page_index);
}
$.PaginationCalculator.prototype.getInterval = function (current_page) {
var num_display_entries = this.opts.num_display_entries;
var start = Math.floor(current_page / num_display_entries) * num_display_entries;
var end = Math.min(start + num_display_entries, this.numPages());
return { start: start, end: end };
};
$("#News-Pagination").pagination(150, {
items_per_page:7,
num_display_entries: 7,
callback:handlePaginationClick
});
$.PaginationRenderers.defaultRenderer.prototype.createLink = function(page_id, current_page, appendopts) {
var lnk, np = this.pc.numPages();
page_id = page_id<0?0:(page_id<np?page_id:np-1); // Normalize page id to sane value
appendopts = $.extend({
//text: page_id+1,
// Note: This is the only line of code I have changed from the original.
// It ensures that the link text is only ever within a range of 1 - num_display_entries
text: (page_id % this.opts.num_display_entries) + 1,
classes: ""
}, appendopts||{});
if(page_id == current_page){
lnk = $("<span class='current'>" + appendopts.text + "</span>");
}
else
{
lnk = $("<a>" + appendopts.text + "</a>")
.attr('href', this.opts.link_to.replace(/__id__/,page_id));
}
if(appendopts.classes){ lnk.addClass(appendopts.classes); }
if(appendopts.rel){ lnk.attr('rel', appendopts.rel); }
lnk.data('page_id', page_id);
return lnk;
};