DIV CSS 佈局教程網

基於 jQuery 封裝的分頁插件代碼
編輯:JQuery特效代碼     

下面是一個用构造函数方式封裝的分頁插件 pagerBar,通過 ajax 異步加載每一頁的內容,並自動維護分頁信息與上一頁/下一頁按鈕狀態。

function pagerBar(dataCount,pageSize,serverUrl,contentPlace,pagerbarPlace,callBack){
  this.dataCount = dataCount;
  this.pageSize = pageSize;
  this.serverUrl = serverUrl;
  this.contentPlace = $("#"+contentPlace);
  this.pagerbarPlace = $("#"+pagerbarPlace);
  this.callBack = callBack;
  this.pageCount = 0;
  this.pageIndex = 1;
  this.curInfo = $("<span/>");
  this.prePage = $("<span/>");
  this.nextPage = $("<span/>");
  this.init();
}
pagerBar.prototype = {
  init : function(){
    this.getPageCount();
    this.initLink();
    this.showBarInfo();
    if(this.pageCount>0){ this.setLink(1); }
  },
  getPageCount : function(){
    this.pageCount = parseInt(this.dataCount / this.pageSize);
    if(this.dataCount % this.pageSize !=0){ this.pageCount++; }
  },
  initLink : function(){
    var self = this;
    this.prePage = $("<span/>").html("上一頁").addClass("pageLink");
    this.prePage.click(function(){ self.setLink(self.pageIndex-1); });
    this.nextPage = $("<span/>").html("下一頁").addClass("pageLink");
    this.nextPage.click(function(){ self.setLink(self.pageIndex+1); });
    this.pagerbarPlace.append(this.curInfo).append(this.prePage).append(this.nextPage);
  },
  showBarInfo : function(){
    this.prePage.hide();
    this.nextPage.hide();
    if(this.pageCount==0){ this.curInfo.html("暫時沒有信息!"); }
    else if(this.pageCount==1){ this.curInfo.html("1/1"); }
    else{ this.curInfo.html(this.pageCount + "/" + this.pageIndex); }
  },
  setLink : function(i){
    var self = this;
    $.ajax({
      url:self.serverUrl,
      type:"get",
      data:{pageSize:self.pageSize,pageIndex:i},
      cache:false,
      error:function(){ alert("數據加載失敗!"); },
      success:function(htmlData){
        self.contentPlace.html(htmlData);
        if(self.pageCount==1){ self.prePage.hide(); self.nextPage.hide(); }
        else{
          if(i==1){ self.prePage.hide(); self.nextPage.show(); }
          else if(i==self.pageCount){ self.prePage.show(); self.nextPage.hide(); }
          else{ self.prePage.show(); self.nextPage.show(); }
        }
        self.pageIndex = i;
        self.curInfo.html(self.pageCount+"/"+self.pageIndex);
        if(self.callBack){ self.callBack(); }
      }
    });
  },
  changeServerUrl : function(dataCount,serverUrl){
    this.dataCount = dataCount;
    this.serverUrl = serverUrl;
    this.pageIndex=1;
    this.getPageCount();
    this.showBarInfo();
    this.contentPlace.html("");
    if(this.pageCount>0){ this.setLink(1); }
  },
  dataCountDec : function(){
    this.dataCount--;
    this.getPageCount();
    if(this.pageCount<this.pageIndex){ this.pageIndex = this.pageCount; }
    if(this.pageIndex>0){ this.setLink(this.pageIndex); }
    this.showBarInfo();
  }
}

設計要點:getPageCount 根據總記錄數與每頁條數算出總頁數;initLink 動態生成上一頁/下一頁並綁定點擊;setLink 通過 ajax 拉取指定頁的 HTML 並根據當前頁位置切換按鈕的顯示;changeServerUrl 支持動態切換數據源並重置到第一頁;dataCountDec 則在記錄減少時重新計算並校正當前頁,避免越界。構造時傳入 callBack 還可在每次加載完成後執行自定義回調。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved