thinkphp自定義分頁

thinkphp5.1內置了分頁實現,要給數據添加分頁輸出功能變得非常簡單,可以直接在db類查詢的時候調用paginate方法。本文為大家介紹了thinkphp自定義分頁樣式的方法。

thinkphp自定義分頁

thinkphp5.1有很方便的分頁類,用render方法即可渲染分頁的html代碼

但”>”這樣的的下一頁有時無法滿足項目多變的需求,有必要自己定義分頁的顯示,比如

首頁? ? 上一頁? ? 1? ? 2? ? 3? ? …? ? 7?????8? ? 下一頁? ? 末頁? ?

立即學習PHP免費學習筆記(深入)”;

這樣,然而官方的手冊并沒有提到自定義分頁樣式的方法,我開始也只是簡單的把分頁的html替換成上一頁下一頁的文字

后來又搜到可以自己定義一個類來完成這個需求,首先需要在config目錄創建paginate.php,文件內容

<?php return [ &#39;type&#39;=>'appindexpagergcudPager'//自己的分頁類可以隨便放,只要命名空間寫對 ];

然后復制”項目目錄thinkphplibrarythinkpaginatordriverbootstrap.php”到一個任意位置,改改命名空間,把paginate.php的type改成相應的命名空間,比如我就把文件復制到了”項目目錄applicationindexpagergcudPager.php”,上面的type也是和這個路徑對應的,然后把命名空間改成了”appindexpager”,對應的類名改成了gcudPager,這樣就可以自行定義分頁的形式了

首頁的實現我是按照上一頁來的,復制它的代碼,略加修改

????/**首頁按鈕 ?????*?@param?string?$text ?????*?@return?string ?????*/ ????protected?function?GetFirstButton($text='首頁'){ ????????if?($this-&gt;currentPage()?getDisabledTextWrapper($text); ????????} ????????$url?=?$this-&gt;url(1);  ????????return?$this-&gt;getPageLinkWrapper($url,?$text); ????}

邏輯很簡單,就是判斷下當前頁數,手動把頁數變量設置為1,同理可以復制下一頁的代碼改成末頁

????/**末頁按鈕 ?????*?@param?string?$text ?????*?@return?string ?????*/ ????protected?function?GetLastButton($text='末頁'){ ????????if?(!$this-&gt;hasMore)?{ ????????????return?$this-&gt;getDisabledTextWrapper($text); ????????} ????????$url?=?$this-&gt;url($this-&gt;lastPage());  ????????return?$this-&gt;getPageLinkWrapper($url,?$text); ????}

其它上一頁下一頁也就是改個文本太簡單不說,render函數部分需要把首頁和末頁按鈕加進來

????/** ?????*?渲染分頁html ?????*?@return?mixed ?????*/ ????public?function?render() ????{ ????????if?($this-&gt;hasPages())?{ ????????????if?($this-&gt;simple)?{ ????????????????return?sprintf( ????????????????????'
    %s?%s

‘, ????????????????????$this->getPreviousButton(), ????????????????????$this->getNextButton() ????????????????); ????????????}?else?{ ????????????????return?sprintf( ????????????????????‘

%s?%s?%s?%s?%s

‘, ????????????????????$this->GetFirstButton(), ????????????????????$this->getPreviousButton(), ????????????????????$this->getLinks(), ????????????????????$this->getNextButton(), ????????????????????$this->GetLastButton() ????????????????); ????????????} ????????} ????}

這樣就弄完了,調用部分完全不用改,最后放上完整代碼

 //?+----------------------------------------------------------------------  namespace?appindexpager;  use?thinkPaginator;  class?gcudPager?extends?Paginator { ????/**首頁按鈕 ?????*?@param?string?$text ?????*?@return?string ?????*/ ????protected?function?GetFirstButton($text='首頁'){ ????????if?($this-&gt;currentPage()?getDisabledTextWrapper($text); ????????} ????????$url?=?$this-&gt;url(1);  ????????return?$this-&gt;getPageLinkWrapper($url,?$text); ????} ????/** ?????*?上一頁按鈕 ?????*?@param?string?$text ?????*?@return?string ?????*/ ????protected?function?getPreviousButton($text?=?"上一頁") ????{  ????????if?($this->currentPage()?getDisabledTextWrapper($text); ????????}  ????????$url?=?$this->url( ????????????$this->currentPage()?-?1 ????????);  ????????return?$this->getPageLinkWrapper($url,?$text); ????} ????/**末頁按鈕 ?????*?@param?string?$text ?????*?@return?string ?????*/ ????protected?function?GetLastButton($text='末頁'){ ????????if?(!$this-&gt;hasMore)?{ ????????????return?$this-&gt;getDisabledTextWrapper($text); ????????} ????????$url?=?$this-&gt;url($this-&gt;lastPage());  ????????return?$this-&gt;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?getUrlRange(1,?$this->lastPage); ????????}?elseif?($this->currentPage?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-&gt;hasPages())?{ ????????????if?($this-&gt;simple)?{ ????????????????return?sprintf( ????????????????????'
    %s?%s

‘, ????????????????????$this->getPreviousButton(), ????????????????????$this->getNextButton() ????????????????); ????????????}?else?{ ????????????????return?sprintf( ????????????????????‘

%s?%s?%s?%s?%s

‘, ????????????????????$this->GetFirstButton(), ????????????????????$this->getPreviousButton(), ????????????????????$this->getLinks(), ????????????????????$this->getNextButton(), ????????????????????$this->GetLastButton() ????????????????); ????????????} ????????} ????} ????/** ?????*?生成一個可點擊的按鈕 ?????* ?????*?@param??string?$url ?????*?@param??int????$page ?????*?@return?string ?????*/ ????protected?function?getAvailablePageWrapper($url,?$page) ????{ ????????return?‘‘?.?$page?.?‘‘; ????} ????/** ?????*?生成一個禁用的按鈕 ?????* ?????*?@param??string?$text ?????*?@return?string ?????*/ ????protected?function?getDisabledTextWrapper($text) ????{ ????????return?‘‘?.?$text?.?‘‘; ????} ????/** ?????*?生成一個激活的按鈕 ?????* ?????*?@param??string?$text ?????*?@return?string ?????*/ ????protected?function?getActivePageWrapper($text) ????{ ????????return?‘‘?.?$text?.?‘‘; ????} ????/** ?????*?生成省略號按鈕 ?????* ?????*?@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教程

以上就是

? 版權聲明
THE END
喜歡就支持一下吧
點贊7 分享