thinkphp5.1自定義分頁樣式

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

但"<<"的上一頁和">>"這樣的的下一頁有時沒法知足項目多變的需求,有必要本身定義分頁的顯示,好比html

首頁    上一頁    1    2    3    ...    7     8    下一頁    末頁   thinkphp

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

後來又搜到能夠本身定義一個類來完成這個需求,首先須要在config目錄建立paginate.php,文件內容app

<?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,這樣就能夠自行定義分頁的形式了ide

首頁的實現我是按照上一頁來的,複製它的代碼,略加修改函數

/**首頁按鈕
     * @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,同理能夠複製下一頁的代碼改爲末頁thinkphp5

/**末頁按鈕
     * @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函數部分須要把首頁和末頁按鈕加進來this

/**
     * 渲染分頁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()
                );
            }
        }
    }

這樣就弄完了,調用部分徹底不用改,最後放上完整代碼url

<?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);
    }
}
相關文章
相關標籤/搜索