Jquery.Pagination分页插件的学习
来源:互联网 发布:如何对待网络语言 编辑:程序博客网 时间:2024/05/13 01:01
http://blog.csdn.net/xiaochengzi_2015/article/details/51762432
编程小白,文章中出现的谬误希望大神指点
工作接到一个任务,需要把现在pagination的样式修改成前端给的样式,当时心慌慌,幸好pagination的源码不太多,参考了一下网上的讲解代码,把学习过程中遇到的问题和对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<np?page_id:np-1); // Normalize page id to sane value
- appendopts = $.extend({text:page_id+1, classes:""}, appendopts||{});
- if(page_id == current_page){
- lnk = $("<a>" + appendopts.text + "</a>");
- }
- else
- {
- lnk = $("<a>" + appendopts.text + "</a>")
- .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<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='wy_1 y_z'></div>");
- //创建首页链接
- if(this.opts.first_text){
- fragment.append(this.createLink(0, 0, {text:this.opts.first_text, classes:"wy_fy"}));
- }
- // 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:"wy_fy"}));
- }
- //创建中间页码显示
- fragment.append("<span>" + (current_page+1) + "/"+np+"</span>");
- // 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("<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)
- // {
- // jQuery("<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
- 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:"wy_fy"}));
- }
- //创建尾页链接
- if(this.opts.end_text){
- fragment.append(this.createLink(np-1, np-1, {text:this.opts.end_text, classes:"wy_fy"}));
- }
- //创建共多少条
- fragment.append("<span>共" + this.maxentries + "条</span>");
- //创建每页多少条
- fragment.append("<span>每页" + this.opts.items_per_page +"条</span>");
- //创建跳转
- fragment.append("<span>跳转到<input type='text' value='' placeholder='' class='f_input'/>页</span>");
- if(this.opts.go_text){
- fragment.append(this.createLink(current_page, current_page, {text:this.opts.go_text, classes:"go"}));
- }
- $('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:1,//如果设置为1,显示首页与尾页,然而并没有什么效果,不知道为什么
- 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;
- var new_current_page = $(evt.target).data('page_id');
- <span style="white-space:pre"> </span>//这里就是点击跳转的代码,核心是获取到input框内用户输入的页数,进行简单判断后,将值作为当前页
- <span style="white-space:pre"> </span>//传递给selectPage方法,让它重新调用本页方法重新生成链接
- if($(evt.target).attr('class')=='go' && $('input[class=f_input]').val() !='' && !isNaN($('input[class=f_input]').val())){
- new_current_page = $('input[class=f_input]').val() - 1;
- new_current_page = Math.min(np-1,Math.max(0,new_current_page));
- }
- var 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;
- }
- });
- </pre><pre name="code" class="javascript"><span style="white-space:pre"> </span>这里的PrevPage、nextPage我不知道哪里来的,希望有人讲解,
- 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);
插件首先调用Initialize containers下的方法,构造链接,完成后将生成的内容放到指定ID的容器内,上配置:
- callback: pageselectCallback,//PageCallback() 为翻页调用次函数。
- items_per_page:parseInt($("#pages").attr('data-epage')), //每页最多显示的记录数
- num_display_entries:0,//可见页码数量
- current_page: parseInt($("#pages").attr('data-page')),
- num_edge_entries:0,//如果设置为1,显示首页与尾页
- //link_to:"#",//链接
- prev_text:"上一页",
- next_text:"下一页",
- first_text:"首页",
- end_text:"尾页",
- go_text:"GO",
- ellipse_text:"",//当页码之间的数组相差很远时,显示的内容
- prev_show_always:true,
- next_show_always:true,
- //renderer:"defaultRenderer",
- load_first_page:false,//插件初始化时被执行
- if(this.opts.first_text){
- fragment.append(this.createLink(0, 0, {text:this.opts.first_text, classes:"wy_fy"}));
- }
其中的0,0就代表首页,第一个0是页码的起始参数,第二个0是页码的结束参数,那尾页的参数是什么呢? 是Pagination的一个变量np,调用创建链接的函数,其中text就是要显示的内容,first_text就是我们在初始化参数里面自己定义的,classes就是当前连接的样式,可以修改成自己的。
0 0
- Jquery.Pagination分页插件的学习
- Jquery.Pagination分页插件的学习
- jQuery PAGINATION 分页插件学习笔记
- jQuery 分页插件 pagination
- 分页插件-jquery.pagination
- jquery的分页插件pagination的使用
- jquery.pagination.js分页插件的运用
- jquery easyui pagination分页插件的使用
- 好看的分页插件-JQUERY PAGINATION PLUGIN
- jquery.pagination.js分页插件的运用
- jquery分页插件pagination 的运用
- 一款不错的jQuery分页插件--pagination
- jquery分页插件pagination.js的使用
- jQuery分页插件(jQuery Pagination)
- jquery.pagination插件实现分页
- jQuery Pagination Ajax分页插件
- 分页插件jquery.pagination.js
- jquery分页插件pagination教程
- MySQL查询优化器--逻辑查询优化技术(四)--连接的消除
- 将Linux下python默认版本切换成替代版本
- MySQL查询优化器--逻辑查询优化技术(五)--外连接的消除(一)
- ubuntu16.04中将python3设置为默认
- MySQL查询优化器--逻辑查询优化技术(五)--外连接的消除(二)
- Jquery.Pagination分页插件的学习
- windows10下安装ubuntu16.04 双系统
- tensorflow学习笔记三:实例数据下载与读取
- PostgreSQL查询计划中的路径-BitmapHeapPath-辨析(二)
- 深度学习框架的评估与比较
- MySQL查询优化器--逻辑查询优化技术(六)--条件化简(表达式处理)
- Deep learning与Neural Network
- MySQL查询优化器--逻辑查询优化技术(七)--等价谓词重写
- ACM无穷常数的设置