jQuery外掛程式pagination.js源碼解讀

來源:互聯網
上載者:User

標籤:

pagination的github地址:https://github.com/gbirke/jquery_pagination

 

公司用的是1.2的版本,所以我就讀1.2的了。

jQuery.fn.pagination = function(maxentries, opts){  opts = $.extend({  isCurrentInfo: false,//是否顯示當前頁資訊: 當前第1頁,共10頁  currentCls: ‘.current-info‘,//當前頁class  items_per_page:10,//每頁最多有幾項  num_display_entries:10,//中間的頁數 如 1 2 ... 5 6 7 8 9 ... 12 13 中間頁數是5  current_page:0,//當前頁  num_edge_entries:1,//兩端頁數 如 1 2...5 6 7 8 9...12 13兩端頁數是2  link_to:"javascript:;",//href  prev_text:"上一頁",  next_text:"下一頁",  ellipse_text:"...",//省略的頁數的文本  prev_show_always:true,//總是顯示上一頁  next_show_always:true,//總是顯示下一頁  callback:function(){return false;}//回調},opts||{});    return this.each(function() {            //code        });};

 

我們需要給pagination方法傳遞2個參數,

maxentries:總共有多少項,必填

opts,各種配置項,都為選填。

 

function numPages(){}計算總頁數

function getInterval(){} 擷取中間頁數這裡的開始頁和結束頁,作為數組返回[5,10]

function pageSelected(page_id, evt){} 分頁的連結處理函數

function drawLinks() {}繪製連結

關鍵就是drawLinks:

function drawLinks() {    panel.empty();//每一次繪製都是全部重繪    var interval = getInterval();//擷取開始和結束頁    var np = numPages();//擷取總頁數    //pageSelected擷取到當前頁,然後重繪了連結    var getClickHandler = function(page_id) {        return function(evt){ return pageSelected(page_id,evt); }    }    // 添加一個單串連    var appendItem = function(page_id, appendopts){        page_id = page_id<0?0:(page_id<np?page_id:np-1);        appendopts = $.extend({text:page_id+1, classes:""}, appendopts||{});        if(page_id == current_page){//如果是當前頁,產生span            var lnk = $("<span class=‘current‘>"+(appendopts.text)+"</span>");        }        else{//否則產生超連結            var lnk = $("<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);}//添加class        panel.append(lnk);//將串連append到panel    }    // 繪製上一頁    if(opts.prev_text && (current_page > 0 || opts.prev_show_always)){        appendItem(current_page-1,{text:opts.prev_text, classes:"prev"});    }    // 繪製起始點  1 2 ...    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)        {            $("<span>"+opts.ellipse_text+"</span>").appendTo(panel);        }    }    // 繪製中間部分的串連 5 6 7 8 9    for(var i=interval[0]; i<interval[1]; i++) {        appendItem(i);    }     // 繪製結束點 ...12 13    if (interval[1] < np && opts.num_edge_entries > 0)    {        if(np-opts.num_edge_entries > interval[1]&& opts.ellipse_text)        {            $("<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);        }            }    // 繪製下一頁    if(opts.next_text && (current_page < np-1 || opts.next_show_always)){        appendItem(current_page+1,{text:opts.next_text, classes:"next"});    }    // 繪製當前第幾頁,共幾頁    if(opts.isCurrentInfo){        var sInfo = ‘當前第 ‘ + (current_page + 1) + ‘ 頁,共 ‘ + np + ‘ 頁‘;        $(opts.currentCls).html(sInfo);                            }}

 

jQuery外掛程式pagination.js源碼解讀

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.