1.前言
作為一名php程序員,我們開發網站主要就是為了客戶從客戶端進行體驗,在這里,thinkphp框架自帶的分頁類是每次翻頁都要刷新一下整個頁面,這種翻頁的用戶體驗顯然是不太理想的,我們希望每次翻頁只刷新我們想要的數據集部分的數據,這樣可以給客戶帶來很好的體驗效果。那么在TP下如何進行ajax無刷新分頁呢?
1.1建立ajax分頁
在TP框架的ThinkPHP\Library\Think文件夾下,有框架自己的page.class.php,我們新建一個Ajaxpage.class.php,下面這個類是我實際用到項目中的
<?php namespace Common\Common; class AjaxPage { // 分頁欄每頁顯示的頁數 public $rollPage = 5; // 頁數跳轉時要帶的參數 public $parameter ; // 默認列表每頁顯示行數 public $listRows = 20; // 起始行數 public $firstRow ; // 分頁總頁面數 protected $totalPages ; // 總行數 protected $totalRows ; // 當前頁數 protected $nowPage ; // 分頁的欄的總頁數 protected $coolPages ; // 分頁顯示定制 protected $config = array('header'=>'條記錄','prev'=>'上一頁','next'=>'下一頁','first'=>'第一頁','last'=>'最后一頁',
'theme'=>' %totalRow% %header% %nowPage%/%totalPage% 頁 %upPage% %downPage% %first% %prePage% %linkPage% %nextPage% %end%'); // 默認分頁變量名 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); //總頁數 $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); //總頁數 $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:".$this->ajax_func."(".$upRow.")'>".$this->config['prev']."</a>"; }else{ $upPage=""; } if ($downRow <= $this->totalPages){ $downPage="<a class='ajaxify' id='big' href='javascript:".$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:".$this->ajax_func."(".$preRow.")'>上".$this->rollPage."頁</a>"; $theFirst = "<a class='ajaxify' id='big' href='javascript:".$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:".$this->ajax_func."(".$nextRow.")' >下".$this->rollPage."頁</a>"; $theEnd = "<a class='ajaxify' id='big' href='javascript:".$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 .= " <a class='ajaxify' id='big' href='javascript:".$this->ajax_func."(".$page.")'> ".$page." </a>"; }else{ break; } }else{ if($this->totalPages != 1){ $linkPage .= " <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; } } ?>
•在這個類中,我們可以根據自己項目的需求,修改我們項目的分頁情況$pageStr,默認列表每頁顯示行數等參數,非常方便,實際情況和page差不多,會用page,差不多就會改這個里面的很多參數,以及要寫入confi的配置項。
1.2 Controller處理
//實例化數據模型 $info=M('info'); //統計要查詢數據的數量 $count=$info->where($where)->count(); //實例化分頁類,傳入三個參數,分別是數據總數、每頁顯示的數據條數、要調用的jQuery ajax方法名 $p=new \Host\Common\AjaxPage($count,10,'index'); //產生分頁信息 $page=$p->show(); //要查詢的數據,limit表示每頁查詢的數量,這里為10條 $data = $server_info->where($where)->limit($p->firstRow.','.$p->listRows)->select(); //assign方法往模板賦值 $this->assign('list',$data); $this->assign('page',$page); //ajax返回信息,就是要替換的模板 $res["content"] = $this->fetch('Index/myinfolist') $this->ajaxReturn($res);
1.3建立並且渲染模板
myinfolist.html與要替換的模板一致。
我們建立一個html,通過控制器,把數據渲染到模板中->
然后通過 $res["content"] = $this->fetch('Index/myinfolist') 獲取模板->
最后通過js用渲染的模板去替換掉要替換的模板。
1.4JS部分
function index(id){
var id = id;
//把數據傳遞到要替換的控制器方法中
$.get('/index/myinfo', {'p':id}, function(data){
//用get方法發送信息到index中的myinfo方法
$("#server").replaceWith("<div id='user7'>"
+data.content+
"</div>");
});
}
//data.content的內容就是$res["content"] = $this->fetch('Index/myinfolist') 獲取的模板的內容
•這個js中的index方法,必須和$p中的第三個參數保持一致(注意,一定要一樣)
//實例化分頁類,傳入三個參數,分別是數據總數、每頁顯示的數據條數、要調用的jQuery ajax方法名 $p=new \Host\Common\AjaxPage($count,10,'index');