列表分页

HTML

<script src="/source/pagination/page.js"></script>
<link rel="stylesheet" href="/source/pagination/page.css"/>


<div class="pages">
    <div id="Pagination"></div>
        <div class="searchPage">
        <span class="page-sum"><strong class="allPage"></strong></span>
        <span class="page-go" style="display:none">跳转<input type="text"></span>
        <a href="javascript:;" class="page-btn" style="display:none">GO</a>
    </div>
</div>

ajax放在$document.ready外面(方法的实现尽量全部放在ready之前,不要在页面加载完毕再实现)

//ajax
beforeSend:function () {
    //鼠标样式变成等待样式(转动的圆圈)
    $(".main-right").css("cursor","wait");
},      
success:function(){
    ...
    if(res.totalPages != $(".allPage").html()){
        $(".allPage").html(res.totalPages);
    }
    ...
},
complete:function(){
    //鼠标样式恢复正常
    $(".main-right").css("cursor","default");
}

注:page.css和page.js是从http://www.jq22.com/yanshi3813扒出来并进行修改的

page.css

.pages {
    margin: 0px auto;
    text-align: center;
    width: -moz-fit-content;
}
.pages #Pagination {
    float: left;
    overflow: hidden;
}
.pages #Pagination .pagination {
    height: 40px;
    text-align: center;
}
.pages #Pagination .pagination a,
.pages #Pagination .pagination span {
    float: left;
    display: inline;
    padding: 6.5px 13px;
    border: 1px solid #e6e6e6;
    border-right: none;
    background: #f6f6f6;
    color: #666666;
    font-size: 14px;
    cursor: pointer;
    text-decoration: none
}
.pages #Pagination .pagination .current {
    background: #00a3d0;
    color: #fff;
}
.pages #Pagination .pagination .prev,
.pages #Pagination .pagination .next {
    float: left;
    padding: 6.5px 13px;
    border: 1px solid #e6e6e6;
    background: #f6f6f6;
    color: #666666;
    cursor: pointer;
}
.pages #Pagination .pagination .prev i,
.pages #Pagination .pagination .next i {
    display: inline-block;
    width: 4px;
    height: 11px;
    margin-right: 5px;
}
.pages #Pagination .pagination .prev {
    border-right: none;
}
.pages #Pagination .pagination .prev i {
    background-position: -144px -1px;
    *background-position: -144px -4px;
}
.pages #Pagination .pagination .next i {
    background-position: -156px -1px;
    *background-position: -156px -4px;
}
.pages #Pagination .pagination .pagination-break {
    padding: 11px 5px;
    border: none;
    border-left: 1px solid #e6e6e6;
    background: none;
    cursor: pointer;
}
.pages .searchPage {
    float: left;
    padding: 27px 8px;
}
.pages .searchPage .page-sum {
    padding: 11px 13px;
    color: #999999;
    font-family: \u5b8b\u4f53,Arial;
    font-size: 14px;
}
.pages .searchPage .page-go {
    padding: 8px 0;
    color: #999999;
    font-size: 14px;
    padding: 10px 0\9;
    *padding: 6px 0;
}
.pages .searchPage .page-go input {
    width: 30px;
    height: 20px;
    margin: 0 5px;
    padding-left: 5px;
    border: 1px solid #e4e4e4;
}
.pages .searchPage .page-btn {
    margin: 9px 0 5px 5px;
    padding: 2px 5px;
    background: #00a3d0;
    border-radius: 2px;
    color: #ffffff;
    font-size: 14px;
    text-decoration: none;
}

page.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
 */
jQuery(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 class='current'>" + 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='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"}));
            }
            // 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 class='pagination-break'>"+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 class='pagination-break'>"+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"}));
            }
            $('a', fragment).click(eventHandler);
            return fragment;
        }
    });

    $(".allPage").bind('DOMNodeInserted', function () {
        $(".page-btn").unbind("click");
        $("#Pagination").pagination(parseInt($(".allPage").html()));
        if($(".allPage").html()<2){
            $(".searchPage .page-go").css("display","none");
            $(".searchPage .page-btn").css("display","none");
        }else{
            $(".searchPage .page-go").css("display","");
            $(".searchPage .page-btn").css("display","");
        }
    });

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

        // Initialize options with default values
        opts = $.extend({
            items_per_page:1,
            num_display_entries:4,
            current_page:0,
            num_edge_entries:1,
            link_to:"#",
            prev_text:"上一页",
            next_text:"下一页",
            ellipse_text:"...",
            prev_show_always:false,
            next_show_always:false,
            renderer:"defaultRenderer",
            show_if_single_page:false,
            load_first_page:false,
            callback:function(){return false;}
        },opts||{});

        var containers = this,
            renderer, links, current_page;

        //goto
        $(".page-btn").on("click",function(){
            //console.log(allPage);
            var goPage = $(".page-go input").val(); //跳转页数
            if(goPage > 0 && goPage <= maxentries){
                InitTable(goPage);
                opts.current_page = goPage-1;
                $(".page-btn").unbind("click");
                $("#Pagination").pagination(maxentries,opts);
            }
            //清空用户跳转页数
            $(".page-go input").val("");
        });

        /**
         * 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).html(),
                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
            var current_page = containers.data('current_page');
            switch(new_current_page){
                case'上一页':
                    new_current_page = 0;
                    if(current_page > new_current_page) {
                        new_current_page = current_page - 1;
                        InitTable(new_current_page+1);
                    }
                    break;
                case'下一页':
                    new_current_page = maxentries - 1;
                    if(current_page < new_current_page) {
                        new_current_page = current_page + 1;
                        InitTable(new_current_page+1);
                    }
                    break;
                default:
                    InitTable(parseInt(--new_current_page)+1);
                    break;
            }
            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);
        containers.data('current_page', current_page);
        // Create a sane value for maxentries and items_per_page
        maxentries = (!maxentries || maxentries < 0)?0: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();
        // 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
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值