ThinkPHP6.0使用自定義分頁(yè)類(lèi) 重寫(xiě)paginate頁(yè)碼樣式

ThinkPHP6.0給我們預(yù)定義了paginate分頁(yè)類(lèi)谒出,幫助我們快速分頁(yè),但是ThinkPHP6提供的分頁(yè)的樣式并不是我們想要的逾滥,需要我們自己擴(kuò)展分頁(yè)類(lèi)徐勃,看看具體如何實(shí)現(xiàn)吧!

  1. 首先我們?nèi)?fù)制一份官方的寫(xiě)好的分頁(yè)類(lèi)礼患,并在此基礎(chǔ)上進(jìn)行修改是钥,具體的路徑在vendor/topthink/think-orm/src/paginator/driver/Bootstrap.php,然后在app/common/Bootstrap.php粘貼缅叠。
  2. 修改app/provider.php服務(wù)提供者悄泥,修改默認(rèn)的分頁(yè)驅(qū)動(dòng)為我們的驅(qū)動(dòng)。
<?php
use app\ExceptionHandle;
use app\Request;

// 容器Provider定義文件
return [
    'think\Request'          => Request::class,
    'think\exception\Handle' => ExceptionHandle::class,
    'think\Paginator'    =>    'app\common\Bootstrap'
];
  1. 閱讀app/common/Bootstrap.php代碼肤粱,在此基礎(chǔ)上修改弹囚。以下是官方提供的分頁(yè)代碼vendor/topthink/think-orm/src/paginator/driver/Bootstrap.php
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: zhangyajun <448901948@qq.com>
// +----------------------------------------------------------------------

namespace think\paginator\driver;

use think\Paginator;

/**
 * Bootstrap 分頁(yè)驅(qū)動(dòng)
 */
class Bootstrap extends Paginator
{

    /**
     * 上一頁(yè)按鈕
     * @param string $text
     * @return string
     */
    protected function getPreviousButton(string $text = "&laquo;"): string
    {

        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 getNextButton(string $text = '&raquo;'): string
    {
        if (!$this->hasMore) {
            return $this->getDisabledTextWrapper($text);
        }

        $url = $this->url($this->currentPage() + 1);

        return $this->getPageLinkWrapper($url, $text);
    }

    /**
     * 頁(yè)碼按鈕
     * @return string
     */
    protected function getLinks(): string
    {
        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(
                    '<ul class="pager">%s %s</ul>',
                    $this->getPreviousButton(),
                    $this->getNextButton()
                );
            } else {
                return sprintf(
                    '<ul class="pagination">%s %s %s</ul>',
                    $this->getPreviousButton(),
                    $this->getLinks(),
                    $this->getNextButton()
                );
            }
        }
    }

    /**
     * 生成一個(gè)可點(diǎn)擊的按鈕
     *
     * @param  string $url
     * @param  string $page
     * @return string
     */
    protected function getAvailablePageWrapper(string $url, string $page): string
    {
        return '<li><a href="' . htmlentities($url) . '">' . $page . '</a></li>';
    }

    /**
     * 生成一個(gè)禁用的按鈕
     *
     * @param  string $text
     * @return string
     */
    protected function getDisabledTextWrapper(string $text): string
    {
        return '<li class="disabled"><span>' . $text . '</span></li>';
    }

    /**
     * 生成一個(gè)激活的按鈕
     *
     * @param  string $text
     * @return string
     */
    protected function getActivePageWrapper(string $text): string
    {
        return '<li class="active"><span>' . $text . '</span></li>';
    }

    /**
     * 生成省略號(hào)按鈕
     *
     * @return string
     */
    protected function getDots(): string
    {
        return $this->getDisabledTextWrapper('...');
    }

    /**
     * 批量生成頁(yè)碼按鈕.
     *
     * @param  array $urls
     * @return string
     */
    protected function getUrlLinks(array $urls): string
    {
        $html = '';

        foreach ($urls as $page => $url) {
            $html .= $this->getPageLinkWrapper($url, $page);
        }

        return $html;
    }

    /**
     * 生成普通頁(yè)碼按鈕
     *
     * @param  string $url
     * @param  string    $page
     * @return string
     */
    protected function getPageLinkWrapper(string $url, string $page): string
    {
        if ($this->currentPage() == $page) {
            return $this->getActivePageWrapper($page);
        }

        return $this->getAvailablePageWrapper($url, $page);
    }
}

例如簡(jiǎn)單修改上一頁(yè)下一頁(yè)為中文漢字,修改以下地方领曼。

  /**
     * 上一頁(yè)按鈕
     * @param string $text
     * @return string
     */
    protected function getPreviousButton(string $text = "上一頁(yè)"): string
    {

        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 getNextButton(string $text = '下一頁(yè)'): string
    {
        if (!$this->hasMore) {
            return $this->getDisabledTextWrapper($text);
        }

        $url = $this->url($this->currentPage() + 1);

        return $this->getPageLinkWrapper($url, $text);
    }

然后根據(jù)自己的業(yè)務(wù)需求進(jìn)行修改鸥鹉!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市悯森,隨后出現(xiàn)的幾起案子宋舷,更是在濱河造成了極大的恐慌,老刑警劉巖瓢姻,帶你破解...
    沈念sama閱讀 212,383評(píng)論 6 493
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件祝蝠,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡幻碱,警方通過(guò)查閱死者的電腦和手機(jī)绎狭,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,522評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)褥傍,“玉大人儡嘶,你說(shuō)我怎么就攤上這事』蟹纾” “怎么了蹦狂?”我有些...
    開(kāi)封第一講書(shū)人閱讀 157,852評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵誓篱,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我凯楔,道長(zhǎng)窜骄,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,621評(píng)論 1 284
  • 正文 為了忘掉前任摆屯,我火速辦了婚禮邻遏,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘虐骑。我一直安慰自己准验,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,741評(píng)論 6 386
  • 文/花漫 我一把揭開(kāi)白布廷没。 她就那樣靜靜地躺著糊饱,像睡著了一般。 火紅的嫁衣襯著肌膚如雪腕柜。 梳的紋絲不亂的頭發(fā)上济似,一...
    開(kāi)封第一講書(shū)人閱讀 49,929評(píng)論 1 290
  • 那天,我揣著相機(jī)與錄音盏缤,去河邊找鬼砰蠢。 笑死,一個(gè)胖子當(dāng)著我的面吹牛唉铜,可吹牛的內(nèi)容都是我干的台舱。 我是一名探鬼主播,決...
    沈念sama閱讀 39,076評(píng)論 3 410
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼潭流,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼竞惋!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起灰嫉,我...
    開(kāi)封第一講書(shū)人閱讀 37,803評(píng)論 0 268
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤拆宛,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后讼撒,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體浑厚,經(jīng)...
    沈念sama閱讀 44,265評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,582評(píng)論 2 327
  • 正文 我和宋清朗相戀三年根盒,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片炎滞。...
    茶點(diǎn)故事閱讀 38,716評(píng)論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡敢艰,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出册赛,到底是詐尸還是另有隱情钠导,我是刑警寧澤震嫉,帶...
    沈念sama閱讀 34,395評(píng)論 4 333
  • 正文 年R本政府宣布,位于F島的核電站辈双,受9級(jí)特大地震影響责掏,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜湃望,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,039評(píng)論 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望痰驱。 院中可真熱鬧证芭,春花似錦、人聲如沸担映。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,798評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)蝇完。三九已至官硝,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間短蜕,已是汗流浹背氢架。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,027評(píng)論 1 266
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留朋魔,地道東北人岖研。 一個(gè)月前我還...
    沈念sama閱讀 46,488評(píng)論 2 361
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像警检,于是被迫代替她去往敵國(guó)和親孙援。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,612評(píng)論 2 350

推薦閱讀更多精彩內(nèi)容