详解thinkphp下部分内容的ajax无刷新分页

 3714

下面将给大家介绍thinkphp下页面内部分内容的ajax无刷新分页,希望对需要的朋友有所帮助!

thinkphp下页面内部分内容的ajax无刷新分页

thinkphp框架自带的分页类是每次翻页都要刷新一下整个页面,这种翻页的用户体验显然是不太理想的,我们希望每次翻页只刷新我们想要的数据集部分的数据,这样我们很容易想到ajax异步通信,用ajax与数据库(本人在开发过程中使用的是mysql数据库)异步交互,将从数据库查询的数据返回,用jquery替换原有的数据,从而在不刷新这个页面的情况下进行局部刷新,从而达到我们预期的效果。

thinkphp ajax 分页类

这个分页类是网上找到的资源,大家可以直接在自己的thinkphp里创建这么一个类,我这里类名是 AjaxPage.class.php,如有需要,可修改命名空间

  1. <?php
  2.  
  3. namespace Think;
  4. class AjaxPage {
  5. // 分页栏每页显示的页数
  6. public $rollPage = 5;
  7. // 页数跳转时要带的参数
  8. public $parameter  ;
  9. // 默认列表每页显示行数
  10. public $listRows = 20;
  11. // 起始行数
  12. public $firstRow ;
  13. // 分页总页面数
  14. protected $totalPages  ;
  15. // 总行数
  16. protected $totalRows  ;
  17. // 当前页数
  18. protected $nowPage    ;
  19. // 分页的栏的总页数
  20. protected $coolPages   ;
  21. // 分页显示定制
  22. protected $config  = array('header'=>'条记录','prev'=>'上一页','next'=>'下一页','first'=>'第一页','last'=>'最后一页','theme'=>' %totalRow% %header% %nowPage%/%totalPage% 页 %upPage% %downPage% %first%  %prePage%  %linkPage%  %nextPage% %end%');
  23. // 默认分页变量名
  24. protected $varPage;
  25.  
  26.  
  27. public function __construct($totalRows,$listRows='',$ajax_func,$parameter='') {
  28.     $this->totalRows = $totalRows;
  29.     $this->ajax_func = $ajax_func;
  30.     $this->parameter = $parameter;
  31.     $this->varPage = C('VAR_PAGE') ? C('VAR_PAGE') : 'p' ;
  32.     if(!empty($listRows)) {
  33.         $this->listRows = intval($listRows);
  34.     }
  35.     $this->totalPages = ceil($this->totalRows/$this->listRows);     //总页数
  36.     $this->coolPages  = ceil($this->totalPages/$this->rollPage);
  37.     $this->nowPage  = !empty($_GET[$this->varPage])?intval($_GET[$this->varPage]):1;
  38.     if(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {
  39.         $this->nowPage = $this->totalPages;
  40.     }
  41.     $this->firstRow = $this->listRows*($this->nowPage-1);
  42. }
  43.  
  44.  public function nowpage($totalRows,$listRows='',$ajax_func,$parameter='') {
  45.     $this->totalRows = $totalRows;
  46.     $this->ajax_func = $ajax_func;
  47.     $this->parameter = $parameter;
  48.     $this->varPage = C('VAR_PAGE') ? C('VAR_PAGE') : 'p' ;
  49.     if(!empty($listRows)) {
  50.         $this->listRows = intval($listRows);
  51.     }
  52.     $this->totalPages = ceil($this->totalRows/$this->listRows);     //总页数
  53.     $this->coolPages  = ceil($this->totalPages/$this->rollPage);
  54.     $this->nowPage  = !empty($_GET[$this->varPage])?intval($_GET[$this->varPage]):1;
  55.     if(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {
  56.         $this->nowPage = $this->totalPages;
  57.     }
  58.     $this->firstRow = $this->listRows*($this->nowPage-1);
  59.  
  60.     return $this->nowPage;
  61. }
  62.  
  63. public function setConfig($name,$value) {
  64.     if(isset($this->config[$name])) {
  65.         $this->config[$name]    =   $value;
  66.     }
  67. }
  68.  
  69. public function show() {
  70.     if(0 == $this->totalRows) return '';
  71.     $p = $this->varPage;
  72.     $nowCoolPage      = ceil($this->nowPage/$this->rollPage);
  73.     $url  =  $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$this->parameter;
  74.     $parse = parse_url($url);
  75.     if(isset($parse['query'])) {
  76.         parse_str($parse['query'],$params);
  77.         unset($params[$p]);
  78.         $url   =  $parse['path'].'?'.http_build_query($params);
  79.     }
  80.     //上下翻页字符串
  81.     $upRow   = $this->nowPage-1;
  82.     $downRow = $this->nowPage+1;
  83.     if ($upRow>0){
  84.         $upPage="<a class='ajaxify' id='big' href='JavaScript:;' onclick='".$this->ajax_func."(".$upRow.")'>".$this->config['prev']."</a>";
  85.     }else{
  86.         $upPage="";
  87.     }
  88.  
  89.     if ($downRow <= $this->totalPages){
  90.         $downPage="<a class='ajaxify' id='big' href='JavaScript:;' onclick='".$this->ajax_func."(".$downRow.")'>".$this->config['next']."</a>";
  91.     }else{
  92.         $downPage="";
  93.     }
  94.     // << < > >>
  95.     if($nowCoolPage == 1){
  96.         $theFirst = "";
  97.         $prePage = "";
  98.     }else{
  99.         $preRow =  $this->nowPage-$this->rollPage;
  100.         $prePage = "<a class='ajaxify' id='big' href='JavaScript:;' onclick='".$this->ajax_func."(".$preRow.")'>上".$this->rollPage."页</a>";
  101.         $theFirst = "<a class='ajaxify' id='big' href='JavaScript:;' onclick='".$this->ajax_func."(1)' >".$this->config['first']."</a>";
  102.     }
  103.     if($nowCoolPage == $this->coolPages){
  104.         $nextPage = "";
  105.         $theEnd="";
  106.     }else{
  107.         $nextRow = $this->nowPage+$this->rollPage;
  108.         $theEndRow = $this->totalPages;
  109.         $nextPage = "<a class='ajaxify' id='big' href='JavaScript:;' onclick='".$this->ajax_func."(".$nextRow.")' >下".$this->rollPage."页</a>";
  110.         $theEnd = "<a class='ajaxify' id='big' href='JavaScript:;' onclick='".$this->ajax_func."(".$theEndRow.")' >".$this->config['last']."</a>";
  111.     }
  112.     // 1 2 3 4 5
  113.     $linkPage = "";
  114.     for($i=1;$i<=$this->rollPage;$i++){
  115.         $page=($nowCoolPage-1)*$this->rollPage+$i;
  116.         if($page!=$this->nowPage){
  117.             if($page<=$this->totalPages){
  118.                $linkPage .= "&nbsp;<a class='ajaxify' id='big' href='JavaScript:;' onclick='".$this->ajax_func."(".$page.")'>&nbsp;".$page."&nbsp;</a>";
  119.             }else{
  120.                 break;
  121.             }
  122.         }else{
  123.             if($this->totalPages != 1){
  124.                 $linkPage .= "&nbsp;<span class='current'>".$page."</span>";
  125.             }
  126.         }
  127.     }
  128.     $pageStr  =  str_replace(
  129.         array('%header%','%nowPage%','%totalRow%','%totalPage%','%upPage%','%downPage%','%first%','%prePage%','%linkPage%','%nextPage%','%end%'),
  130.         array($this->config['header'],$this->nowPage,$this->totalRows,$this->totalPages,$upPage,$downPage,$theFirst,$prePage,$linkPage,$nextPage,$theEnd),$this->config['theme']);
  131.     return $pageStr;
  132. }
  133.  
  134. }


具体步骤

1、控制器

现在index方法里display,再在page方法里fetch,ajaxReturn,这里要注意fetch的是引用页(article)

  1. <?php
  2. namespace Home\Controller;
  3. use Think\Controller;
  4. class IndexController extends Controller {
  5.     public function index(){
  6.         $info=M('info');
  7.         //统计要查询数据的数量
  8.         $count=$info->count();
  9.         //实例化分页类,传入三个参数,分别是数据总数、每页显示的数据条数、要调用的jQuery ajax方法名
  10.         $p=new \Think\AjaxPage($count,4,'server');
  11.         //产生分页信息
  12.         $page=$p->show();
  13.         //要查询的数据,limit表示每页查询的数量,这里为4条
  14.         $data = $info->limit($p->firstRow.','.$p->listRows)->select();
  15.         //assign方法往模板赋值
  16.         $this->assign('list',$data);
  17.         $this->assign('page',$page);
  18. //        $res["content"] = $this->fetch('Index/index');
  19. //        $this->ajaxReturn($res);
  20.         $this->display();
  21.     }
  22.     public function page(){
  23.         //实例化数据模型
  24.         $info=M('info');
  25.         //统计要查询数据的数量
  26.         $count=$info->count();
  27.         //实例化分页类,传入三个参数,分别是数据总数、每页显示的数据条数、要调用的jQuery ajax方法名
  28.         $p=new \Think\AjaxPage($count,4,'server');
  29.         //产生分页信息
  30.         $page=$p->show();
  31.         //要查询的数据,limit表示每页查询的数量,这里为4条
  32.         $data = $info->limit($p->firstRow.','.$p->listRows)->select();
  33.         //assign方法往模板赋值
  34.         $this->assign('list',$data);
  35.         $this->assign('page',$page);
  36.         //ajax返回信息
  37.         $res["content"] = $this->fetch('Index/article');
  38.         $this->ajaxReturn($res);
  39.     }
  40. }


2、模板

柱模板 index.html

其中,引用的模板为需要分页的这部分内容

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. </head>
  5. <p>
  6.     hello world
  7. </p>
  8. <include file="Index/article" />
  9. <script>
  10.     function server(pid){  
  11.         var pid = pid;
  12.         $.get("{:U('Index/page')}", "p="+pid, function(data){  
  13.              $("#server").replaceWith("<p  id='server'>"
  14.              +data.content+
  15.              "</p>"); 
  16.         });
  17.     }
  18. </script> 
  19. <script src="__PUBLIC__/jq/jquery1.8.3.min.js"></script>
  20. </html>

需要分页的模板

article.html

  1.    <div id="server">
  2.         <div class="row-fluid"  >
  3.             <div class="span12">
  4.                 <div class="portlet box green">
  5.                     <div class="portlet-title">
  6.                         <div class="caption"><i class="icon-globe"></i>信息列表</div>
  7.                     </div>
  8.  
  9.                     <div class="portlet-body" >
  10.                  
  11.                         <table class="table table-striped table-bordered table-hover table-full-width" id="sample_1">
  12.  
  13.                             <thead>
  14.  
  15.                                 <tr>
  16.                                     <th class="hidden-480">a</th>
  17.                                     <th class="hidden-480">b</th>
  18.                                     <th class="hidden-480">c</th>
  19.                                     <th class="hidden-480">d</th>
  20.                                 </tr>
  21.  
  22.                             </thead>
  23.  
  24.                             <tbody>
  25.                                     //循环赋值
  26.                                     <foreach name='list' item='info'>
  27.                                         <tr>
  28.                                             <td>{$info.a}</td>
  29.                                             <td>{$info.b}</td>     
  30.                                             <td>{$info.c}</td>
  31.                                             <td>{$info.d}</td>
  32.                                           
  33.                                         </tr>
  34.                                     </foreach>
  35.                                  
  36.                             </tbody>
  37.                              
  38.                         </table>
  39.                         //分页信息
  40.                         <div class="row-fluid"> {$page} </div>
  41.                          
  42.                     </div>
  43.                 </div>    
  44.             </div>        
  45.         </div>
  46. </div>

这样就可以保证,点击页码的时候,不会导致分页内容上边的内容会再次加载一遍,造成网页乱


3、js部分

这一步是实现ajax无刷新分页的重点,用到了jQuery的ajax通信,通过与数据库的ajax交互,将获取到的数据写到模板中,替换掉之前的数据集,达到分页的目的。server.js ,可写在内部也可写在外部,自由选择

  1. <script>
  2.     function server(pid){  
  3.         var pid = pid;
  4.         $.get("{:U('Index/page')}", "p="+pid, function(data){  
  5.              $("#server").replaceWith("<p  id='server'>"
  6.              +data.content+
  7.              "</p>"); 
  8.         });
  9.     }
  10. </script>

这个方法名 server 就是控制器中实例化分页类中传的第三个参数,每次在模板上点击翻页,都会触发这个js方法server(p),里面传递的是第几页的页码。

  1. $p=new \Think\AjaxPage($count,4,'server');

这里用到的是jQuery里ajax方法的.get形式进行ajax与后台通信,拿到返回的数据用replaceWith方法,用

  1. <div id='server'>+数据</div>

替换模板中id为server的p,实现分页效果。


本文网址:https://www.zztuku.com/detail-10901.html
站长图库 - 详解thinkphp下部分内容的ajax无刷新分页
申明:本文转载于《segmentfault》,如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐