成人精品一区二区三区中文字幕-成人精品一区二区三区-成人精品一级毛片-成人精品亚洲-日本在线视频一区二区-日本在线视频免费

導(dǎo)航首頁(yè) ? 技術(shù)教程 ? jQuery+Ajax實(shí)現(xiàn)無(wú)刷新分頁(yè)
全站頭部文字 我要出現(xiàn)在這里
jQuery+Ajax實(shí)現(xiàn)無(wú)刷新分頁(yè) 655 2024-03-22   

1、前臺(tái)使用ajax無(wú)刷新分頁(yè),主要需要生成分頁(yè)的工具條,這里使用的是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() {
   /**
   * 計(jì)算最大分頁(yè)顯示數(shù)目
    */
   function numPages() {
     return Math.ceil(maxentries/opts.items_per_page);
    }  
   /**
   * 極端分頁(yè)的起始和結(jié)束點(diǎn),這取決于current_page 和 num_display_entries.
   * @返回 {數(shù)組(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];
    }
     
    /**
     * 分頁(yè)鏈接事件處理函數(shù)
     * @參數(shù) {int} page_id 為新頁(yè)碼
    */
     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;
   }
   
   /**
    * 此函數(shù)將分頁(yè)鏈接插入到容器元素中
     */
    function drawLinks() {
      panel.empty();
      var interval = getInterval();
       var np = numPages();
      // 這個(gè)輔助函數(shù)返回一個(gè)處理函數(shù)調(diào)用有著正確page_id的pageSelected。
       var getClickHandler = function(page_id) {
        return function(evt){ return pageSelected(page_id,evt); }
      }
      //輔助函數(shù)用來(lái)產(chǎn)生一個(gè)單鏈接(如果不是當(dāng)前頁(yè)則產(chǎn)生span標(biāo)簽)
      var appendItem = function(page_id, appendopts){
        page_id = page_id<0?0:(page_id<np?page_id:np-1); // 規(guī)范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);
      }
       //產(chǎn)生描述
      panel.append("<span>共有 " + maxentries + " 條記錄,當(dāng)前第 <b>" + (current_page + 1) + "</b>/" + np + " 頁(yè)</span>");
       
      // 產(chǎn)生"Previous"-鏈接
       if(opts.prev_text && (current_page > 0 || opts.prev_show_always)){
         appendItem(current_page-1,{text:opts.prev_text, classes:"prev"});
       }
     // 產(chǎn)生起始點(diǎn)
       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);
        }
       }
       // 產(chǎn)生內(nèi)部的些鏈接
       for(var i=interval[0]; i<interval[1]; i++) {
         appendItem(i);
       }
      // 產(chǎn)生結(jié)束點(diǎn)
       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);
         }
        
       }
       // 產(chǎn)生 "Next"-鏈接
     if(opts.next_text && (current_page < np-1 || opts.next_show_always)){
         appendItem(current_page+1,{text:opts.next_text, classes:"next"});
       }
      }
     
     //從選項(xiàng)中提取current_page
     var current_page = opts.current_page;
     //創(chuàng)建一個(gè)顯示條數(shù)和每頁(yè)顯示條數(shù)值
    maxentries = (!maxentries || maxentries < 0)?1:maxentries;
     opts.items_per_page = (!opts.items_per_page || opts.items_per_page < 0)?1:opts.items_per_page;
     //存儲(chǔ)DOM元素,以方便從所有的內(nèi)部結(jié)構(gòu)中獲取
     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();
     // 回調(diào)函數(shù)
     //opts.callback(current_page, this);
  });
}

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

樣式代碼:

 .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;}

顯示效果如下:

查看圖片

原來(lái)的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;
 }

可以根據(jù)自己設(shè)計(jì)顯示樣式

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中放的是要顯示的數(shù)據(jù),生成的分頁(yè)的工具條是放在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  class='a-blue'>查看明細(xì)</a></td></tr>");
    });
    $("#tbIncome").append(arrHtml.join(''));
   });
  };
  function pageInit() {
   var totalCount = $("#hideTotalCount").val();
   $("#Pagination").pagination(parseInt(totalCount), {
    items_per_page: 10,
    //current_page: 1,//當(dāng)前選中的頁(yè)面默認(rèn)是0,表示第1頁(yè)
    num_edge_entries: 2,//兩側(cè)顯示的首尾分頁(yè)的條目數(shù),默認(rèn)為0,好像是尾部顯示的個(gè)數(shù)
    num_display_entries: 2,//連續(xù)分頁(yè)主體部分顯示的分頁(yè)條目數(shù),默認(rèn)是10
    link_to: "javascript:void(0)",//分頁(yè)的鏈接
    prev_text: "上一頁(yè)",
    next_text: "下一頁(yè)",
    prev_show_always: true,
    next_show_always: true,
    callback: searchMyIncome
   });
  }

searchMyme是獲取分頁(yè)的數(shù)據(jù),將總數(shù)放到一個(gè)隱藏的控件中,總數(shù)分頁(yè)控件需要使用,這里ajax調(diào)用需要同步執(zhí)行,不然取不到返回的總數(shù)pageInit() 就是初始化控件,這樣設(shè)置基本就OK了。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助。



主站蜘蛛池模板: 富二代| 黄美棋| 青春残酷物语| 3片| 南京铁道职业技术学校单招| asshole| 鬼吹灯之精绝古城演员表| 鲁班书咒语大全| 接吻戏| 康熙微服记四部免费观看在线| 卢昱晓电视剧| 名剑风流 电视剧| 黑水电影| 不回微信判30年图片| jixxzz| 咏春拳电影| 男女视频在线播放| 小学五年级研究报告| 百合 电影| 违规吃喝研讨发言材料| 嫩模被xxxx视频在线观看| 岩男润子| 汪汪队100集全免费| 赖小子| 默读车| 局中局演员表| 查妮甘·唐卡伯缇| 花火图片| 蕾切尔薇兹牺牲最大的电影| 香谱七十二法图解| 好好说再见电影| 神宫寺奈绪从早做到晚上| 谭耀文演的电影| 李泽锋个人资料| 一起再看流星雨| 女生网站| 平型关大捷纪念馆| 翡翠台高清直播| 谁的青春不迷茫 电影| 高钧贤| 《画江湖之不良人》|

!!!站長(zhǎng)長(zhǎng)期在線(xiàn)接!!!

網(wǎng)站、小程序:定制開(kāi)發(fā)/二次開(kāi)發(fā)/仿制開(kāi)發(fā)等

各種疑難雜癥解決/定制接口/定制采集等

站長(zhǎng)微信:lxwl520520

站長(zhǎng)QQ:1737366103