這篇文章主要介紹了thinkphp怎樣自定義分頁,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
ThinkPHP5.1內置了分頁實現,要給數據添加分頁輸出功能變得非常簡單,可以直接在Db類查詢的時候調用paginate方法。
thinkphp5.1有很方便的分頁類,用render方法即可渲染分頁的html代碼
但"<<"的上一頁和">>"這樣的的下一頁有時無法滿足項目多變的需求,有必要自己定義分頁的顯示,比如
首頁 上一頁 1 2 3 ... 7 8 下一頁 末頁
這樣,然而官方的手冊并沒有提到自定義分頁樣式的方法,我開始也只是簡單的把分頁的html替換成上一頁下一頁的文字
后來又搜到可以自己定義一個類來完成這個需求,首先需要在config目錄創建paginate.php,文件內容
<?php return [ 'type'=>'app\index\pager\gcudPager'//自己的分頁類可以隨便放,只要命名空間寫對 ];
然后復制"項目目錄\thinkphp\library\think\paginator\driver\Bootstrap.php"到一個任意位置,改改命名空間,把paginate.php的type改成相應的命名空間,比如我就把文件復制到了"項目目錄\application\index\pager\gcudPager.php",上面的type也是和這個路徑對應的,然后把命名空間改成了"app\index\pager",對應的類名改成了gcudPager,這樣就可以自行定義分頁的形式了
首頁的實現我是按照上一頁來的,復制它的代碼,略加修改
/**首頁按鈕
* @param string $text
* @return string
*/
protected function GetFirstButton($text='首頁'){
if ($this->currentPage() <= 1) {
return $this->getDisabledTextWrapper($text);
}
$url = $this->url(1);
return $this->getPageLinkWrapper($url, $text);
}邏輯很簡單,就是判斷下當前頁數,手動把頁數變量設置為1,同理可以復制下一頁的代碼改成末頁
/**末頁按鈕
* @param string $text
* @return string
*/
protected function GetLastButton($text='末頁'){
if (!$this->hasMore) {
return $this->getDisabledTextWrapper($text);
}
$url = $this->url($this->lastPage());
return $this->getPageLinkWrapper($url, $text);
}其它上一頁下一頁也就是改個文本太簡單不說,render函數部分需要把首頁和末頁按鈕加進來
/**
* 渲染分頁html
* @return mixed
*/
public function render()
{
if ($this->hasPages()) {
if ($this->simple) {
return sprintf(
'<ul class="pager">%s %s</ul>',
$this->getPreviousButton(),
$this->getNextButton()
);
} else {
return sprintf(
'<div class="page-captions">%s %s %s %s %s</div>',
$this->GetFirstButton(),
$this->getPreviousButton(),
$this->getLinks(),
$this->getNextButton(),
$this->GetLastButton()
);
}
}
}這樣就弄完了,調用部分完全不用改,最后放上完整代碼
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: zhangyajun <448901948@qq.com>
// +----------------------------------------------------------------------
namespace app\index\pager;
use think\Paginator;
class gcudPager extends Paginator
{
/**首頁按鈕
* @param string $text
* @return string
*/
protected function GetFirstButton($text='首頁'){
if ($this->currentPage() <= 1) {
return $this->getDisabledTextWrapper($text);
}
$url = $this->url(1);
return $this->getPageLinkWrapper($url, $text);
}
/**
* 上一頁按鈕
* @param string $text
* @return string
*/
protected function getPreviousButton($text = "上一頁")
{
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 GetLastButton($text='末頁'){
if (!$this->hasMore) {
return $this->getDisabledTextWrapper($text);
}
$url = $this->url($this->lastPage());
return $this->getPageLinkWrapper($url, $text);
}
/**
* 下一頁按鈕
* @param string $text
* @return string
*/
protected function getNextButton($text = '下一頁')
{
if (!$this->hasMore) {
return $this->getDisabledTextWrapper($text);
}
$url = $this->url($this->currentPage() + 1);
return $this->getPageLinkWrapper($url, $text);
}
/**
* 頁碼按鈕
* @return string
*/
protected function getLinks()
{
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="pager">%s %s</ul>',
$this->getPreviousButton(),
$this->getNextButton()
);
} else {
return sprintf(
'<div class="page-captions">%s %s %s %s %s</div>',
$this->GetFirstButton(),
$this->getPreviousButton(),
$this->getLinks(),
$this->getNextButton(),
$this->GetLastButton()
);
}
}
}
/**
* 生成一個可點擊的按鈕
*
* @param string $url
* @param int $page
* @return string
*/
protected function getAvailablePageWrapper($url, $page)
{
return '<a href="' . htmlentities($url) . '">' . $page . '</a>';
}
/**
* 生成一個禁用的按鈕
*
* @param string $text
* @return string
*/
protected function getDisabledTextWrapper($text)
{
return '<a>' . $text . '</a>';
}
/**
* 生成一個激活的按鈕
*
* @param string $text
* @return string
*/
protected function getActivePageWrapper($text)
{
return '<span>' . $text . '</span>';
}
/**
* 生成省略號按鈕
*
* @return string
*/
protected function getDots()
{
return $this->getDisabledTextWrapper('...');
}
/**
* 批量生成頁碼按鈕.
*
* @param array $urls
* @return string
*/
protected function getUrlLinks(array $urls)
{
$html = '';
foreach ($urls as $page => $url) {
$html .= $this->getPageLinkWrapper($url, $page);
}
return $html;
}
/**
* 生成普通頁碼按鈕
*
* @param string $url
* @param int $page
* @return string
*/
protected function getPageLinkWrapper($url, $page)
{
if ($this->currentPage() == $page) {
return $this->getActivePageWrapper($page);
}
return $this->getAvailablePageWrapper($url, $page);
}
}感謝你能夠認真閱讀完這篇文章,希望小編分享的“thinkphp怎樣自定義分頁”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。