>[success] # 默認分頁模板

>[success] # 自定義分頁模板

```php
<?php
namespace tool;
use think\Paginator;
class TpPageHelper extends Paginator
{
//TODO : 中間按鈕個數
protected $bnumber = 10;
//TODO : 首頁
protected function getfirstButton($str = '')
{
if ($this->currentPage() <= 1) {
return $this->getDisabledTextWrapper($str);
}
$url = $this->url(1);
return $this->getPageLinkWrapper($url, $str);
}
//TODO : 上一頁
protected function getPreviousButton($text = "«")
{
if ($this->currentPage() <= 1) {
return $this->getDisabledTextWrapper($text);
}
$url = $this->url(
$this->currentPage() - 1
);
return $this->getPageLinkWrapper($url, $text);
}
//TODO : 頁碼
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));
}
}
}
//TODO : 下一頁
protected function getNextButton($text = '»')
{
if (!$this->hasMore) {
return $this->getDisabledTextWrapper($text);
}
$url = $this->url($this->currentPage() + 1);
return $this->getPageLinkWrapper($url, $text);
}
//TODO : 末頁
protected function getlastButton($text = '')
{
if (!$this->hasMore) {
return $this->getDisabledTextWrapper($text);
}
$url = $this->url($this->lastPage());
return $this->getPageLinkWrapper($url, $text);
}
//TODO : 渲染頁
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('末頁')
);
}
}
//TODO : 生成禁用按鈕
protected function getDisabledTextWrapper($text)
{
return '<li class="disabled"><span>' . $text . '</span></li>';
}
//TODO : 生成普通按鈕
protected function getPageLinkWrapper($url, $page)
{
if ($page == $this->currentPage()) {
return $this->getActivePageWrapper($page);
}
return $this->getAvailablePageWrapper($url, $page);
}
//TODO : 生成當前頁按鈕
protected function getActivePageWrapper($text)
{
return '<li class="active"><span>' . $text . '</span></li>';
}
//TODO : 可點擊按鈕
protected function getAvailablePageWrapper($url, $page)
{
return '<li><a href="' . htmlentities($url) . '">' . $page . '</a></li>';
}
//TODO : 批量生成頁碼按鈕
protected function getUrlLinks(array $urls)
{
$html = '';
foreach ($urls as $page => $url) {
$html .= $this->getPageLinkWrapper($url, $page);
}
return $html;
}
}
```
* 將 TpPageHelper.php 放在extend\tool下

* 修改默認模板

> ### 項目源碼下載
- Tip:公眾號回復【025】獲取源代碼

- 技術擴展閱讀
- 第一章
- 第一節 PHP與Golang 項目案例 - 留言板
- 第二節 PHP 實現日歷功能
- 第三節 ThinkPHP 自定義分頁模板
- 第四節 WebUpload 文件上傳
- 第五節 UEditor 文本編輯器
- 第六節 ThinkPHP 驗證碼
- 第七節 百度地圖
- 第八節 PHP 接口調試工具 SocketLog
- 第九節 PHP 跟蹤調試代碼 XDebug
- 第十節 PHPExcel 表格導入和導出
- 第二章
- 十一節 實戰筆記 - Kafka 篇
- 十二節 實戰筆記 - Redis 篇
- 十三節 實戰筆記 - MySQL 篇
- 十四節 圖片轉ASCII碼圖
- 十六節 Python 視頻轉代碼視頻
- 源代碼
- 代碼2
- 十七節 GRPC PHP客戶端