jQuery+Ajax實現無重新整理分頁_jquery

來源:互聯網
上載者:User

1、前台使用ajax無重新整理分頁,主要需要產生分頁的工具條,這裡使用的是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("<a href class='currentPage'>" + (appendopts.text) + "</a>");        }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);      }       //產生描述      panel.append("<span>共有 " + maxentries + " 條記錄,當前第 <b>" + (current_page + 1) + "</b>/" + np + " 頁</span>");             // 產生"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("<a href>"+opts.ellipse_text+"</a>").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("<a href>"+opts.ellipse_text+"</a>").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);  });}

代碼還是比較容易看明白的,可以根據自己需要修改,這裡使用的是自己的樣式

樣式代碼:

 .pages {display: inline-block; overflow: hidden;padding: 15px 0;text-align: center; width:100%; margin:50px 0;} .pages b{ color:#e75f49;} .pages a { color:#666; border: 1px solid #e5e5e5;cursor: pointer;font-size: 12px;margin-right: 5px; padding: 8px 12px; text-decoration: none; background-color:#fafafa;} .pages .currentPage{ background-color: #00a0e9; border: 1px solid #00a0e9;color: #fff; font-weight: bold;}

顯示效果如下:

 

原來的css樣式:

.pagination a { text-decoration: none;  border: 1px solid #AAE;  color: #15B; } .pagination a, .pagination span {  display: inline-block; padding: 0.1em 0.4em;  margin-right: 5px; margin-bottom: 5px; } .pagination .current { background: #26B; color: #fff; border: 1px solid #AAE; } .pagination .current.prev, .pagination .current.next{  color:#999;  border-color:#999;  background:#fff; }

可以根據自己設計顯示樣式

2、使用方法

2.1、html顯示

 <div class="second-ul-ctn">   <ul class="second-ul" id="ulProducts">   </ul>  <div class="pages">   <input type="hidden" id="hideTotalCount" />    <div id="Pagination" class="pagination">    </div>   </div>  </div>

ulProducts中放的是要顯示的資料,產生的分頁的工具條是放在Pagination中的

2.2 javascript代碼

$(function () {   searchMyme(0);   pageInit();   $("#btnSearch").on("click", function () {    searchMyme(0);    pageInit();    return false;   });  });  function searchMyme(page, pageination) {   var month = $("#btnMonth").val();   var obj = {    Month: month,    OpType: "getme",    page: (page + 1)    , rows: 10   };   var url = "../../Controler/FinaceMo/GetStaffIncome_H.ashx";   $.get(url, obj, function (data) {    $("#tbIncome").empty();    var obj = JSON.parse(data);    var total = obj.Total;    $("#hideTotalCount").val(total);    var arrHtml = [];    $.each(obj.Rows, function (i, data) {     arrHtml.push("<tr><td>" + (i + 1) + "</td>");     arrHtml.push("<td>" + data.Account + "</td>");     arrHtml.push("<td>" + data.Name + "</td>");     arrHtml.push("<td>" + data.Month + "</td>");     arrHtml.push("<td>" + data.IncomeAmount + "</td>");     arrHtml.push("<td><a href='MyDetail.aspx?Account="+data.Account+"&Month="+data.Month+"' class='a-blue'>查看明細</a></td></tr>");    });    $("#tbIncome").append(arrHtml.join(''));   });  };  function pageInit() {   var totalCount = $("#hideTotalCount").val();   $("#Pagination").pagination(parseInt(totalCount), {    items_per_page: 10,    //current_page: 1,//當前選中的頁面預設是0,表示第1頁    num_edge_entries: 2,//兩側顯示的首尾分頁的條目數,預設為0,好像是尾部顯示的個數    num_display_entries: 2,//連續分頁主體部分顯示的分頁條目數,預設是10    link_to: "javascript:void(0)",//分頁的連結    prev_text: "上一頁",    next_text: "下一頁",    prev_show_always: true,    next_show_always: true,    callback: searchMyIncome   });  }

searchMyme是擷取分頁的資料,將總數放到一個隱藏的控制項中,總數分頁控制項需要使用,這裡ajax調用需要同步執行,不然取不到返回的總數pageInit() 就是初始化控制項,這樣設定基本就OK了。

以上就是本文的全部內容,希望對大家的學習有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.