新聞中心
這篇文章主要介紹了thinkphp怎樣自定義分頁(yè),具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)是一家專業(yè)提供定遠(yuǎn)企業(yè)網(wǎng)站建設(shè),專注與做網(wǎng)站、成都網(wǎng)站制作、H5高端網(wǎng)站建設(shè)、小程序制作等業(yè)務(wù)。10年已為定遠(yuǎn)眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jìn)行中。
ThinkPHP5.1內(nèi)置了分頁(yè)實(shí)現(xiàn),要給數(shù)據(jù)添加分頁(yè)輸出功能變得非常簡(jiǎn)單,可以直接在Db類查詢的時(shí)候調(diào)用paginate方法。
thinkphp5.1有很方便的分頁(yè)類,用render方法即可渲染分頁(yè)的html代碼
但"<<"的上一頁(yè)和">>"這樣的的下一頁(yè)有時(shí)無(wú)法滿足項(xiàng)目多變的需求,有必要自己定義分頁(yè)的顯示,比如
首頁(yè) 上一頁(yè) 1 2 3 ... 7 8 下一頁(yè) 末頁(yè)
這樣,然而官方的手冊(cè)并沒(méi)有提到自定義分頁(yè)樣式的方法,我開(kāi)始也只是簡(jiǎn)單的把分頁(yè)的html替換成上一頁(yè)下一頁(yè)的文字
后來(lái)又搜到可以自己定義一個(gè)類來(lái)完成這個(gè)需求,首先需要在config目錄創(chuàng)建paginate.php,文件內(nèi)容
'app\index\pager\gcudPager'//自己的分頁(yè)類可以隨便放,只要命名空間寫(xiě)對(duì) ];
然后復(fù)制"項(xiàng)目目錄\thinkphp\library\think\paginator\driver\Bootstrap.php"到一個(gè)任意位置,改改命名空間,把paginate.php的type改成相應(yīng)的命名空間,比如我就把文件復(fù)制到了"項(xiàng)目目錄\application\index\pager\gcudPager.php",上面的type也是和這個(gè)路徑對(duì)應(yīng)的,然后把命名空間改成了"app\index\pager",對(duì)應(yīng)的類名改成了gcudPager,這樣就可以自行定義分頁(yè)的形式了
首頁(yè)的實(shí)現(xiàn)我是按照上一頁(yè)來(lái)的,復(fù)制它的代碼,略加修改
/**首頁(yè)按鈕 * @param string $text * @return string */ protected function GetFirstButton($text='首頁(yè)'){ if ($this->currentPage() <= 1) { return $this->getDisabledTextWrapper($text); } $url = $this->url(1); return $this->getPageLinkWrapper($url, $text); }
邏輯很簡(jiǎn)單,就是判斷下當(dāng)前頁(yè)數(shù),手動(dòng)把頁(yè)數(shù)變量設(shè)置為1,同理可以復(fù)制下一頁(yè)的代碼改成末頁(yè)
/**末頁(yè)按鈕 * @param string $text * @return string */ protected function GetLastButton($text='末頁(yè)'){ if (!$this->hasMore) { return $this->getDisabledTextWrapper($text); } $url = $this->url($this->lastPage()); return $this->getPageLinkWrapper($url, $text); }
其它上一頁(yè)下一頁(yè)也就是改個(gè)文本太簡(jiǎn)單不說(shuō),render函數(shù)部分需要把首頁(yè)和末頁(yè)按鈕加進(jìn)來(lái)
/** * 渲染分頁(yè)html * @return mixed */ public function render() { if ($this->hasPages()) { if ($this->simple) { return sprintf( '
- %s %s
這樣就弄完了,調(diào)用部分完全不用改,最后放上完整代碼
// +---------------------------------------------------------------------- namespace app\index\pager; use think\Paginator; class gcudPager extends Paginator { /**首頁(yè)按鈕 * @param string $text * @return string */ protected function GetFirstButton($text='首頁(yè)'){ if ($this->currentPage() <= 1) { return $this->getDisabledTextWrapper($text); } $url = $this->url(1); return $this->getPageLinkWrapper($url, $text); } /** * 上一頁(yè)按鈕 * @param string $text * @return string */ protected function getPreviousButton($text = "上一頁(yè)") { if ($this->currentPage() <= 1) { return $this->getDisabledTextWrapper($text); } $url = $this->url( $this->currentPage() - 1 ); return $this->getPageLinkWrapper($url, $text); } /**末頁(yè)按鈕 * @param string $text * @return string */ protected function GetLastButton($text='末頁(yè)'){ if (!$this->hasMore) { return $this->getDisabledTextWrapper($text); } $url = $this->url($this->lastPage()); return $this->getPageLinkWrapper($url, $text); } /** * 下一頁(yè)按鈕 * @param string $text * @return string */ protected function getNextButton($text = '下一頁(yè)') { if (!$this->hasMore) { return $this->getDisabledTextWrapper($text); } $url = $this->url($this->currentPage() + 1); return $this->getPageLinkWrapper($url, $text); } /** * 頁(yè)碼按鈕 * @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; } /** * 渲染分頁(yè)html * @return mixed */ public function render() { if ($this->hasPages()) { if ($this->simple) { return sprintf( '
- %s %s
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“thinkphp怎樣自定義分頁(yè)”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
文章標(biāo)題:thinkphp怎樣自定義分頁(yè)
URL鏈接:http://fisionsoft.com.cn/article/jcgcsj.html