不同的项目有不同的风格,那么Thinkphp6如何根据自己的风格自定义分页条呢?具体步骤如下:
1.在app/lib/paginator目录(这个目录根据自己的情况建立)下创建WebPagination.php文件,编写自己的分页代码,以下代码仅供参考:
<?php
namespace app\lib\paginator;
use think\Paginator;
class WebPagination extends Paginator
{
/**
* 上一页按钮
* @param string $text
* @return string
*/
protected function getPreviousButton(string $text = "上一页"): string
{
if ($this->currentPage() <= 1) {
return $this->getDisabledTextWrapper($text);
}
$url = $this->url(
$this->currentPage() - 1
);
return $this->getPageLinkWrapper($url, $text);
}
/**
* 下一页按钮
* @param string $text
* @return string
*/
protected function getNextButton(string $text = '下一页'): string
{
if (!$this->hasMore) {
return $this->getDisabledTextWrapper($text);
}
$url = $this->url($this->currentPage() + 1);
return $this->getPageLinkWrapper($url, $text);
}
/**
* 页码按钮
* @return string
*/
protected function getLinks(): string
{
if ($this->simple) {
return '';
}
$block = [
'first' => null,
'slider' => null,
'last' => null,
];
$side = 3;
$window = $side * 2;
if ($this->lastPage < $window + 6) {
$block['first'] = $this->getUrlRange(1, $this->lastPage);
} elseif ($this->currentPage <= $window) {
$block['first'] = $this->getUrlRange(1, $window + 2);
$block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
} elseif ($this->currentPage > ($this->lastPage - $window)) {
$block['first'] = $this->getUrlRange(1, 2);
$block['last'] = $this->getUrlRange($this->lastPage - ($window + 2), $this->lastPage);
} else {
$block['first'] = $this->getUrlRange(1, 2);
$block['slider'] = $this->getUrlRange($this->currentPage - $side, $this->currentPage + $side);
$block['last'] = $this->getUrlRange($this->lastPage - 1, $this->lastPage);
}
$html = '';
if (is_array($block['first'])) {
$html .= $this->getUrlLinks($block['first']);
}
if (is_array($block['slider'])) {
$html .= $this->getDots();
$html .= $this->getUrlLinks($block['slider']);
}
if (is_array($block['last'])) {
$html .= $this->getDots();
$html .= $this->getUrlLinks($block['last']);
}
return $html;
}
/**
* 渲染分页html
* @return mixed
*/
public function render()
{
if ($this->hasPages()) {
if ($this->simple) {
return sprintf(
'<ul class="pagination">%s %s</ul>',
$this->getPreviousButton(),
$this->getNextButton()
);
} else {
return sprintf(
'<ul class="pagination">%s %s %s</ul>',
$this->getPreviousButton(),
$this->getLinks(),
$this->getNextButton()
);
}
}
}
/**
* 生成一个可点击的按钮
*
* @param string $url
* @param string $page
* @return string
*/
protected function getAvailablePageWrapper(string $url, string $page): string
{
return '<li class="page-item"><a class="page-link " href="' . htmlentities($url) . '">' . $page . '</a></li>';
}
/**
* 生成一个禁用的按钮
*
* @param string $text
* @return string
*/
protected function getDisabledTextWrapper(string $text): string
{
return '<li class="disabled"><a class="page-link " href="javascript:;">' . $text . '</a></li>';
}
/**
* 生成一个激活的按钮
*
* @param string $text
* @return string
*/
protected function getActivePageWrapper(string $text): string
{
return '<li class="current"><a class="page-link active" href="javascript:;">' . $text . '</a></li>';
}
/**
* 生成省略号按钮
*
* @return string
*/
protected function getDots(): string
{
return $this->getDisabledTextWrapper('...');
}
/**
* 批量生成页码按钮.
*
* @param array $urls
* @return string
*/
protected function getUrlLinks(array $urls): string
{
$html = '';
foreach ($urls as $page => $url) {
$html .= $this->getPageLinkWrapper($url, $page);
}
return $html;
}
/**
* 生成普通页码按钮
*
* @param string $url
* @param string $page
* @return string
*/
protected function getPageLinkWrapper(string $url, string $page): string
{
if ($this->currentPage() == $page) {
return $this->getActivePageWrapper($page);
}
return $this->getAvailablePageWrapper($url, $page);
}
}
2.打开app/provider.php 文件 添加以下代码:
'think\Paginator' => app\lib\paginator\WebPagination::class,
有时项目使用多级控制器(不是多应用模式)来区分前后台或者用户中心,目录结构如下:
app
controller
web
uc
admin
那么在前台(web),后台(admin),用户中心(uc)是如何使用不同的分页样式呢?
最简单的办法就是让前端编写相同的HTML结果 ,使用不同的CSS即可。如果前端使用了不同的HTML结构怎么办呢?那我们就通过代码来解决,方法如下:
1.在app/lib/paginator目录(这个目录根据自己的情况建立)下分别创建WebPagination.php,UcPagination.php,AdminPagination.php文件, 代码根据自己情况调整
2.打开app/provider.php 文件 添加以下代码:
if (isset($_SERVER['REQUEST_URI']) && Str::startsWith($_SERVER['REQUEST_URI'], '/admin.')) {
$provider['think\Paginator'] = AdminbPagination::class;
}
if (isset($_SERVER['REQUEST_URI']) && Str::startsWith($_SERVER['REQUEST_URI'], '/uc.')) {
$provider['think\Paginator'] = UcPagination::class;
}
if (isset($_SERVER['REQUEST_URI']) && Str::startsWith($_SERVER['REQUEST_URI'], '/web.')) {
$provider['think\Paginator'] = WebPagination::class;
}
return $provider;
表示根据不同的控制器层,引用不同的分页样式
RoveCoder版权所有,转载请注明