thinkphp フレームワークに付属するページング クラスでは、毎回ページ全体を更新する必要がありますページがめくられます。この種のページめくりのユーザー エクスペリエンスは明らかに理想的ではありません。ページをめくるたびに、必要なデータ セットの部分のデータのみが更新されることを望みます。このようにして、次のことができます。 ajax 非同期通信、ajax を使用してデータベースと通信する (私は開発中です。プロセスは mysql データベースを使用します) 非同期対話、データベースからクエリされたデータを返し、元のデータを jquery に置き換え、ページを更新せずに部分更新を実行することを簡単に考えられます。 、こうして望ましい効果を達成します。
thinkphp ajax ページング クラス
このページング クラスはインターネット上にあるリソースです。このようなクラスは独自の thinkphp で直接作成できます。ここでのクラス名は AjaxPage.class.php です。必要に応じて、名前空間を変更できます
<?php namespace Think; 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:;' 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 .= " <a class='ajaxify' id='big' href='JavaScript:;' onclick='".$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; } }
具体的な手順
1.Controller
インデックス メソッドで表示し、ページ メソッドでフェッチして ajaxReturn を実行するなどここに注目 取得するのは参照ページ(記事)
<?php namespace Home\Controller; use Think\Controller; class IndexController extends Controller { public function index(){ $info=M('info'); //统计要查询数据的数量 $count=$info->count(); //实例化分页类,传入三个参数,分别是数据总数、每页显示的数据条数、要调用的jQuery ajax方法名 $p=new \Think\AjaxPage($count,4,'server'); //产生分页信息 $page=$p->show(); //要查询的数据,limit表示每页查询的数量,这里为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(){ //实例化数据模型 $info=M('info'); //统计要查询数据的数量 $count=$info->count(); //实例化分页类,传入三个参数,分别是数据总数、每页显示的数据条数、要调用的jQuery ajax方法名 $p=new \Think\AjaxPage($count,4,'server'); //产生分页信息 $page=$p->show(); //要查询的数据,limit表示每页查询的数量,这里为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
このうち、参照しているテンプレートはコンテンツの一部です。ページ分割が必要です
<!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> //循环赋值 <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>
これにより、ページ番号をクリックすると、ページ分割コンテンツの上のコンテンツが確実に表示されます。再度読み込まれないため、Web ページがめちゃくちゃになります
3.js 部分
このステップは、ajax 非リフレッシュ ページングの実現に焦点を当てています。これは、jQuery の ajax 通信を使用します。データベースを作成すると、取得したデータがテンプレートに書き込まれ、以前のデータと置き換えられ、ページングの目的を達成するために設定されます。 server.js は、内部または外部に記述することができ、自由に選択できます
<script> function server(pid){ var pid = pid; $.get("{:U('Index/page')}", "p="+pid, function(data){ $("#server").replaceWith("<p id='server'>" +data.content+ "</p>"); }); } </script>
このメソッド ネーム サーバーは、コントローラーのインスタンス化されたページング クラスに渡される 3 番目のパラメーターです。ページごとにテンプレートをクリックすると、この jsメソッドserver(p)がトリガーされ、その中で渡されるページ番号。
$p=new \Think\AjaxPage($count,4,'server');
ここで使用されているのは、ajax とバックグラウンドの間で通信するための jQuery の ajax メソッドの .get 形式です。返されたデータを取得するには、replaceWith メソッドを使用し、
<div id='server'>+数据</div>
を使用します。テンプレート内のサーバーの ID を置き換えます。p、ページング効果を実現します。