ThinkPHP6自带分页定制,自定义分页类

2020-08-19   阅读:3424   分类:后端    标签: TP6

ThinkPHP6自定义分页类,按照官方文档说法:

如果你需要自定义分页,可以扩展一个分页驱动。

然后在provider.php定义文件中重新绑定

return [
    'think\Paginator'    =>    'app\common\Bootstrap'
];

1、接下来我们来自定义分页,效果

image.png

2、开始自定义我们的分页

2.1、在容器文件下重新绑定,加入分页类路径:

'think\Paginator' => '\app\common\paginator\AdminBootstrap',  //修改为分页类所在目录

image.png

2.2、在 app\common\paginator目录下(没有这个目录的话,可以自行创建),创建 AdminBootstrap.php文件。给该分页类写入如下代码:

注意使用 namespace app\common\paginator; 引入命名空间。

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2017 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: zhangyajun <448901948@qq.com>
// +----------------------------------------------------------------------
namespace app\common\paginator;
use think\Paginator;

class AdminBootstrap extends Paginator
{

    /**
     * <按钮
     * @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);
    }



    //总数标签
    protected  function totalshow()
    {

        $totalhtml="<li class=\"disabled\"><span>共".$this->total."条记录&nbsp&nbsp第".$this->currentPage()."页/共".$this->lastPage()."页</span></li>";
        return $totalhtml;

    }

    //尾页标签

    protected  function showlastpage($text = '尾页')
    {

        if($this->currentPage()==$this->lastPage())
        {
            return $this->getDisabledTextWrapper($text);

        }

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

    //首页标签

    protected  function showfirstpage($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 getNextButton($text = '»')
    {
        if (!$this->hasMore) {
            return $this->getDisabledTextWrapper($text);
        }

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

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

    //跳转到哪页
    protected  function gopage()
    {


        return $gotohtml="<li><form action='' method='get' style='display: inline-block'><a style='float:left;margin-left:2px;'><span style='color: #777;'>到</span> <input style='height:30px; display: inline-block; width: 40px; text-align: center; margin-right:2px;color: #777;' type='text' name='page' value='1' class='form-control'> <span style='color: #777;'>页</span> <button type='button' class='btn btn-sm btn-default btn-outline' style='color: #777'>确定</button> </a></form></li>";
        // return $totalhtml;;

    }

    /**
     * 页码按钮
     * @return string
     */
    protected function getLinks()
    {
        if ($this->simple)
            return '';

        $block = [
            'first'  => null,
            'slider' => null,
            'last'   => null
        ];

        $side   = 2;
        $window = $side * 2;

        if ($this->lastPage < $window +1) {
            $block['slider'] = $this->getUrlRange(1, $this->lastPage);

        } elseif ($this->currentPage <= $window-1) {

            $block['slider'] = $this->getUrlRange(1, $window + 1);
        } elseif ($this->currentPage > ($this->lastPage - $window+1)) {
            $block['slider']  = $this->getUrlRange($this->lastPage - ($window), $this->lastPage);

        } else {

            $block['slider'] = $this->getUrlRange($this->currentPage - $side, $this->currentPage + $side);
        }

        $html = '';

        if (is_array($block['first'])) {
            $html .= $this->getUrlLinks($block['first']);
        }

        if (is_array($block['slider'])) {

            $html .= $this->getUrlLinks($block['slider']);
        }

        if (is_array($block['last'])) {
            $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 %s</ul>',

                    $this->getPreviousButton(),
                    $this->getNextButton()
                );
            } else {
                return sprintf(
                    '<ul class="pagination"> %s %s %s %s %s %s %s </ul>',
                    //显示数量页码信息
                    $this->totalshow(),
                    //第一页
                    $this->showfirstpage(),
                    //<
                    $this->getPreviousButton(),
                    //页码
                    $this->getLinks(),
                    //>
                    $this->getNextButton(),
                    //最后一页
                    $this->showlastpage(),
                //最后再加个参数 %s 可以显示跳转到哪页
                    $this->gopage()
                );
            }
        }
    }

    /**
     * 生成一个可点击的按钮
     *
     * @param  string $url
     * @param  int    $page
     * @return string
     */
    protected function getAvailablePageWrapper($url, $page)
    {
        return '<li><a href="' . htmlentities($url) . '">' . $page . '</a></li>';
    }

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

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

    /**
     * 生成省略号按钮
     *
     * @return string
     */
    protected function getDots($text = '...')
    {

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

        //  return $this->getPageLinkWrapper($url, $text);
        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 ($page == $this->currentPage()) {
            return $this->getActivePageWrapper($page);
        }

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

至此,完成了我们的自定义分页类。

2.3、使用分页

    public function admin_list()
    {
        $admin_list=Adminlist::order('id', 'desc')->paginate(1);
        View::assign([
            'admin_list'  =>$admin_list,
        ]);
        return View::fetch();
    }
 <ul class="pagination">
      {$admin_list|raw}
 </ul>


【腾讯云】 爆款2核2G3M云服务器首年 61元,2核2G4M云服务器新老同享 99元/年,续费同价

‘简忆博客’微信公众号 扫码关注‘简忆博客’微信公众号,获取最新文章动态
转载:请说明文章出处“来源简忆博客”。http://www.tpxhm.com/adetail/386.html

×
觉得文章有用就打赏一下文章作者
微信扫一扫打赏 微信扫一扫打赏
支付宝扫一扫打赏 支付宝扫一扫打赏

文章评论(4)

登录

华诚2023-09-19 16:49:55
你好,按照你的步骤重写了paginator类,可按“确定”按钮无法进行页面跳转,其它模块均正常使用,求解?谢谢!回复

2023-09-20 10:03:53
您用的是哪个版本的ThinkPHP,看下分页类名称是否有改名。回复

逆流2023-12-10 15:30:51
把按钮 类型改成submit 或者自己写js 提交form回复

简忆博客壁纸一
简忆博客壁纸二
简忆博客壁纸三
简忆博客壁纸四
简忆博客壁纸五
简忆博客壁纸六
简忆博客壁纸七
简忆博客壁纸八
头像

简忆博客
勤于学习,乐于分享

置顶推荐

打赏本站

如果你觉得本站很棒,可以通过扫码支付打赏哦!
微信扫码:你说多少就多少~
微信扫码
支付宝扫码:你说多少就多少~
支付宝扫码
×