thinkphp自定义分页_技术学院_宜昌市隼壹珍商贸有限公司

您好,欢迎访问宜昌市隼壹珍商贸有限公司

400 890 5375
当前位置: 主页 > 新闻动态 > 技术学院

thinkphp自定义分页

发布时间:2020-04-10  |  点击率:

thinkphp5.1内置了分页实现,要给数据添加分页输出功能变得非常简单,可以直接在db类查询的时候调用paginate方法。本文为大家介绍了thinkphp自定义分页样式的方法。

thinkphp5.1有很方便的分页类,用render方法即可渲染分页的html代码

但">"这样的的下一页有时无法满足项目多变的需求,有必要自己定义分页的显示,比如

首页    上一页    1    2    3    ...    7     8    下一页    末页   

这样,然而官方的手册并没有提到自定义分页样式的方法,我开始也只是简单的把分页的html替换成上一页下一页的文字

后来又搜到可以自己定义一个类来完成这个需求,首先需要在config目录创建paginate.php,文件内容

'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(
                    '
    %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 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(
                    '
    %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教程

全国统一服务电话

400 890 5375

电子邮箱:879577@qq.com

公司地址:宜昌市西陵区黄河路5号三峡明珠10栋1051室

咨询微信

TEL:13680874598