搜尋
首頁php框架ThinkPHPthinkphp自訂分頁

thinkphp自訂分頁

Apr 10, 2020 am 09:02 AM
thinkphp

ThinkPHP5.1內建了分頁實現,而要為資料新增分頁輸出功能變得非常簡單,可以直接在Db類別查詢的時候呼叫paginate方法。本文為大家介紹了thinkphp自訂分頁樣式的方法。

thinkphp自訂分頁

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

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

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

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

後來又搜到可以自己定義一個類別來完成這個需求,首先需要在config目錄建立paginate.php,檔案內容

<?php
return [
&#39;type&#39;=>&#39;app\index\pager\gcudPager&#39;//自己的分页类可以随便放,只要命名空间写对
];

然後複製"專案目錄\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=&#39;首页&#39;){
        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=&#39;末页&#39;){
        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(
                    &#39;<ul class="pager">%s %s</ul>&#39;,
                    $this->getPreviousButton(),
                    $this->getNextButton()
                );
            } else {
                return sprintf(
                    &#39;<div class="page-captions">%s %s %s %s %s</div>&#39;,
                    $this->GetFirstButton(),
                    $this->getPreviousButton(),
                    $this->getLinks(),
                    $this->getNextButton(),
                    $this->GetLastButton()
                );
            }
        }
    }

這樣就弄完了,呼叫部分完全不用改,最後放上完整程式碼


// +----------------------------------------------------------------------

namespace app\index\pager;

use think\Paginator;

class gcudPager extends Paginator
{
    /**首页按钮
     * @param string $text
     * @return string
     */
    protected function GetFirstButton($text=&#39;首页&#39;){
        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=&#39;末页&#39;){
        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(
                    &#39;<ul class="pager">%s %s</ul>&#39;,
                    $this->getPreviousButton(),
                    $this->getNextButton()
                );
            } else {
                return sprintf(
                    &#39;<div class="page-captions">%s %s %s %s %s</div>&#39;,
                    $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教學

#

以上是thinkphp自訂分頁的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文轉載於:OSCHINA。如有侵權,請聯絡admin@php.cn刪除

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

MantisBT

MantisBT

Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。