seed
This commit is contained in:
@@ -41,51 +41,51 @@ class CusteomPage extends Paginator
|
||||
return $this->getPageLinkWrapper($url, $text);
|
||||
}
|
||||
|
||||
/**
|
||||
* 页码按钮
|
||||
* @return string
|
||||
*/
|
||||
/**
|
||||
* 页码按钮
|
||||
* @return string
|
||||
*/
|
||||
protected function getLinks(): string
|
||||
{
|
||||
if ($this->simple) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$block = [
|
||||
'first' => null,
|
||||
'slider' => null,
|
||||
'last' => null,
|
||||
];
|
||||
|
||||
if ($this->lastPage <= 3) {
|
||||
// 如果总页数少于或等于3,显示所有页码
|
||||
$block['first'] = $this->getUrlRange(1, $this->lastPage);
|
||||
} else {
|
||||
if ($this->currentPage == 1) {
|
||||
// 如果当前页码是第一页,显示前两页和第三页
|
||||
$block['first'] = $this->getUrlRange(1, 3);
|
||||
} elseif ($this->currentPage == $this->lastPage) {
|
||||
// 如果当前页码是最后一页,显示最后三页
|
||||
$block['first'] = $this->getUrlRange($this->lastPage - 2, $this->lastPage);
|
||||
} else {
|
||||
// 否则,显示当前页码及其前后各一页
|
||||
$block['first'] = $this->getUrlRange($this->currentPage - 1, $this->currentPage + 1);
|
||||
/**
|
||||
* 页码按钮
|
||||
* @return string
|
||||
*/
|
||||
/**
|
||||
* 页码按钮
|
||||
* @return string
|
||||
*/
|
||||
protected function getLinks(): string
|
||||
{
|
||||
if ($this->simple) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$block = [
|
||||
'first' => null,
|
||||
'slider' => null,
|
||||
'last' => null,
|
||||
];
|
||||
|
||||
if ($this->lastPage <= 3) {
|
||||
// 如果总页数少于或等于3,显示所有页码
|
||||
$block['first'] = $this->getUrlRange(1, $this->lastPage);
|
||||
} else {
|
||||
if ($this->currentPage == 1) {
|
||||
// 如果当前页码是第一页,显示前两页和第三页
|
||||
$block['first'] = $this->getUrlRange(1, 3);
|
||||
} elseif ($this->currentPage == $this->lastPage) {
|
||||
// 如果当前页码是最后一页,显示最后三页
|
||||
$block['first'] = $this->getUrlRange($this->lastPage - 2, $this->lastPage);
|
||||
} else {
|
||||
// 否则,显示当前页码及其前后各一页
|
||||
$block['first'] = $this->getUrlRange($this->currentPage - 1, $this->currentPage + 1);
|
||||
}
|
||||
}
|
||||
|
||||
$html = '';
|
||||
|
||||
if (is_array($block['first'])) {
|
||||
$html .= $this->getUrlLinks($block['first']);
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
$html = '';
|
||||
|
||||
if (is_array($block['first'])) {
|
||||
$html .= $this->getUrlLinks($block['first']);
|
||||
}
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
/**
|
||||
* 渲染分页html
|
||||
* @return mixed
|
||||
|
||||
152
code/extend/template/page/CusteomPage02.php
Normal file
152
code/extend/template/page/CusteomPage02.php
Normal file
@@ -0,0 +1,152 @@
|
||||
<?php
|
||||
|
||||
namespace template\page;
|
||||
|
||||
use think\Paginator;
|
||||
|
||||
class CusteomPage02
|
||||
{
|
||||
/**
|
||||
* 生成分页按钮数据
|
||||
* @param int $currentPage 当前页码
|
||||
* @param int $totalItems 总数据量
|
||||
* @param int $itemsPerPage 每页条数
|
||||
* @param string $baseUrl 基础 URL 模板(如 /male/xuanhuan-xiuzhen/finished/page{page})
|
||||
* @param int $totalButtons 总按钮数量(必须大于3)
|
||||
* @param array $buttonLabels 按钮文本配置
|
||||
* @return array 分页按钮数据
|
||||
*/
|
||||
public static function generate(
|
||||
$currentPage,
|
||||
$totalItems,
|
||||
$itemsPerPage,
|
||||
$baseUrl,
|
||||
$totalButtons = 5,
|
||||
$buttonLabels = []
|
||||
) {
|
||||
// 默认按钮文本
|
||||
$defaultLabels = [
|
||||
'prev' => '上一页',
|
||||
'next' => '下一页',
|
||||
'ellipsis' => '...'
|
||||
];
|
||||
$labels = array_merge($defaultLabels, $buttonLabels);
|
||||
|
||||
// 验证 totalButtons 必须大于 3
|
||||
if ($totalButtons < 3) {
|
||||
throw new \InvalidArgumentException('Total buttons must be greater than 3');
|
||||
}
|
||||
|
||||
// 计算总页数
|
||||
$totalPages = ceil($totalItems / $itemsPerPage);
|
||||
if ($totalPages <= 1) return []; // 只有一页时返回空数组
|
||||
|
||||
// 确保当前页码在有效范围内
|
||||
$currentPage = max(1, min($currentPage, $totalPages));
|
||||
|
||||
// 计算除去“上一页”和“下一页”后可显示的页码按钮数
|
||||
$pageButtons = $totalButtons - 2; // 减去“上一页”和“下一页”
|
||||
|
||||
// 初始化按钮数组
|
||||
$buttons = [];
|
||||
|
||||
// 上一页按钮
|
||||
$buttons[] = [
|
||||
'type' => 'prev',
|
||||
'label' => $labels['prev'],
|
||||
'url' => $currentPage > 1 ? str_replace('{page}', $currentPage - 1, $baseUrl) : null,
|
||||
'rel' => $currentPage > 1 ? 'prev' : null,
|
||||
'disabled' => $currentPage <= 1
|
||||
];
|
||||
|
||||
// 计算页码范围
|
||||
$half = floor($pageButtons / 2);
|
||||
$start = max(1, $currentPage - $half);
|
||||
$end = $start + $pageButtons - 1;
|
||||
|
||||
// 调整范围,确保不超过总页数
|
||||
if ($end > $totalPages) {
|
||||
$end = $totalPages;
|
||||
$start = max(1, $end - $pageButtons + 1);
|
||||
}
|
||||
|
||||
// 如果总页数大于可显示的页码数,添加省略号逻辑
|
||||
if ($totalPages > $pageButtons) {
|
||||
if ($start > 1) {
|
||||
$buttons[] = [
|
||||
'type' => 'page',
|
||||
'label' => '1',
|
||||
'url' => str_replace('{page}', 1, $baseUrl),
|
||||
'current' => false
|
||||
];
|
||||
if ($start > 2) {
|
||||
$buttons[] = [
|
||||
'type' => 'ellipsis',
|
||||
'label' => $labels['ellipsis'],
|
||||
'url' => null
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// 添加页码按钮
|
||||
for ($i = $start; $i <= $end; $i++) {
|
||||
$buttons[] = [
|
||||
'type' => 'page',
|
||||
'label' => (string)$i,
|
||||
'url' => str_replace('{page}', $i, $baseUrl),
|
||||
'current' => $i === $currentPage
|
||||
];
|
||||
}
|
||||
|
||||
if ($end < $totalPages) {
|
||||
if ($end < $totalPages - 1) {
|
||||
$buttons[] = [
|
||||
'type' => 'ellipsis',
|
||||
'label' => $labels['ellipsis'],
|
||||
'url' => null
|
||||
];
|
||||
}
|
||||
$buttons[] = [
|
||||
'type' => 'page',
|
||||
'label' => (string)$totalPages,
|
||||
'url' => str_replace('{page}', $totalPages, $baseUrl),
|
||||
'current' => false
|
||||
];
|
||||
}
|
||||
} else {
|
||||
// 总页数少于等于可显示页码数,直接全部显示
|
||||
for ($i = 1; $i <= $totalPages; $i++) {
|
||||
$buttons[] = [
|
||||
'type' => 'page',
|
||||
'label' => (string)$i,
|
||||
'url' => str_replace('{page}', $i, $baseUrl),
|
||||
'current' => $i === $currentPage
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// 下一页按钮
|
||||
$buttons[] = [
|
||||
'type' => 'next',
|
||||
'label' => $labels['next'],
|
||||
'url' => $currentPage < $totalPages ? str_replace('{page}', $currentPage + 1, $baseUrl) : null,
|
||||
'rel' => $currentPage < $totalPages ? 'next' : null,
|
||||
'disabled' => $currentPage >= $totalPages
|
||||
];
|
||||
|
||||
return $buttons;
|
||||
}
|
||||
}
|
||||
|
||||
// // 测试示例 1:显示 5 个按钮
|
||||
// $currentPage = 3;
|
||||
// $totalItems = 100;
|
||||
// $itemsPerPage = 10;
|
||||
// $baseUrl = '/male/xuanhuan-xiuzhen/finished/page{page}';
|
||||
// $buttons = Pagination::generate($currentPage, $totalItems, $itemsPerPage, $baseUrl, 5);
|
||||
// print_r($buttons);
|
||||
|
||||
// // 测试示例 2:显示 7 个按钮
|
||||
// $buttons = Pagination::generate($currentPage, $totalItems, $itemsPerPage, $baseUrl, 7);
|
||||
// print_r($buttons);
|
||||
//
|
||||
Reference in New Issue
Block a user