1. 程式人生 > >thinkphp5.1自定義分頁樣式

thinkphp5.1自定義分頁樣式

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 <[email protected]>
// +----------------------------------------------------------------------

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);
    }
}