默认分页模板
![这里写图片描述](https://img-blog.csdn.net/20171122221310563?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMjAyMDM3NTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
自定义分页模板
![这里写图片描述](https://img-blog.csdn.net/20171122221532767?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMjAyMDM3NTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
<?php
namespace tool;
use think\Paginator;
class TpPageHelper extends Paginator
{
protected $bnumber = 10;
protected function getfirstButton($str = '')
{
if ($this->currentPage() <= 1) {
return $this->getDisabledTextWrapper($str);
}
$url = $this->url(1);
return $this->getPageLinkWrapper($url, $str);
}
protected function getPreviousButton($text = "«")
{
if ($this->currentPage() <= 1) {
return $this->getDisabledTextWrapper($text);
}
$url = $this->url(
$this->currentPage() - 1
);
return $this->getPageLinkWrapper($url, $text);
}
protected function getLinks()
{
if ($this->total > $this->listRows) {
if ($this->lastPage < $this->bnumber) {
return $this->getUrlLinks($this->getUrlRange(1, $this->lastPage));
} else {
$min = 1;
if ($this->currentPage > $this->bnumber / 2) $min = $this->currentPage - floor($this->bnumber / 2);
if ($this->lastPage - $this->currentPage < $this->bnumber / 2) $min = $this->lastPage - $this->bnumber + 1;
return $this->getUrlLinks($this->getUrlRange($min, $min + $this->bnumber - 1));
}
}
}
protected function getNextButton($text = '»')
{
if (!$this->hasMore) {
return $this->getDisabledTextWrapper($text);
}
$url = $this->url($this->currentPage() + 1);
return $this->getPageLinkWrapper($url, $text);
}
protected function getlastButton($text = '')
{
if (!$this->hasMore) {
return $this->getDisabledTextWrapper($text);
}
$url = $this->url($this->lastPage());
return $this->getPageLinkWrapper($url, $text);
}
public function render()
{
if ($this->hasPages()) {
return sprintf(
'<ul class="btn-item fr">%s %s %s %s %s</ul>',
$this->getfirstButton('首页'),
$this->getPreviousButton('上一页'),
$this->getLinks(),
$this->getNextButton('下一页'),
$this->getlastButton('末页')
);
}
}
protected function getDisabledTextWrapper($text)
{
return '<li class="disabled"><span>' . $text . '</span></li>';
}
protected function getPageLinkWrapper($url, $page)
{
if ($page == $this->currentPage()) {
return $this->getActivePageWrapper($page);
}
return $this->getAvailablePageWrapper($url, $page);
}
protected function getActivePageWrapper($text)
{
return '<li class="active"><span>' . $text . '</span></li>';
}
protected function getAvailablePageWrapper($url, $page)
{
return '<li><a href="' . htmlentities($url) . '">' . $page . '</a></li>';
}
protected function getUrlLinks(array $urls)
{
$html = '';
foreach ($urls as $page => $url) {
$html .= $this->getPageLinkWrapper($url, $page);
}
return $html;
}
}
- 将 TpPageHelper.php 放在extend\tool下
![这里写图片描述](https://img-blog.csdn.net/20171122221617485?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMjAyMDM3NTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
![这里写图片描述](https://img-blog.csdn.net/20171122221637528?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMjAyMDM3NTU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
相关阅读