datatable.js进行前端分页

一、最近做一个项目,并没有分页接口,一次性返回所有的数据,需要前端进行分页显示功能;可以参考网址:http://www.h-ui.net/lib/datatables.js.shtml
二、代码
(1)引入相关文件

<script type="text/javascript" src="../../lib/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="../../lib/layer/2.4/layer.js"></script>
<script type="text/javascript" src="../../static/h-ui/js/H-ui.min.js"></script>
<script type="text/javascript" src="../../static/h-ui.admin/js/H-ui.admin.js"></script>
<script type="text/javascript" src="../../lib/datatables/1.10.0/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="../../lib/laypage/1.2/laypage.js"></script>

表格html

<table class="table table-border table-bordered table-bg table-hover  table-responsive  table-sort">
     <thead>
      <tr class="text-c">
       <th>id</th>
       <th>客户名称</th>
       <th>客户名称</th>
       <th>客户名称</th>
       <th>客户名称</th>
       <th>客户名称</th>
       <th>操作</th>
      </tr>
     </thead>
     <tbody id="cuslist">
     </tbody>
  </table>

(2)主要的js部分,含ajax数据请求

$(document).ready(function() {
    
    
 var htmlList='';
 $.ajax({
    
    
  type: "GET",
  url: "url接口",
  async: true,
 data: {
    
    
  },
  success: function(res) {
    
    
   console.log(res);
   for(i = 0; i < res.length; i++) {
    
    
    htmlList += '<tr class="text-c"><td>'+res[i].userName+'</td><td>'+res[i].contactName+'</td><td>'+res[i].contactPhone+'</td><td>'+res[i].contactAddr+'</td>'
   }
   $('#cuslist').html(htmlList);
   $('.table-sort').dataTable({
    
    }); //这个是主要的,进行分页
 
  },
  error: function(res) {
    
    
  },
 });



});

猜你喜欢

转载自blog.csdn.net/huang_jimei/article/details/109649105