首頁 > web前端 > js教程 > 主體

在jQuery中如何實現動態資料分頁

亚连
發布: 2018-06-14 15:22:21
原創
3507 人瀏覽過

本片文章教給大家用jQuery實現頁碼跳轉式動態資料分頁,效果非常好而且沒有頁面刷新,需要的朋友學習下吧。

我們這次帶給大家的是用jQuery實現頁碼跳轉式動態資料分頁這個效果,這個效果最明顯的就是沒有資料載入和刷新頁面這個過程,再做小型資料的載入也多級網頁載入的時候很有效。我們先來看下實際效果:

下面我們給出全部效果代碼:

HTML全部程式碼:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jQuery实现页码跳转式动态数据分页-脚本之家</title>
<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
<!-- <script type="text/javascript" src="back-endPage.js"></script> --> <!-- 后台分页 -->
<script type="text/javascript" src="js/front-endPage.js"></script><!-- 前台分页 -->
<script type="text/javascript" src="js/shujv2.js"></script>
<link rel="stylesheet" type="text/css" href="css/table.css" rel="external nofollow" >
</head>
<body>
<p id="histroyBox"></p>
<script type="text/javascript">
  //前台分页的样子
  $(&#39;#histroyBox&#39;).CJJTable({
    &#39;title&#39;:["装点","卸点","运输货物","下单日期","作者"],//thead中的标题 必填
    &#39;body&#39;:["taskCode","startAddr","endAddr","varietiesTypeName","createDate","customerName"],//tbody td 取值的字段 必填
    &#39;display&#39;:[1,1,1,1,1,2],//隐藏域,1显示,2隐藏 必填
    &#39;pageNUmber&#39;:10,//每页显示的条数 选填
    &#39;pageLength&#39;:data.length,//选填
    &#39;url&#39;:data,//数据源 必填
    dbTrclick:function(e){//双击tr事件
    alert(e.find(&#39;.taskCode&#39;).html())
    }
  });
  //后台分页的样子
  /*$(&#39;#histroyBox&#39;).CJJTable({
    &#39;title&#39;:["装点","卸点","运输货物","下单日期"],//thead中的标题 必填
    &#39;body&#39;:["contactName","contactMobliePhone","carrierName","taskNum","taskCustomerExpectPrice","taskCustomerBudgetFreight"],//tbody td 取值的字段 必填
    &#39;display&#39;:[1,1,1,1,2,2],//隐藏域,1显示,2隐藏 必填
    &#39;pageJson&#39;:{
      "taskId":528710,
      "pageSize":100,//ajax请求参数中的每页展示数量 选填
      "token":"yJUmunFeG3REqisYAmCfeA"
    },
    &#39;url&#39;:&#39;api/quoted/quotedList&#39;,//数据源 必填
    dbTrclick:function(that){ //双击tr事件
      alert(that.find(&#39;.contactName&#39;).html())
    }
  });*/
</script>
</body>
</html>
登入後複製

其中是資料JS內容,大家在實際操作的時候可以用JS取得json達到要的資料格式:

shujv2.js程式碼:

var data = [ {
  "warehouseID" : "3750",
  "warehouseCode" : "CK20140825061813777127447",
  "province" : "上海",
  "endProvince" : "江苏省",
  "provinceId" : "310000",
  "cityId" : "310100",
  "areaId" : "310109",
  "endProvinceId" : "320000",
  "endCityId" : "320600",
  "endAreaId" : "320612",
  "materialId" : "0",
  "material" : "",
  "specId" : "0",
  "productNId" : "0",
  "varietiesId" : "8",
  "varietiesTypeId" : "621",
  "taskCode" : "T1611230481",
  "customerID" : "20147",
  "customerName" : "脚本之家",
  "createDate" : "2016-11-23 18:41:40",
  "wareHouseName" : "找钢仓库",
  "city" : "上海市",
  "area" : "虹口区",
  "startAddr" : "逸仙路25号",
  "endCity" : "南通市",
  "endArea" : "通州区",
  "endAddr" : "1",
  "varietiesTypeName" : "钢材",
  "varieties" : "线材",
  "productN" : "",
  "spec" : "",
  "weight" : "1",
  "num" : "1",
  "receiver" : "",
  "receiverPhone" : "1",
  "remark" : "",
  "label" : "",
  "startAddrNumber" : "1",
  "varietiesNumber" : "1",
  "contactsId" : "22494",
  "contactsName" : "刘宇测试"
 } ]
登入後複製

front-endPage.js 是分頁載入的效果實作。程式碼為:

;(function($, window, document,undefined) {
 var cjjTable = function(ele,opt){
    this.$element = ele,
    this.defaults ={
       title:null,
       body:null,
       display:null,
       pageNUmber:8,
       pageLength:0,
       url:null,
       dbTrclick:function(that){
       }
    }
    this.options = $.extend({},this.defaults,opt)
 }
 cjjTable.prototype = {
    start:function(){
      var _this = this;
      var titlelistBox="";
      var titlesmall="";
      for(var i=0;i<_this.options.title.length;i++){
        titlesmall+="<th>"+_this.options.title[i]+"</th>";
        titlelistBox = titlesmall;
      }
      var json = "";
      var maxpagenumberBox = 7;//选择项最多的数量
      var json = this.options.url;
      var histroy_DDBox = "";
      var histroy_DD = "";
      var firstPageNumber=_this.options.pageLength>_this.options.pageNUmber?_this.options.pageNUmber:_this.options.pageLength;
      for (var i = 0; i <firstPageNumber; i++) {
        var bodyBigBox="";
        var bodyBox="";
        for(var x=0;x<_this.options.body.length;x++){
          var type = _this.options.body[x];
          var display = "table-cell";
          if(_this.options.body.length>_this.options.title.length&&_this.options.display[x]!=undefined){
            display = _this.options.display[x]*1==1?"table-cell":"none";
          }
          bodyBox+="<td class=&#39;"+type+"&#39; style=&#39;display:"+display+"&#39;>"+json[i][type]+"</td>";
          bodyBigBox = bodyBox;
        }
        histroy_DD +="<tr class=&#39;new_productBox&#39;>"+bodyBigBox+"</tr>";
        histroy_DDBox = histroy_DD;
      }
      $( _this.$element.selector+" table tfoot").html("");
      if (Math.ceil(_this.options.pageLength/ _this.options.pageNUmber) == 1) {
        $( _this.$element.selector+" .nextPage").css("display", "none");
        $(_this.$element.selector+" .endPage").css("display", "none");
      }
      var maxpagenumberBoxBigbox = "";
      var maxpagenumberBoxBig = "";
      if (Math.ceil(_this.options.pageLength/ _this.options.pageNUmber) < maxpagenumberBox) {
        for (var i = 0; i < Math.ceil(_this.options.pageLength/ _this.options.pageNUmber); i++) {
          var className = "";
          if(i==0){
            className = "pagenumberBoxLi";
          }
          maxpagenumberBoxBig += &#39;<li class="&#39;+className+&#39;">&#39; + (i * 1 + 1) + &#39;</li>&#39;;
          maxpagenumberBoxBigbox = maxpagenumberBoxBig;
        }
      } else {
        for (var i = 0; i < maxpagenumberBox; i++) {
          var className = "";
          if(i==0){
            className = "pagenumberBoxLi";
          }
          maxpagenumberBoxBig += &#39;<li class="&#39;+className+&#39;">&#39; + (i * 1 + 1) + &#39;</li>&#39;;
          maxpagenumberBoxBigbox = maxpagenumberBoxBig;
        }
      }
      var buttonTfoot = "<tr>"+
      "<td colspan=&#39;"+_this.options.title.length+"&#39;>"+
        "<p style=&#39;float:right;margin-left:10px;&#39; class=&#39;tfootRight&#39;>"+
          "<input placeholder=&#39;输入页码&#39; type=&#39;text&#39;>"+
          "<button>确定</button>"+
          "</p>"+
          "<p style=&#39;float:right&#39;>"+
            "<span class=&#39;firstPage&#39; style=&#39;margin-right:10px;cursor: pointer;float:left;display: none;margin-left:10px;&#39;>首页</span>"+
            "<span class=&#39;lastPage&#39; style=&#39;margin-right:10px;cursor: pointer;float:left;display: none;&#39;>上一页</span>"+
            "<ul class=&#39;pagenumberBox&#39;>"+maxpagenumberBoxBigbox+"</ul>"+
            "<input class=&#39;typeNumber&#39; type=&#39;text&#39; value=&#39;1&#39; onfocus=&#39;this.blur()&#39; style=&#39;display:none;width:20px;height:20px;text-align:center;line-height:20px;border:1px solid #666;margin-right:5px;float:left;margin-top:2.5px;&#39;>"+
            "<span class=&#39;nextPage&#39; style=&#39;margin-right:10px;float:left;cursor: pointer;&#39;>下一页</span>"+
            "<span class=&#39;endPage&#39; style=&#39;cursor: pointer;float:left;&#39;>尾页</span>"+
          "</p>"+
          "<p style=&#39;float:right&#39;>"+
             "<select><option value=&#39;5&#39;>5</option><option value=&#39;10&#39;>10</option><option value=&#39;20&#39;>20</option><option value=&#39;50&#39;>50</option><option value=&#39;100&#39;>100</option><option value=&#39;200&#39;>200</option><option value=&#39;500&#39;>500</option></select>"
          "</p>"+
        "</p>"+
      "<td>"+
      "<tr>";
      _this.$element.html("<table class=&#39;CJJ-Table&#39;><thead>"+titlelistBox+"</thead><tbody>"+histroy_DDBox+"</tbody><tfoot>"+buttonTfoot+"</tfoot></table>");
      $(_this.$element.selector+ &#39; select&#39;).val(_this.options.pageNUmber);
      if(Math.ceil(_this.options.pageLength/_this.options.pageNUmber)<2){
        $(_this.$element.selector+ &#39; .endPage&#39;).hide();
        $(_this.$element.selector+ &#39; .nextPage&#39;).hide();
      }
      $(_this.$element.selector+ &#39; .tfootRight input&#39;).unbind(&#39;keyup&#39;).keyup(function(){
        _this.inputKeyup(_this,maxpagenumberBox,json);
      })
      $(_this.$element.selector+ &#39; .tfootRight button&#39;).unbind(&#39;click&#39;).click(function(){
         _this.button(_this,maxpagenumberBox,json);
      });
      $(_this.$element.selector+ &#39; .firstPage&#39;).unbind(&#39;click&#39;).click(function(){
         _this.firstPage(_this,maxpagenumberBox,json);
      });
      $(_this.$element.selector+ &#39; .endPage&#39;).unbind(&#39;click&#39;).click(function(){
         _this.endPage(_this,maxpagenumberBox,json);
      });
      $(_this.$element.selector+ &#39; .nextPage&#39;).unbind(&#39;click&#39;).click(function(){
         _this.nextPage(_this,maxpagenumberBox,json);
      });
      $(_this.$element.selector+ &#39; table tfoot ul li&#39;).unbind(&#39;click&#39;).click(function(){
         _this.nextTableLi(_this,maxpagenumberBox,json,$(this));
      });
      $(_this.$element.selector+ &#39; .lastPage&#39;).unbind(&#39;click&#39;).click(function(){
         _this.lastPage(_this,maxpagenumberBox,json);
      });
      $(_this.$element.selector+ &#39; select&#39;).unbind(&#39;change&#39;).change(function(){
         _this.select(_this,maxpagenumberBox,json,$(this));
      });
      $(_this.$element.selector+ &#39; tbody tr&#39;).unbind(&#39;dblclick&#39;).dblclick(function(){
         _this.options.dbTrclick($(this));
      });
    },
    inputKeyup:function(e,maxpagenumberBox,json){
      var val = $(e.$element.selector+ " .tfootRight input").val();
      if (val == 0) {
        var val2 = val.replace(0, "");
      } else if (val > 0 && val <= Math.ceil(e.options.pageLength / e.options.pageNUmber)) {
        var val2 = val.replace(/[^0-9]/g, "");
      } else if (val > Math.ceil(e.options.pageLength/ e.options.pageNUmber)) {
        var val2 = Math.ceil(e.options.pageLength / e.options.pageNUmber);
      }
      $(e.$element.selector+ &#39; .tfootRight input&#39;).val(val2);
    },
    button:function(e,maxpagenumberBox,json){
      var val = $(e.$element.selector+ &#39; .tfootRight input&#39;).val();
      $(e.$element.selector+ " .typeNumber").val(val);
      if (val != "") {
        e.page($(e.$element.selector+ " .typeNumber").val(), e.options.pageNUmber, maxpagenumberBox,json,e.$element, e);
      }
    },
    firstPage:function(e,maxpagenumberBox,json){
      $(e.$element.selector+ " .typeNumber").val(1);
      e.page($(e.$element.selector+ " .typeNumber").val(), e.options.pageNUmber, maxpagenumberBox,json,e.$element, e);
    },
    endPage:function(e,maxpagenumberBox,json){
      $(e.$element.selector+ " .typeNumber").val(Math.ceil(e.options.pageLength / e.options.pageNUmber));
      e.page($(e.$element.selector+ " .typeNumber").val(), e.options.pageNUmber, maxpagenumberBox,json,e.$element, e);
    },
    nextPage:function(e,maxpagenumberBox,json){
      var number = $(e.$element.selector+ " .typeNumber").val();
      $(e.$element.selector+ " .typeNumber").val(number * 1 + 1);
      e.page($(e.$element.selector+ " .typeNumber").val(), e.options.pageNUmber, maxpagenumberBox,json,e.$element, e);
  
    },
    nextTableLi:function(e,maxpagenumberBox,json,that){
      var val = that.html();
      $(e.$element.selector+ " .typeNumber").val(val);
      e.page($(e.$element.selector+ " .typeNumber").val(), e.options.pageNUmber, maxpagenumberBox,json,e.$element, e);
    },
    lastPage:function(e,maxpagenumberBox,json){
      var number = $(e.$element.selector+ " .typeNumber").val();
      if (number > 1) {
        $(e.$element.selector+ " .typeNumber").val(number * 1 - 1);
        e.page($(e.$element.selector+ " .typeNumber").val(), e.options.pageNUmber, maxpagenumberBox,json,e.$element, e);
      }
    },
    select:function(e,maxpagenumberBox,json,that){
      var select = that.find("option:selected").val();
      $(e.$element.selector+ " .typeNumber").val(1); 
      e.options.pageNUmber = select;
      e.page($(e.$element.selector+ " .typeNumber").val(), e.options.pageNUmber, maxpagenumberBox,json,e.$element, e);
    },
    page:function(Pagenumber, pageNUmber, maxpagenumberBox,json,that,e) {
      var histroy_DDBox = "";
      var histroy_DD = "";
      var lastPage=Pagenumber<Math.ceil(e.options.pageLength / pageNUmber)?Pagenumber*pageNUmber:e.options.pageLength;
      for (var i =(Pagenumber-1)*pageNUmber; i < lastPage; i++) {
        var bodyBigBox="";
        var bodyBox="";
        for(var x=0;x<e.options.body.length;x++){
          var type = e.options.body[x];
          var display = "table-cell";
          if(e.options.body.length>e.options.title.length&&e.options.display[x]!=undefined){
            display = e.options.display[x]*1==1?"table-cell":"none";
          }
          bodyBox+="<td class=&#39;"+type+"&#39; style=&#39;display:"+display+"&#39;>"+json[i][type]+"</td>";
          bodyBigBox = bodyBox;
        }
        histroy_DD +="<tr class=&#39;new_productBox&#39;>"+bodyBigBox+"</tr>";
        histroy_DDBox = histroy_DD;
      }
      $(that.selector+" table tbody").html(histroy_DD);
      var maxpagenumberBoxBigbox = "";
      var maxpagenumberBoxBig = "";
      if (Math.ceil(e.options.pageLength/ e.options.pageNUmber) < maxpagenumberBox) {
        for (var i = 0; i < Math.ceil(e.options.pageLength/ e.options.pageNUmber); i++) {
          var className = "";
          if(i==0){
            className = "pagenumberBoxLi";
          }
          maxpagenumberBoxBig += &#39;<li class="&#39;+className+&#39;">&#39; + (i * 1 + 1) + &#39;</li>&#39;;
          maxpagenumberBoxBigbox = maxpagenumberBoxBig;
        }
      } else {
        for (var i = 0; i < maxpagenumberBox; i++) {
          var className = "";
          if(i==0){
            className = "pagenumberBoxLi";
          }
          maxpagenumberBoxBig += &#39;<li class="&#39;+className+&#39;">&#39; + (i * 1 + 1) + &#39;</li>&#39;;
          maxpagenumberBoxBigbox = maxpagenumberBoxBig;
        }
      }
      $(that.selector+" table tfoot ul").html(maxpagenumberBoxBigbox);
      if (Pagenumber == 1) {
        $(that.selector+" .firstPage,"+that.selector+" .lastPage").hide();
      } else {
        $(that.selector+" .firstPage,"+that.selector+" .lastPage").show();
      }
      if (Pagenumber == Math.ceil(e.options.pageLength / pageNUmber)) {
        $(that.selector+" .endPage,"+that.selector+" .nextPage").hide();
      } else {
        $(that.selector+" .endPage,"+that.selector+" .nextPage").show();
      }
      if (Math.ceil(e.options.pageLength/ pageNUmber) > maxpagenumberBox) {
        if (Pagenumber > 0 && Pagenumber < Math.ceil(maxpagenumberBox / 2) * 1 + 1) {
          maxpagenumberBoxBigbox = "";
          maxpagenumberBoxBig = "";
          for (var i = 0; i < maxpagenumberBox; i++) {
            maxpagenumberBoxBig += &#39;<li>&#39; + (i * 1 + 1) + &#39;</li>&#39;;
            maxpagenumberBoxBigbox = maxpagenumberBoxBig;
          }
          $(that.selector+" .pagenumberBox").html(maxpagenumberBoxBigbox);
          $(that.selector+&#39; .pagenumberBox li&#39;).eq(Pagenumber - 1).addClass(&#39;pagenumberBoxLi&#39;);
        } else if (Pagenumber >= Math.ceil(maxpagenumberBox / 2) * 1 + 1 && Pagenumber < Math.ceil(e.options.pageLength / pageNUmber) - Math.ceil(maxpagenumberBox / 2) + 2) {
          maxpagenumberBoxBigbox = "";
          maxpagenumberBoxBig = "";
          for (var i = Pagenumber - Math.ceil(maxpagenumberBox / 2) + 1; i < Pagenumber * 1 + Math.ceil(maxpagenumberBox / 2) * 1; i++) {
            maxpagenumberBoxBig += &#39;<li>&#39; + (i) + &#39;</li>&#39;;
            maxpagenumberBoxBigbox = maxpagenumberBoxBig;
          }
          $(that.selector+" .pagenumberBox").html(maxpagenumberBoxBigbox);
          $(that.selector+&#39; .pagenumberBox li&#39;).eq(Math.ceil(maxpagenumberBox / 2) - 1).addClass(&#39;pagenumberBoxLi&#39;);
        } else if (Pagenumber >= Math.ceil(e.options.pageLength / pageNUmber) - Math.ceil(maxpagenumberBox / 2) + 2 && Pagenumber <= Math.ceil(e.options.pageLength / pageNUmber)) {
          maxpagenumberBoxBigbox = "";
          maxpagenumberBoxBig = "";
          for (var i = Math.ceil(e.options.pageLength / pageNUmber) - maxpagenumberBox; i < Math.ceil(e.options.pageLength / pageNUmber); i++) {
            maxpagenumberBoxBig += &#39;<li>&#39; + (i * 1 + 1) + &#39;</li>&#39;;
            maxpagenumberBoxBigbox = maxpagenumberBoxBig;
          }
          $(that.selector+" .pagenumberBox").html(maxpagenumberBoxBigbox);
          $(that.selector+&#39; .pagenumberBox li&#39;).eq(Pagenumber - Math.ceil(e.options.pageLength/ pageNUmber) + maxpagenumberBox * 1 - 1).addClass(&#39;pagenumberBoxLi&#39;);
        }
      } else {
        if (Pagenumber <= Math.ceil(e.options.pageLength / pageNUmber)) {
          $(that.selector+&#39; .pagenumberBox li&#39;).removeClass(&#39;pagenumberBoxLi&#39;);
          $(that.selector+&#39; .pagenumberBox li&#39;).eq(Pagenumber - 1).addClass(&#39;pagenumberBoxLi&#39;);
        }
      }
      $(that.selector+ &#39; table tfoot ul li&#39;).unbind(&#39;click&#39;).click(function(){
         e.nextTableLi(e,maxpagenumberBox,json,$(this));
      });
      $(that.selector+ &#39; tbody tr&#39;).unbind(&#39;dblclick&#39;).dblclick(function(){
         e.options.dbTrclick($(this));
      });
    }  

 }
 $.fn.CJJTable = function(options){
     var cjj = new cjjTable(this,options);
     return cjj.start();
 }
})(jQuery, window, document);
登入後複製

上面是我整理給大家的,希望今後對大家有幫助。

相關文章:

在js中如何實作上傳並壓縮圖片功能(詳細教學)

使用vue如何實現CSS過渡效果

使用微信小程式如何實作圖片上傳功能

#使用微信小程式有關傳參數的方法,有哪些?

使用async-validator如何寫Form元件(詳細教學)

使用casperjs和resemble.js如何實作像素比較(詳細教學)

使用JavaScript如何實現快速排序(詳細教學)

以上是在jQuery中如何實現動態資料分頁的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板