站長資訊網(wǎng)
        最全最豐富的資訊網(wǎng)站

        詳解thinkphp下部分內(nèi)容的ajax無刷新分頁

        下面thinkphp框架教程欄目將給大家介紹thinkphp下頁面內(nèi)部分內(nèi)容的ajax無刷新分頁,希望對需要的朋友有所幫助!

        thinkphp框架自帶的分頁類是每次翻頁都要刷新一下整個(gè)頁面,這種翻頁的用戶體驗(yàn)顯然是不太理想的,我們希望每次翻頁只刷新我們想要的數(shù)據(jù)集部分的數(shù)據(jù),這樣我們很容易想到ajax異步通信,用ajax與數(shù)據(jù)庫(本人在開發(fā)過程中使用的是mysql數(shù)據(jù)庫)異步交互,將從數(shù)據(jù)庫查詢的數(shù)據(jù)返回,用jquery替換原有的數(shù)據(jù),從而在不刷新這個(gè)頁面的情況下進(jìn)行局部刷新,從而達(dá)到我們預(yù)期的效果。
        thinkphp ajax 分頁類
        這個(gè)分頁類是網(wǎng)上找到的資源,大家可以直接在自己的thinkphp里創(chuàng)建這么一個(gè)類,我這里類名是 AjaxPage.class.php,如有需要,可修改命名空間

        <?php  namespace Think; class AjaxPage { // 分頁欄每頁顯示的頁數(shù) public $rollPage = 5; // 頁數(shù)跳轉(zhuǎn)時(shí)要帶的參數(shù) public $parameter  ; // 默認(rèn)列表每頁顯示行數(shù) public $listRows = 20; // 起始行數(shù) public $firstRow ; // 分頁總頁面數(shù) protected $totalPages  ; // 總行數(shù) protected $totalRows  ; // 當(dāng)前頁數(shù) protected $nowPage    ; // 分頁的欄的總頁數(shù) protected $coolPages   ; // 分頁顯示定制 protected $config  = array('header'=>'條記錄','prev'=>'上一頁','next'=>'下一頁','first'=>'第一頁','last'=>'最后一頁','theme'=>' %totalRow% %header% %nowPage%/%totalPage% 頁 %upPage% %downPage% %first%  %prePage%  %linkPage%  %nextPage% %end%'); // 默認(rèn)分頁變量名 protected $varPage;   public function __construct($totalRows,$listRows='',$ajax_func,$parameter='') {     $this->totalRows = $totalRows;     $this->ajax_func = $ajax_func;     $this->parameter = $parameter;     $this->varPage = C('VAR_PAGE') ? C('VAR_PAGE') : 'p' ;     if(!empty($listRows)) {         $this->listRows = intval($listRows);     }     $this->totalPages = ceil($this->totalRows/$this->listRows);     //總頁數(shù)     $this->coolPages  = ceil($this->totalPages/$this->rollPage);     $this->nowPage  = !empty($_GET[$this->varPage])?intval($_GET[$this->varPage]):1;     if(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {         $this->nowPage = $this->totalPages;     }     $this->firstRow = $this->listRows*($this->nowPage-1); }   public function nowpage($totalRows,$listRows='',$ajax_func,$parameter='') {     $this->totalRows = $totalRows;     $this->ajax_func = $ajax_func;     $this->parameter = $parameter;     $this->varPage = C('VAR_PAGE') ? C('VAR_PAGE') : 'p' ;     if(!empty($listRows)) {         $this->listRows = intval($listRows);     }     $this->totalPages = ceil($this->totalRows/$this->listRows);     //總頁數(shù)     $this->coolPages  = ceil($this->totalPages/$this->rollPage);     $this->nowPage  = !empty($_GET[$this->varPage])?intval($_GET[$this->varPage]):1;     if(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {         $this->nowPage = $this->totalPages;     }     $this->firstRow = $this->listRows*($this->nowPage-1);      return $this->nowPage; }  public function setConfig($name,$value) {     if(isset($this->config[$name])) {         $this->config[$name]    =   $value;     } }  public function show() {     if(0 == $this->totalRows) return '';     $p = $this->varPage;     $nowCoolPage      = ceil($this->nowPage/$this->rollPage);     $url  =  $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$this->parameter;     $parse = parse_url($url);     if(isset($parse['query'])) {         parse_str($parse['query'],$params);         unset($params[$p]);         $url   =  $parse['path'].'?'.http_build_query($params);     }     //上下翻頁字符串     $upRow   = $this->nowPage-1;     $downRow = $this->nowPage+1;     if ($upRow>0){         $upPage="<a class='ajaxify' id='big' href='JavaScript:;' onclick='".$this->ajax_func."(".$upRow.")'>".$this->config['prev']."</a>";     }else{         $upPage="";     }      if ($downRow <= $this->totalPages){         $downPage="<a class='ajaxify' id='big' href='JavaScript:;' onclick='".$this->ajax_func."(".$downRow.")'>".$this->config['next']."</a>";     }else{         $downPage="";     }     // << < > >>     if($nowCoolPage == 1){         $theFirst = "";         $prePage = "";     }else{         $preRow =  $this->nowPage-$this->rollPage;         $prePage = "<a class='ajaxify' id='big' href='JavaScript:;' onclick='".$this->ajax_func."(".$preRow.")'>上".$this->rollPage."頁</a>";         $theFirst = "<a class='ajaxify' id='big' href='JavaScript:;' onclick='".$this->ajax_func."(1)' >".$this->config['first']."</a>";     }     if($nowCoolPage == $this->coolPages){         $nextPage = "";         $theEnd="";     }else{         $nextRow = $this->nowPage+$this->rollPage;         $theEndRow = $this->totalPages;         $nextPage = "<a class='ajaxify' id='big' href='JavaScript:;' onclick='".$this->ajax_func."(".$nextRow.")' >下".$this->rollPage."頁</a>";         $theEnd = "<a class='ajaxify' id='big' href='JavaScript:;' onclick='".$this->ajax_func."(".$theEndRow.")' >".$this->config['last']."</a>";     }     // 1 2 3 4 5     $linkPage = "";     for($i=1;$i<=$this->rollPage;$i++){         $page=($nowCoolPage-1)*$this->rollPage+$i;         if($page!=$this->nowPage){             if($page<=$this->totalPages){                $linkPage .= "&nbsp;<a class='ajaxify' id='big' href='JavaScript:;' onclick='".$this->ajax_func."(".$page.")'>&nbsp;".$page."&nbsp;</a>";             }else{                 break;             }         }else{             if($this->totalPages != 1){                 $linkPage .= "&nbsp;<span class='current'>".$page."</span>";             }         }     }     $pageStr  =  str_replace(         array('%header%','%nowPage%','%totalRow%','%totalPage%','%upPage%','%downPage%','%first%','%prePage%','%linkPage%','%nextPage%','%end%'),         array($this->config['header'],$this->nowPage,$this->totalRows,$this->totalPages,$upPage,$downPage,$theFirst,$prePage,$linkPage,$nextPage,$theEnd),$this->config['theme']);     return $pageStr; }  }

        具體步驟
        1.控制器
        現(xiàn)在index方法里display,再在page方法里fetch,ajaxReturn,這里要注意fetch的是引用頁(article)

        <?php namespace HomeController; use ThinkController; class IndexController extends Controller {     public function index(){         $info=M('info');         //統(tǒng)計(jì)要查詢數(shù)據(jù)的數(shù)量         $count=$info->count();         //實(shí)例化分頁類,傳入三個(gè)參數(shù),分別是數(shù)據(jù)總數(shù)、每頁顯示的數(shù)據(jù)條數(shù)、要調(diào)用的jQuery ajax方法名         $p=new ThinkAjaxPage($count,4,'server');         //產(chǎn)生分頁信息         $page=$p->show();         //要查詢的數(shù)據(jù),limit表示每頁查詢的數(shù)量,這里為4條         $data = $info->limit($p->firstRow.','.$p->listRows)->select();         //assign方法往模板賦值         $this->assign('list',$data);         $this->assign('page',$page); //        $res["content"] = $this->fetch('Index/index'); //        $this->ajaxReturn($res);         $this->display();     }     public function page(){         //實(shí)例化數(shù)據(jù)模型         $info=M('info');         //統(tǒng)計(jì)要查詢數(shù)據(jù)的數(shù)量         $count=$info->count();         //實(shí)例化分頁類,傳入三個(gè)參數(shù),分別是數(shù)據(jù)總數(shù)、每頁顯示的數(shù)據(jù)條數(shù)、要調(diào)用的jQuery ajax方法名         $p=new ThinkAjaxPage($count,4,'server');         //產(chǎn)生分頁信息         $page=$p->show();         //要查詢的數(shù)據(jù),limit表示每頁查詢的數(shù)量,這里為4條         $data = $info->limit($p->firstRow.','.$p->listRows)->select();         //assign方法往模板賦值         $this->assign('list',$data);         $this->assign('page',$page);         //ajax返回信息         $res["content"] = $this->fetch('Index/article');         $this->ajaxReturn($res);     } }

        2.模板
        柱模板 index.html

        其中,引用的模板為需要分頁的這部分內(nèi)容

        <!DOCTYPE html> <html> <head> </head> <p>     hello world </p> <include file="Index/article" /> <script>     function server(pid){           var pid = pid;         $.get("{:U('Index/page')}", "p="+pid, function(data){                $("#server").replaceWith("<p  id='server'>"              +data.content+              "</p>");          });     } </script>  <script src="__PUBLIC__/jq/jquery1.8.3.min.js"></script> </html>

        需要分頁的模板

        article.html

           <div id="server">         <div class="row-fluid"  >             <div class="span12">                 <div class="portlet box green">                     <div class="portlet-title">                         <div class="caption"><i class="icon-globe"></i>信息列表</div>                     </div>                      <div class="portlet-body" >                                          <table class="table table-striped table-bordered table-hover table-full-width" id="sample_1">                              <thead>                                  <tr>                                     <th class="hidden-480">a</th>                                     <th class="hidden-480">b</th>                                     <th class="hidden-480">c</th>                                     <th class="hidden-480">d</th>                                 </tr>                              </thead>                              <tbody>                                     //循環(huán)賦值                                     <foreach name='list' item='info'>                                         <tr>                                             <td>{$info.a}</td>                                             <td>{$info.b}</td>                                                  <td>{$info.c}</td>                                             <td>{$info.d}</td>                                                                                   </tr>                                     </foreach>                                                              </tbody>                                                      </table>                         //分頁信息                         <div class="row-fluid"> {$page} </div>                                              </div>                 </div>                 </div>                 </div> </div>

        這樣就可以保證,點(diǎn)擊頁碼的時(shí)候,不會(huì)導(dǎo)致分頁內(nèi)容上邊的內(nèi)容會(huì)再次加載一遍,造成網(wǎng)頁亂

        3.js部分
        這一步是實(shí)現(xiàn)ajax無刷新分頁的重點(diǎn),用到了jQuery的ajax通信,通過與數(shù)據(jù)庫的ajax交互,將獲取到的數(shù)據(jù)寫到模板中,替換掉之前的數(shù)據(jù)集,達(dá)到分頁的目的。server.js ,可寫在內(nèi)部也可寫在外部,自由選擇

         <script>     function server(pid){           var pid = pid;         $.get("{:U('Index/page')}", "p="+pid, function(data){                $("#server").replaceWith("<p  id='server'>"              +data.content+              "</p>");          });     } </script>

        這個(gè)方法名 server 就是控制器中實(shí)例化分頁類中傳的第三個(gè)參數(shù),每次在模板上點(diǎn)擊翻頁,都會(huì)觸發(fā)這個(gè)js方法server(p),里面?zhèn)鬟f的是第幾頁的頁碼。

        $p=new ThinkAjaxPage($count,4,'server');

        這里用到的是jQuery里ajax方法的.get形式進(jìn)行ajax與后臺通信,拿到返回的數(shù)據(jù)用replaceWith方法,用

        <div id='server'>+數(shù)據(jù)</div>

        替換模板中id為server的p,實(shí)現(xiàn)分頁效果。

        推薦學(xué)習(xí):《thinkphp視頻教程》

        贊(0)
        分享到: 更多 (0)
        網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號
        主站蜘蛛池模板: 国产欧美日韩精品丝袜高跟鞋| 欧美精品国产一区二区三区| 九九热这里只有国产精品| 国产在线精品免费aaa片| 久久久WWW成人免费精品| 久久精品一区二区国产| 亚洲精品和日本精品| 国产成人精品一区在线| 国产精品美女一区二区视频| 亚洲精品无码专区在线播放 | 久久精品国产亚洲一区二区三区| 欧美精品黑人巨大在线播放| 国产成人亚洲综合无码精品| 久久精品无码午夜福利理论片| 亚洲午夜福利精品无码| 久热这里只有精品99国产6| 亚洲av永久无码精品网站| 无码国产亚洲日韩国精品视频一区二区三区| 日韩欧美精品不卡| 国产精品ⅴ无码大片在线看| 亚洲国产精品一区第二页| 亚洲第一永久AV网站久久精品男人的天堂AV | 国内精品久久久久| 99精品在线观看| 99热在线日韩精品免费| 国产精品无码成人午夜电影| 久久永久免费人妻精品下载| 亚洲乱码国产乱码精品精| 亚洲精品老司机在线观看| 亚洲爆乳精品无码一区二区| 亚洲国产欧美日韩精品一区二区三区 | 四虎精品成人免费观看| 久久亚洲国产欧洲精品一| 久久96国产精品久久久| 精品国产欧美一区二区| 久久福利青草精品资源站免费| 97久久国产亚洲精品超碰热| 91精品在线看| 久久成人精品视频| 国产高清在线精品一区二区| 成人国产精品日本在线观看|