TP5.0 调用bootstrap分页类显示分页
一.首先将分页类文件放到 thinkphp\library\think下,记住要给分页类命名空间namespace think;
<?php namespace think; class Page { public $page; //当前页 public $total; //总记录数 public $listRows; //每页显示记录数 private $uri;//动态url public $pageNum; //总页数 private $listNum=6;//显示页码按钮数量 public $render;//分页后的html模板 public $data;//分页后渲染到模板的数据 /* * 初始化分页数据 *$sdata 待分页的数据 * $listRows 每页记录数 */ public function __construct($sdata, $listRows=15){ $this->total=count($sdata); $this->listRows=$listRows; $this->uri=$this->getUri(); $this->page=!empty($_GET["page"]) ? $_GET["page"] : 1; $this->pageNum=ceil($this->total/$this->listRows); $this->render=$this->pageHtml(); $this->data=array_slice($sdata,($this->page-1)*$this->listRows,$listRows); return $this->data; } //动态获取url private function getUri(){ $url=$_SERVER["REQUEST_URI"].(strpos($_SERVER["REQUEST_URI"], '?')?'':"?"); $parse=parse_url($url); if(isset($parse["query"])){ parse_str($parse['query'],$params); unset($params["page"]); $url=$parse['path'].'?'.http_build_query($params); } return $url; } //首页 private function first(){ $html = ""; if($this->page==1) $html.=" <a style='magin=10px;' class='current btn disabled'>首 页</a>"; else $html.=" <a class='btn btn-primary-outline' href='{$this->uri}&page=1'>首 页</a>"; return $html; } //上一页 private function prev(){ $html = ""; if($this->page==1) $html.=" <a class='current btn disabled'>上一页</a>"; else $html.=" <a class='btn btn-primary-outline' href='{$this->uri}&page=".($this->page-1)."'>上一页</a>"; return $html; } //页码按钮 private function pageList(){ $linkPage=""; $inum=floor($this->listNum/2); for($i=$this->page-$inum;$i<=$this->page+$inum;$i++){ if($i<=0){ continue; } if($i>$this->pageNum){ continue; } if($i == $this->page){ $linkPage.=" <a class='current btn btn-secondary'>{$i}</a>"; }else{ $linkPage.=" <a class='btn btn-primary-outline' href='{$this->uri}&page={$i}'>{$i}</a>"; } } return $linkPage; } //下一页 private function next(){ $html = ""; if($this->page==$this->pageNum) $html.=" <a class='current btn disabled'>下一页</a>"; else $html.=" <a class='btn btn-primary-outline' href='{$this->uri}&page=".($this->page+1)."'>下一页</a>"; return $html; } //尾页 private function last(){ $html = ""; if($this->page==$this->pageNum) $html.=" <a class='current btn disabled'>尾 页</a>"; else $html.=" <a class='btn btn-primary-outline' href='{$this->uri}&page=".($this->pageNum)."'>尾 页</a>"; return $html; } //输入指定页码 private function goPage(){ return ' <input class="input-text" type="text" onkeydown="javascript:if(event.keyCode==13){var page=(this.value>'.$this->pageNum.')?'.$this->pageNum.':this.value;location=\''.$this->uri.'&page=\'+page+\'\'}" value="'.$this->page.'" style="width:52px"><input class="btn btn-secondary" type="button" value="GO" onclick="javascript:var page=(this.previousSibling.value>'.$this->pageNum.')?'.$this->pageNum.':this.previousSibling.value;location=\''.$this->uri.'&page=\'+page+\'\'"> '; } //选择指定页码 function selectPage(){ $inum=10; $location = $this->uri.'&page='; $selectPage ="<span class='va-m'>到第 </span> <span class='select-box' style='width:initial'><select class='select' name='topage' size='1' onchange='window.location=\"$location\"+this.value'>"; for($i=$this->page-$inum;$i<=$this->page+$inum;$i++){ if($i<=0){ continue; } if($i>$this->pageNum){ continue; } if($i == $this->page){ $selectPage .="<option value='$i' selected>$i</option>"; }else{ $selectPage .="<option value='$i'>$i</option>"; } } $selectPage .="</select></span> <span class='va-m'>页</span>"; return $selectPage; } //组装分页的html模板 function pageHtml(){ $html = "<div class='cl mt-20 text-c'>"; // $html .= "<span class='pr-20 va-m'>共有<b>{$this->total}</b>条记录</span>"; // $html .= "<span class='pr-20 va-m'>每页显示<b>{$this->listRows}</b>条</span>"; // $html .= "<span class='pr-20 va-m'><b>当前{$this->page}/{$this->pageNum}</b>页</span>"; $html .= $this->first(); $html .= $this->prev(); $html .= $this->pageList(); $html .= $this->next(); $html .= $this->last(); $html .= $this->goPage(); $html .= $this->selectPage(); $html .= '</div>'; return $html; } }
二.控制器页面 调用分页类use \think\Page;
<?php namespace app\index\controller; use \think\View; use \think\Controller; use \think\Db; use \think\Session; use \think\Page; class Index { public function index() { $shuju = Db::table('user')->select(); $p = new Page($shuju,10); //新建分页类,查询10条 $sj= $p->data; $fenye = $p ->pageHtml(); $n = new View(); $n->assign("shuju",$sj); $n ->assign("yema",$fenye); return $n->fetch(); } }
三.视图层页面
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <link type="text/css" rel="stylesheet" href="__CSS__/bootstrap.min.css" /> <link rel="stylesheet" type="text/css" href="__CSS__/reset.css" /> <link rel="stylesheet" type="text/css" href="__CSS__/style.css" /> <script src="__JS__/jquery-3.2.0.min.js" type="text/javascript" ></script> </head> <body> <table class="table table-hover" width="70%"> <!--border="1" bordercolor="#009BD8" align="center" cellpadding="0" cellspacing="1" id="datatable" style="font-size:16px;"--> <thead> <tr> <td style="background-color:#EBF4FE;">用户名</td> <td style="background-color:#EBF4FE;">姓名</td> <td style="background-color:#EBF4FE;">性别</td> <td style="background-color:#EBF4FE;">联系电话</td> <td style="background-color:#EBF4FE;">身份证号</td> <td style="background-color:#EBF4FE;">地区</td> <td style="background-color:#EBF4FE;">部门</td> <td style="background-color:#EBF4FE;">录入人</td> <td style="background-color:#EBF4FE;">推荐人</td> <td style="background-color:#EBF4FE;">督训人</td> <td style="background-color:#EBF4FE;">操作</td> </tr> </thead> <tbody> {volist name="shuju" id="v"} <tr> <td>{$v.ygid}</td> <td>{$v.ygname}</td> <td>{$v.sex}</td> <td>{$v.tel}</td> <td>{$v.sfid}</td> <td>{$v.diqu}</td> <td>{$v.bm}</td> <td>{$v.lrr}</td> <td>{$v.tjrname}</td> <td>{$v.dxname}</td> <td> <a href="ygxq? ygid={$v.ygid}">更多详情</a> </tr> {/volist} </tbody> </table> <br /><br /> <div>{$yema}</div> </body> </html>
效果如下: