<?php
namespace app\extend;
use think\paginator\driver\Bootstrap;
class Pager extends Bootstrap {
protected $params = [
'home' => '首页',
'end' => '尾页',
'pre' => '',
'next' => '',
'show' => false //是否显示为自定义分页信息,默认不显示
];
public function setParams(array $params=[]){
foreach($params as $key=>$item){
if(array_key_exists($key, $this->params)){
$this->params[$key] = $item;
}
}
return $this;
}
protected function getPreviousButton($text = "«") {
$text = empty($this->params['pre']) ? $text : $this->params['pre'];
return parent::getPreviousButton($text); // TODO: Change the autogenerated stub
}
protected function getNextButton($text = '»') {
$text = empty($this->params['next']) ? $text : $this->params['next'];
return parent::getNextButton($text); // TODO: Change the autogenerated stub
}
/**
* 获取 共 多少 页信息
* @return string
*/
protected function getTotalInfo()
{
return '<li><span>共<strong>' . $this->lastPage() .'</strong>页 <strong>'.$this->total().'</strong> 条记录</span></li>';
}
protected function getFirstPage(){
if($this->currentPage != 1){
return '<li><a href="' . $this->url(1) . '">' . $this->params['home'] . '</a></li>';
}else{
return '<li><span>' . $this->params['home'] . '</span></li>';
}
}
protected function getLastPage(){
if($this->currentPage != $this->lastPage){
return '<li><a href="' . $this->url($this->lastPage) . '">' . $this->params['end'] . '</a></li>';
}else{
return '<li><span>' . $this->params['end'] . '</span></li>';
}
}
/**
* 渲染分页html
* @return mixed
*/
public function render()
{
if ($this->hasPages()) {
if ($this->simple) {
return sprintf(
'<ul class="pager">%s %s</ul>',
$this->getPreviousButton(),
$this->getNextButton()
);
} elseif ($this->params['show']){
return sprintf(
'<ul class="pagination">%s %s %s %s %s %s</ul>',
$this->getFirstPage(),
$this->getPreviousButton(),
$this->getLinks(),
$this->getNextButton(),
$this->getLastPage(),
$this->getTotalInfo()
);
} else {
return sprintf(
'<ul class="pagination">%s %s %s</ul>',
$this->getPreviousButton(),
$this->getLinks(),
$this->getNextButton()
);
}
}
}
}在控制器中调用:$list = Db::connect('dbConfig')->table('goods')->where('id', '<=', 50000)->paginate(10, false, [
'type' => '\app\extend\Pager',
]);
$list->setParams([
'show' => true,
'pre' => '上一页',
'next' => '下一页',
]);效果图:ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。