当前位置:首页 > CMS教程 > Thinkphp > 列表

ThinkPHP5.1+Ajax实现的无刷新分页功能示例

发布:smiling 来源: PHP粉丝网  添加日期:2022-02-10 09:51:26 浏览: 评论:0 

本文实例讲述了ThinkPHP5.1+Ajax实现的无刷新分页功能,分享给大家供大家参考,具体如下:

无刷新分页可以减轻服务器负担,利用Ajax技术,请求部分信息,提高网站访问速度,是网站建设的必备技术。

需要在后台展示自定义属性列表(lst.html),其中的列表部分摘出来,放到(paginate1.html)中:

  1. <div class="row"> 
  2.   <div class="col-sm-12"> 
  3.     <div class="ibox float-e-margins"> 
  4.       <div class="ibox-content"> 
  5.           <table class="table table-bordered"> 
  6.             <thead> 
  7.               <tr> 
  8.                 <th>ID</th> 
  9.                 <th>名称</th> 
  10.                 <th>取值</th> 
  11.                 <th>显示</th> 
  12.                 <th>排序</th> 
  13.                 <th>操作</th> 
  14.               </tr> 
  15.             </thead> 
  16.             <tbody> 
  17.               {volist name="self" id="vo"
  18.               <tr> 
  19.                 <td>{$vo.id}</td> 
  20.                 <td>{$vo.name}</td> 
  21.                 <td>{$vo.value}</td> 
  22.                 <td> 
  23.                   {if $vo.isshow==1} 
  24.                   <button type="button" class="btn btn-success btn-sm"></button> 
  25.                   {else/} 
  26.                   <button type="button" class="btn btn-danger btn-sm"></button> 
  27.                   {/if} 
  28.                 </td> 
  29.                 <td><input type="text" value="{$vo.order}" name=""></td> 
  30.                 <td> 
  31.                   <div class="btn-group open"> 
  32.                     <button data-toggle="dropdown" class="btn btn-primary dropdown-toggle" aria-expanded="true">操作 <span class="caret"></span> 
  33.                     </button> 
  34.                     <ul class="dropdown-menu"> 
  35.                       <li><a href="">修改</a> 
  36.                       </li> 
  37.                       <li><a href="">删除</a> 
  38.                       </li> 
  39.                     </ul> 
  40.                   </div> 
  41.                 </td> 
  42.               </tr> 
  43.               {/volist} 
  44.             </tbody> 
  45.           </table> 
  46.         {$self|raw} 
  47.         <div class="row"> 
  48.           <div class="col-sm-2"> 
  49.             <button class="btn btn-success" type="button" id="changeOrder"> 
  50.               <i class="fa fa-plus-square"></i>&nbsp;&nbsp; 
  51.               <span class="bold">排序</span> 
  52.             </button> 
  53.           </div> 
  54.         </div> 
  55.       </div> 
  56.     </div> 
  57.   </div> 
  58. </div> 

其中self是服务器端传递过来的自定义属性,并进行了分页操作:

$selfattribute_select = db("selfattribute")->paginate(5);

$this->assign("self",$selfattribute_select);

因为lst.html把列表摘了出来,所以还要在引入回去,才能使页面完整,同时,为了方便进行jquery操作,把列表用带id的div包裹起来:

  1. <div id="paginate"> 
  2.     {include file="selfattribute/paginate1"
  3. </div> 

ThinkPHP5.1带的分页类使用的是BootStrap样式,它在页面显示时实际会有一个pagination的类,查看源代码如下:

  1. <ul class="pagination"> 
  2.   <li class="disabled"> 
  3.     <span>&laquo;</span></li> 
  4.   <li class="active"> 
  5.     <span>1</span></li> 
  6.   <li> 
  7.     <a href="/xkershouche/public/admin/selfattribute/lst.html?page=2" rel="external nofollow" rel="external nofollow" >2</a></li> 
  8.   <li> 
  9.     <a href="/xkershouche/public/admin/selfattribute/lst.html?page=3" rel="external nofollow" >3</a></li> 
  10.   <li> 
  11.     <a href="/xkershouche/public/admin/selfattribute/lst.html?page=4" rel="external nofollow" >4</a></li> 
  12.   <li> 
  13.     <a href="/xkershouche/public/admin/selfattribute/lst.html?page=5" rel="external nofollow" >5</a></li> 
  14.   <li> 
  15.     <a href="/xkershouche/public/admin/selfattribute/lst.html?page=6" rel="external nofollow" >6</a></li> 
  16.   <li> 
  17.     <a href="/xkershouche/public/admin/selfattribute/lst.html?page=2" rel="external nofollow" rel="external nofollow" >&raquo;</a></li> 
  18. </ul> 

这就是好多人搞不懂的pagination是怎么来的。

然后开始写js代码,因为我们的分页按钮也在被请求的页面当中,属于“未来”的元素,所以这里我们要用on方法,这个方法是jquery1.7以后的方法,注意自己的jquery版本。

  1. <script type="text/javascript"
  2.   $(document).on('click''.pagination a'function(event) { 
  3.     var url = $(this).attr('href'); 
  4.     $.ajax({ 
  5.       url: url, 
  6.       type: 'get'
  7.     }) 
  8.     .done(function(data) { 
  9.       $("#paginate").html(data); 
  10.     }) 
  11.     return false
  12.   }); 
  13.   </script> 

其中.done()方法和success方法是一样的,return false是为了阻止默认事件,防止直接跳转。

那么服务器端就可以根据情况渲染模板了,代码如下:

  1. public function lst() 
  2.   { 
  3.     $selfattribute_select = db("selfattribute")->paginate(5); 
  4.     $this->assign("self",$selfattribute_select); 
  5.     if (request()->isAjax()) { 
  6.       return view("paginate1"); 
  7.     } else { 
  8.       return view(); 
  9.     } 
  10.   }

Tags: ThinkPHP5.1 Ajax无刷新分页

分享到: