php使用pagination.js,jquery.pagination.js分页插件的简单使用

舒博雅
2023-12-01

以下是v1.1的版本,v1.3的请点击下载:http://pan.baidu.com/s/1pKPbPs3 密码:31bo

v1.3原创url : http://www.jq22.com/jquery-info5697

Document

var pageIndex = 0;     //页面索引初始值

var pageSize = 15;     //每页显示数据条数

$(function () {       //$(document).ready(function(){}) 的简写,文档加载完自动执行

InitTable(0);    //Load事件,初始化首页数据,页面索引为0(第一页)

var pageCount = 1000;

//分页,PageCount是总记录数,这是必选参数,其它参数都是可选,

//可以在php中计算出总记录数,发到模板页,就可以根据该总数来控制显示123的数目,

//比如总共就5条数据,一页就可以,分页中的23就不必显示,因为显示的话点击也是空值。

$("#Pagination").pagination(pageCount, {

callback: PageCallback,  //PageCallback() 翻页调用此函数。

prev_text: "←上一页",

next_text: "下一页→",

items_per_page:pageSize,//每页显示数据条目数

num_edge_entries: 3,       //当分页非常多时,两侧首尾分页条目数为3条,不显示的部分用省略号代替

num_display_entries: 9,    //当分页非常多时,连续分页最多显示9条,不显示的部分用省略号代替

current_page: pageIndex,   //当前页索引

});

//翻页调用

function PageCallback(index,jq) {

InitTable(index);

console.log(index);//打印看一下当前页索引是否正确,主要就是依据索引在php中使用limit获取数据

}

//请求数据

function InitTable(pageIndex) {

$.ajax({

type: "POST",

dataType: "text",

url: 'url',      //提交到php页面请求数据

data: "pageIndex=" + (pageIndex+1) + "&pageSize=" + pageSize,

//提交两个参数:pageIndex(页面索引),pageSize(显示条数)

success: function(data) {

//在此处理php返回的数据   在php中需处理条数

if ($param['page']) {     $page=(($param['page']-1)*ITEMS_PER_PAGES).','.ITEMS_PER_PAGES; } else {        $page='0,'.ITEMS_PER_PAGES; }                          }

});

}

});

.pagination a{

padding:5px 10px;

border-radius:10px;

background-color:#ccc;

}

.current{

padding:5px 10px;

border-radius:10px;

background-color:red;

color:#fff;

}

.prev,.next{

background-color: #ccc!important;

color:#000;

}

其中的参数:

参数                描述     maxentries                 总条目数 必选参数,整数 items_per_page         每页显示的条目数 可选参数,默认是10 num_display_entries 连续分页主体部分显示的分页条目数 可选参数,默认是10 current_page        当前选中的页面 可选参数,默认是0,表示第1页 num_edge_entries     两侧显示的首尾分页的条目数 可选参数,默认是0 link_to                       分页的链接 字符串,可选参数,默认是"#" prev_text               “前一页”分页按钮上显示的文字 字符串参数,可选,默认是"Prev" next_text                “下一页”分页按钮上显示的文字 字符串参数,可选,默认是"Next" ellipse_text                省略的页数用什么文字表示 可选字符串参数,默认是"…" prev_show_always 是否显示“前一页”分页按钮 布尔型,可选参数,默认为true,即显示“前一页”按钮 next_show_always 是否显示“下一页”分页按钮 布尔型,可选参数,默认为true,即显示“下一页”按钮 callback                       回调函数当点击链接的时候此函数被调用,此函数接受两个参数,新一页的id和pagination容器(一个DOM元素)。如果回调函数返回false,则pagination事件停止执行

插件的源代码:

/**

* 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

" + appendopts.text + "");

}

else

{

lnk = $("

" + appendopts.text + "")

.attr('href', this.opts.link_to.replace(/__id__/,page_id));

}

if(appendopts.classes){ lnk.addClass(appendopts.classes); }

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

"); // Generate "Previous"-Link if(this.opts.prev_text && (current_page > 0 || this.opts.prev_show_always)){ fragment.append(this.createLink(current_page-1, current_page, {text:this.opts.prev_text, classes:"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) { jQuery(""+this.opts.ellipse_text+"").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) { jQuery(""+this.opts.ellipse_text+"").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 if(this.opts.next_text && (current_page < np-1 || this.opts.next_show_always)){ fragment.append(this.createLink(current_page+1, current_page, {text:this.opts.next_text, classes:"next"})); } $('a', fragment).click(eventHandler); return fragment; } }); // Extend jQuery $.fn.pagination = function(maxentries, opts){ // Initialize options with default values opts = jQuery.extend({ items_per_page:10, num_display_entries:11, current_page:0, num_edge_entries:0, link_to:"#", prev_text:"Prev", next_text:"Next", ellipse_text:"...", prev_show_always:true, next_show_always:true, renderer:"defaultRenderer", load_first_page:false, 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 = opts.current_page; 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.bind('setPage', {numPages:np}, function(evt, page_id) { if(page_id >= 0 && page_id < evt.data.numPages) { selectPage(page_id); return false; } }); containers.bind('prevPage', function(evt){ var current_page = $(this).data('current_page'); if (current_page > 0) { selectPage(current_page - 1); } return false; }); containers.bind('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; }); // When all initialisation is done, draw the links links = renderer.getLinks(current_page, paginationClickHandler); containers.empty(); links.appendTo(containers); // call callback function if(opts.load_first_page) { opts.callback(current_page, containers); } } // End of $.fn.pagination block })(jQuery);

 类似资料: