ThinkPHP 內建的 Think\Page 分頁類功能強大、配置靈活,只需傳入總記錄數、每頁條數即可快速生成分頁導航。本文深度解析其核心屬性、配置陣列、樣式定制機制與底層實現原理。
| 屬性 | 說明 | 預設值 |
|---|---|---|
| firstRow | 起始行數 (OFFSET) | 0 |
| listRows | 每頁顯示行數 (LIMIT) | 20 |
| parameter | 分頁跳轉攜帶參數 (陣列或查詢字串) | '' |
| totalRows | 總記錄數 | 0 |
| totalPages | 總頁數 (自動計算) | 0 |
| rollPage | 分頁欄顯示頁碼數量 | 11 |
| lastSuffix | 末頁是否顯示總頁數 | true |
| p (private) | URL 分頁參數名 | 'p' |
| url (private) | 當前 URL 基礎路徑 | '' |
| nowPage (private) | 當前頁碼 | 1 |
<?php
// 控制器中
$count = $model->where($where)->count(); // 總記錄數
$Page = new \Think\Page($count, 25); // 實例化:總數、每頁25條
$show = $Page->show(); // 生成分頁 HTML
// 模板輸出
$this->assign('page', $show);
$this->display();
?>
<?php
$Page = new \Think\Page($count, 25);
// 只顯示 5 個頁碼
$Page->rollPage = 5;
// 末頁不顯示總頁數 (由 header 顯示總條數)
$Page->lastSuffix = false;
// 自定義 URL 參數名 (預設為 p)
$Page->p = 'page';
// 攜帶額外查詢參數 (如搜尋關鍵字)
$Page->parameter = array('keyword' => $keyword, 'status' => $status);
// 或字串形式
$Page->parameter = 'keyword=thinkphp&status=1';
?>
分頁 HTML 由 config['theme'] 模板字串配合 str_replace() 替換佔位符生成:
<?php
// 內部預設配置
private $config = array(
'header' => '共 %TOTAL_ROW% 條記錄',
'prev' => '<<',
'next' => '>>',
'first' => '1...',
'last' => '...%TOTAL_PAGE%',
'theme' => '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%',
);
// 覆蓋單個配置項
$Page->setConfig('header', '總計 %TOTAL_ROW% 筆資料');
$Page->setConfig('prev', '上一頁');
$Page->setConfig('next', '下一頁');
$Page->setConfig('first', '首頁');
$Page->setConfig('last', '尾頁');
// 關鍵:theme 中加入 %HEADER% 才會顯示統計資訊
$Page->setConfig('theme', '%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
?>
| 佔位符 | 含義 |
|---|---|
| %HEADER% | 統計資訊 (對應 config['header']) |
| %NOW_PAGE% | 當前頁碼 |
| %UP_PAGE% | 上一頁連結 (config['prev']) |
| %DOWN_PAGE% | 下一頁連結 (config['next']) |
| %FIRST% | 首頁連結 (config['first']) |
| %LINK_PAGE% | 數字頁碼連結群 |
| %END% | 尾頁連結 (config['last']) |
| %TOTAL_ROW% | 總記錄數 |
| %TOTAL_PAGE% | 總頁數 |
源碼核心片段 (Think\Page::show()):
<?php
// 計算各部件 HTML
$up_page = $this->nowPage > 1 ? '<a href="' . $this->url($this->nowPage - 1) . '">' . $this->config['prev'] . '</a>' : '';
$down_page = $this->nowPage < $this->totalPages ? '<a href="' . $this->url($this->nowPage + 1) . '">' . $this->config['next'] . '</a>' : '';
$the_first = $this->nowPage > $this->rollPage ? '<a href="' . $this->url(1) . '">' . $this->config['first'] . '</a>' : '';
$the_end = $this->nowPage < $this->totalPages - $this->rollPage ? '<a href="' . $this->url($this->totalPages) . '">' . $this->config['last'] . '</a>' : '';
$link_page = $this->getPageLinks(); // 生成數字頁碼 HTML
// 統一替換:這是設計精華,靈活控制順序與內容
$page_str = str_replace(
array('%HEADER%', '%NOW_PAGE%', '%UP_PAGE%', '%DOWN_PAGE%', '%FIRST%', '%LINK_PAGE%', '%END%', '%TOTAL_ROW%', '%TOTAL_PAGE%'),
array($this->config['header'], $this->nowPage, $up_page, $down_page, $the_first, $link_page, $the_end, $this->totalRows, $this->totalPages),
$this->config['theme']
);
return $page_str;
?>
分頁類自動為常用元素添加 class:
.page a - 普通頁碼.page .current - 當前頁 (無 href).page .prev - 上一頁.page .next - 下一頁引入一段 CSS 即可美化:
<style>
.page { text-align: center; margin: 20px 0; font-family: Arial; }
.page a, .page span { display: inline-block; padding: 6px 12px; margin: 0 2px; border: 1px solid #ddd; color: #337ab7; text-decoration: none; border-radius: 3px; }
.page a:hover { background: #337ab7; color: #fff; }
.page .current { background: #337ab7; color: #fff; border-color: #337ab7; cursor: default; }
.page .disabled { color: #999; border-color: #ddd; cursor: not-allowed; }
</style>
<?php
// 重寫 url 方法輸出 JS 調用
$Page->setConfig('theme', '%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
// 結合 data-page 屬性 + jQuery 委派事件實現
?>
<script>
$('.page').on('click', 'a', function(e) {
e.preventDefault();
var page = $(this).data('page') || $(this).attr('href').match(/p=(\d+)/)[1];
loadList(page);
});
</script>