jquery.pagination.js添加跳转页

原作者github地址:https://github.com/gbirke/jquery_pagination

在这基础上加入了跳转到指定页。

修改后的jquery.pagination.js

/**
 * 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 = $("<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;
        },
        // 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
            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",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
            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",rel:"next"}));
            }
            // Generate "Jump"-Link
            if(this.opts.jump_text && this.opts.jump_show_always){
                var lnk = $("<a href=‘" + this.opts.link_to + "‘ class=‘jump‘ rel=‘jump‘>" + this.opts.jump_text + "</a>");
                fragment.append("<input class=‘jump_page‘ value=‘" + (current_page + 1) + "‘ />").append(lnk);
            }
            $(‘a‘, fragment).click(eventHandler);
            return fragment;
        }
    });

    // Extend jQuery
    $.fn.pagination = function(maxentries, opts){

        // Initialize options with default values
        opts = $.extend({
            items_per_page:10,
            num_display_entries:11,
            current_page:0,
            num_edge_entries:0,
            link_to:"#",
            prev_text:"Prev",
            next_text:"Next",
            jump_text:"Jump",
            ellipse_text:"...",
            prev_show_always:true,
            next_show_always:true,
            jump_show_always:true,
            renderer:"defaultRenderer",
            show_if_single_page:false,
            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‘);
            if(new_current_page == undefined){
                new_current_page = parseInt($(evt.target).siblings(".jump_page").val());
                new_current_page = new_current_page > 0 ? new_current_page - 1 : 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 = 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); return false;
                }
        });
        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;
        });

        containers.off(‘jumpPage‘).on(‘jumpPage‘, function(){
                var current_page = $(this).data(‘jump_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);

样式pagination.css

.pagination {
            font-size: 80%;
        }

.pagination a {
    text-decoration: none;
    border: solid 1px #AAE;
    color: #15B;
}

.pagination a, .pagination span, .pagination input {
    display: block;
    float: left;
    padding: 0.3em 0.5em;
    margin-right: 5px;
    margin-bottom: 5px;
    min-width:1em;
    text-align:center;
}

.pagination .current {
    background: #26B;
    color: #fff;
    border: solid 1px #AAE;
}

.pagination .current.prev, .pagination .current.next , .pagination .current.jump{
    color:#999;
    border-color:#999;
    background:#fff;
}
.pagination .jump_page{
    width:25px;
    height:17px;
    border: solid 1px #AAE;
}    

展示:

时间: 2024-10-05 05:02:16

jquery.pagination.js添加跳转页的相关文章

jQuery 分页插件(jQuery.pagination.js)ajax 实现分页

首先需要引入jQuery 再次需要引入 <script src="jquery/jquery.pagination.js"></script> 同时也要引入 <link rel="stylesheet" href="css/pagination.css"> 引入之后,分页基本样子 下面看分页配置参数 var defaults = { totalData: 0, //数据总条数 showData: 0, //每页

无刷新分页 jquery.pagination.js

1.使用插件为 jquery.pagination.js ,如果没有这个js文件的话,我可以给发个. 首先引用 jquery.pagination.js (分页js),跟pagination.css(分页样式css). 点击获取查看这两个文件 2.页面js代码为 <script type="text/javascript"> var pageIndex = 0; //页面索引初始值 var pageSize = 15; //每页显示条数初始化,修改显示条数,修改这里即可 $

ajax分页实现,jquery.pagination.js

1.前台使用ajax无刷新分页,主要需要生成分页的工具条,这里使用的是jquery.pagination.js 插件参数可以参考----张龙豪-jquery.pagination.js分页 下面贴出代码 1 /** 2 * This jQuery plugin displays pagination links inside the selected elements. 3 * 4 * @author Gabriel Birke (birke *at* d-scribe *dot* de) 5

jquery.pagination.js分页

参数说明 参数名 描述 参数值 maxentries 总条目数                           必选参数,整数 items_per_page 每页显示的条目数                       可选参数,默认是10 num_display_entries 连续分页主体部分显示的分页条目数                       可选参数,默认是10 current_page 当前选中的页面                      可选参数,默认是0,表示第1页

jquery.pagination.js分页插件的使用

1.引用jquery.pagination.js <script src="${ctx}/themes/b2b2cv2/doctor/js/message/jquery.pagination.js"></script> 2.发送2次ajax请求,第一次需传入首页和页大小,返回数据得到所有数据的长度: 第二次发送请求传入点击的页码号和页的大小,返回数据得到点击页的数据 参考链接: 1.https://blog.csdn.net/baidu_25343343/art

jquery与js添加子元素

例如在select中添加option JQuery做法: <select id="myselect" name="myselect"> </select> $("#myselect").append('<option value="-1">请选择</option>'); js的做法:@参考文章 关于 select 的添加 option 应该注意的问题. 标准的做法如上也就是说,标

jquery pagination.js 分页

js代码: var pageIndex = 0;     //页面索引初始值              var pageSize = 10;     //每页显示条数初始化,修改显示条数,修改这里即可              var where="";          //查询条件            var searchwhere="";    //查询条件                        function PageSet(data){    

knockoutjs+ jquery pagination+asp.net web Api 实现无刷新列表页

Knockoutjs 是一个微软前雇员开发的前端MVVM JS框架, 具体信息参考官网 http://knockoutjs.com/ Web API数据准备: 偷个懒数据结构和数据copy自官网实例 http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api using System.Collections.Generic; using System.Lin

jquery.pagination结合jquery.tmpl封装前台分页--纯js实现与后端语言无关--适合所有前台分页情况

1.实现效果 2.逻辑说明,通过ajax请求从后获取所有数据,返回对象数据,在DOM加载完成是调用此方法完成数据装载和分页组件初始化,下面是使用DEMO说明,依赖js库稍后附上. <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <script type="text/javasc