首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

ThinkPHP 分頁類深度解析:配置參數、樣式定制與核心原理

     編輯:PHP 相關知識

ThinkPHP 分頁類深度解析:配置參數、樣式定制與核心原理

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';
?>

深度定制:config 陣列與 theme 模板

分頁 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%總頁數

核心原理:str_replace 替換機制

源碼核心片段 (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;
?>

CSS 樣式快速接入

分頁類自動為常用元素添加 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>

進階技巧:AJAX 無刷新分頁

<?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>
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved