更新:添加自定义分页

virtual
zwesy 2020-12-07 09:52:03 +08:00
parent 2ba5674952
commit 837a178343
3 changed files with 177 additions and 0 deletions

5
app/provider.php Normal file
View File

@ -0,0 +1,5 @@
<?php
// 全局容器对象绑定文件
return [
'think\Paginator' => 'tool\CustomPageHelper'
];

View File

@ -0,0 +1,164 @@
<?php
namespace tool;
use think\Paginator;
/**
* 自定义分页模板
* Class CustomPageHelper
* @package tool
*/
class CustomPageHelper extends Paginator
{
// 中间页码按钮个数
protected $btnNumber = 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 = "&laquo;")
{
if ($this->currentPage() <= 1) {
return $this->getDisabledTextWrapper($text, 'prev');
}
$url = $this->url(
$this->currentPage() - 1
);
return $this->getPageLinkWrapper($url, $text, 'prev');
}
// 页码
protected function getLinks()
{
if ($this->total > $this->listRows) {
if ($this->lastPage < $this->btnNumber) {
return $this->getUrlLinks($this->getUrlRange(1, $this->lastPage), false);
} else {
$min = 1;
if ($this->currentPage > $this->btnNumber / 2) $min = $this->currentPage - floor($this->btnNumber / 2);
if ($this->lastPage - $this->currentPage < $this->btnNumber / 2) $min = $this->lastPage - $this->btnNumber + 1;
$max = $min + $this->btnNumber - 1;
$ellipsis = false;
if ($this->lastPage > $max) {
$ellipsis = true;
$max = $max - 1;
}
return $this->getUrlLinks($this->getUrlRange($min, $max), $ellipsis);
}
}
}
// 下一页
protected function getNextButton($text = '&raquo;')
{
if (!$this->hasMore) {
return $this->getDisabledTextWrapper($text, 'next');
}
$url = $this->url($this->currentPage() + 1);
return $this->getPageLinkWrapper($url, $text, 'next');
}
// 末页
protected function getLastButton($text = '')
{
if (!$this->hasMore) {
return $this->getDisabledTextWrapper($text);
}
$url = $this->url($this->lastPage());
return $this->getPageLinkWrapper($url, $text);
}
// 渲染分页
public function render($type = '')
{
//数据是否足够分页
if ($this->hasPages()) {
if(strtoupper($type) == 'A') {
return sprintf(
'<ul class="pageing">%s %s %s %s</ul>',
$this->getFirstButton('首页'),
$this->getPreviousButton('上一页'),
$this->getLinks(),
$this->getNextButton('下一页')
);
} elseif (strtoupper($type) == 'B') {
return sprintf(
'<ul class="pageing">%s %s %s %s %s</ul>',
$this->getFirstButton('首页'),
$this->getPreviousButton('上一页'),
$this->getLinks(),
$this->getNextButton('下一页'),
$this->getLastButton('末页')
);
} else {
return sprintf(
'<ul class="pageing">%s %s %s</ul>',
$this->getPreviousButton('上一页'),
$this->getLinks(),
$this->getNextButton('下一页')
);
}
}
}
// 生成禁用按钮
protected function getDisabledTextWrapper($text, $htmlClass = '')
{
return '<li class="disabled '.$htmlClass.'"><span>' . $text . '</span></li>';
}
// 生成普通按钮
protected function getPageLinkWrapper($url, $page, $htmlClass = '')
{
if ($page == $this->currentPage()) {
return $this->getActivePageWrapper($page, $htmlClass);
}
return $this->getAvailablePageWrapper($url, $page, $htmlClass);
}
// 生成当前页按钮
protected function getActivePageWrapper($text, $htmlClass = '')
{
return '<li class="cur "'.$htmlClass.'><span>' . $text . '</span></li>';
}
// 可点击按钮
protected function getAvailablePageWrapper($url, $page, $htmlClass = '')
{
return '<li class="'.$htmlClass.'"><a href="' . htmlentities($url) . '">' . $page . '</a></li>';
}
// 批量生成页码按钮
protected function getUrlLinks(array $urls, bool $ellipsis, $htmlClass = '')
{
$html = '';
foreach ($urls as $page => $url) {
$html .= $this->getPageLinkWrapper($url, $page, $htmlClass);
}
if($ellipsis) {
$html .= $this->getDisabledTextWrapper('...', $htmlClass);
}
return $html;
}
}

View File

@ -346,6 +346,14 @@ input,select,textarea{outline:medium none; resize: none;}
.product-box .center-block ul li a:hover span img{ transform: scale(1.1);}
.pager{ margin: 3rem 0 0;}
.pageing{ display: flex; justify-content: center; flex-wrap: wrap;}
.pageing li{ display: inline-block;}
.pageing li a,.pageing li span{ display: block; min-width: 14px; border: 1px solid #d1d1d1; background: #fff; text-align: center; font-size: 14px; color: #565656; margin: 0 3px; transition: all 0.5s;}
.pageing li a:hover{ color: #0677ce;}
.pageing li.prev a:hover, .pageing li.next a:hover{ color: #fff; background: #999; border-color: #999;}
.pageing li.cur a,.pageing li.cur span{ background: #0677ce; border-color: #0677ce; color: #fff;}
.pageing li.cur a:hover{ color: #fff;}
.pageing li.disabled{ cursor: not-allowed;}
.search-form-box .layui-form{ padding: 0 20%;}
.search-form-box .layui-form .layui-input,.product-box .layui-form .layui-form-select{ width: 100%; margin: 0 15px;}