jquery.pagination.js分頁demo


公用jquery.pagination.js

/**
 * This jQuery plugin displays pagination links inside the selected elements.
 *
 * @author Gabriel Birke (birke *at* d-scribe *dot* de)
 * @version 1.2
 * @param {int} maxentries Number of entries to paginate
 * @param {Object} opts Several options (see README for documentation)
 * @return {Object} jQuery Object
 */
jQuery.fn.pagination = function(maxentries, opts){
    opts = jQuery.extend({
        items_per_page:10,
        num_display_entries:10,
        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,
        callback:function(){return false;}
    },opts||{});
    
    return this.each(function() {
        /**
         * 計算最大分頁顯示數目
         */
        function numPages() {
            return Math.ceil(maxentries/opts.items_per_page);
        }    
        /**
         * 極端分頁的起始和結束點,這取決於current_page 和 num_display_entries.
         * @返回 {數組(Array)}
         */
        function getInterval()  {
            var ne_half = Math.ceil(opts.num_display_entries/2);
            var np = numPages();
            var upper_limit = np-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, np):Math.min(opts.num_display_entries, np);
            return [start,end];
        }
        
        /**
         * 分頁鏈接事件處理函數
         * @參數 {int} page_id 為新頁碼
         */
        function pageSelected(page_id, evt){
            current_page = page_id;
            drawLinks();
            var continuePropagation = opts.callback(page_id, panel);
            if (!continuePropagation) {
                if (evt.stopPropagation) {
                    evt.stopPropagation();
                }
                else {
                    evt.cancelBubble = true;
                }
            }
            return continuePropagation;
        }
        
        /**
         * 此函數將分頁鏈接插入到容器元素中
         */
        function drawLinks() {
            panel.empty();
            var interval = getInterval();
            var np = numPages();
            // 這個輔助函數返回一個處理函數調用有着正確page_id的pageSelected。
            var getClickHandler = function(page_id) {
                return function(evt){ return pageSelected(page_id,evt); }
            }
            //輔助函數用來產生一個單鏈接(如果不是當前頁則產生span標簽)
            var appendItem = function(page_id, appendopts){
                page_id = page_id<0?0:(page_id<np?page_id:np-1); // 規范page id值
                appendopts = jQuery.extend({text:page_id+1, classes:""}, appendopts||{});
                if(page_id == current_page){
                    var lnk = jQuery("<span class='current'>"+(appendopts.text)+"</span>");
                }else{
                    var lnk = jQuery("<a>"+(appendopts.text)+"</a>")
                        .bind("click", getClickHandler(page_id))
                        .attr('href', opts.link_to.replace(/__id__/,page_id));        
                }
                if(appendopts.classes){lnk.addClass(appendopts.classes);}
                panel.append(lnk);
            }
            // 產生"Previous"-鏈接
            if(opts.prev_text && (current_page > 0 || opts.prev_show_always)){
                appendItem(current_page-1,{text:opts.prev_text, classes:"prev"});
            }
            // 產生起始點
            if (interval[0] > 0 && opts.num_edge_entries > 0)
            {
                var end = Math.min(opts.num_edge_entries, interval[0]);
                for(var i=0; i<end; i++) {
                    appendItem(i);
                }
                if(opts.num_edge_entries < interval[0] && opts.ellipse_text)
                {
                    jQuery("<span>"+opts.ellipse_text+"</span>").appendTo(panel);
                }
            }
            // 產生內部的些鏈接
            for(var i=interval[0]; i<interval[1]; i++) {
                appendItem(i);
            }
            // 產生結束點
            if (interval[1] < np && opts.num_edge_entries > 0)
            {
                if(np-opts.num_edge_entries > interval[1]&& opts.ellipse_text)
                {
                    jQuery("<span>"+opts.ellipse_text+"</span>").appendTo(panel);
                }
                var begin = Math.max(np-opts.num_edge_entries, interval[1]);
                for(var i=begin; i<np; i++) {
                    appendItem(i);
                }
                
            }
            // 產生 "Next"-鏈接
            if(opts.next_text && (current_page < np-1 || opts.next_show_always)){
                appendItem(current_page+1,{text:opts.next_text, classes:"next"});
            }
        }
        
        //從選項中提取current_page
        var current_page = opts.current_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;
        //存儲DOM元素,以方便從所有的內部結構中獲取
        var panel = jQuery(this);
        // 獲得附加功能的元素
        this.selectPage = function(page_id){ pageSelected(page_id);}
        this.prevPage = function(){ 
            if (current_page > 0) {
                pageSelected(current_page - 1);
                return true;
            }
            else {
                return false;
            }
        }
        this.nextPage = function(){ 
            if(current_page < numPages()-1) {
                pageSelected(current_page+1);
                return true;
            }
            else {
                return false;
            }
        }
        // 所有初始化完成,繪制鏈接
        drawLinks();
        // 回調函數
        opts.callback(current_page, this);
    });
}

在js中調用:

$("#pagination").pagination($("#totalRecords").text(),{
            num_edge_entries: 1, //邊緣頁數
            num_display_entries: 4, //主體頁數
            current_page:parseInt(obj.currPageIndex),
            items_per_page:parseInt(obj.pageSize),
            prev_text: "<<",
            next_text: ">>",
            callback:function(pageIndex,container){
                if(obj.isPageInit)
                    {
                        obj.isPageInit=false;                        
                    }
                else
                    {                        
                       obj.currPageIndex=pageIndex;
                        obj.getBookPager();
                        return false;
                    }
                 
            }            
        });

jsp頁面:

<div class="row-fluid div-pager">
    <div class="span4">
        <div class="pageTip">
            當前為第<input type="hidden" id="hd-pageSize" 
                    value="${pageInfo.pageSize}"> <input type="hidden" id="hd-pageIndex" 
                    value="${pageInfo.pageIndex}">${pageInfo.totalRecord==0?0:
                pageInfo.pageSize*pageInfo.pageIndex+1}
                到第${(pageInfo.pageIndex+1)*pageInfo.pageSize>pageInfo.totalRecord?pageInfo.totalRecord:(pageInfo.pageIndex+1)*pageInfo.pageSize}
                條數據;總共有<span id="totalRecords">${pageInfo.totalRecord}</span>條記錄
        </div>
    </div>
    <div class="span7 pull-right">
        <div class=" pull-right">到第<input type="number"  name="toNo" value="${toNo}" min="1" max="${pageInfo.totalPageNumber}"  style="width:40px;height:15px;margin-bottom:3px;padding:0 3px">&nbsp;&nbsp;<a href="#" class="toPageNo" style="color:black">確定</a></div>
        <div id="pagination" class="pagination pull-right"></div>

    </div>
    
</div>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM