TP5分页样式修改

在config里的分页设置

'paginate'               => [
        'type'      => 'bootstrap5',
        'var_page'  => 'page',
        'list_rows' => 15,
        'page_size'=>5, //页码数量
        'page_button'=>[
            'total_rows'=>true, //是否显示总条数
            'turn_page'=>true, //上下页按钮
            'turn_group'=>true, //上下组按钮
            'first_page'=>true, //首页
            'last_page'=>true, //尾页
        ]
    ],

新建一个bootstrap5的页面,在thinkphp/library/think/paginator/driver 里面

<?php

/**
 * 分页
 * 按照段分页
 * 
 * 例1:1-5,4-8,7-11,...
 * 在第一段时:点击5时跳到下一段
 * 在第二段时:点击8时跳到下一段,点击4时回到上一段
 * 
 * 例2:1-7,6-12,11-17,...
 * 在第二段时:点击12时跳到下一段点击6时回到上一段
 * 在第三段时:点击17时跳到下一段,点击11时回到上一段
 * 
 * 
 */
namespace think\paginator\driver;
use think\Config;
use think\Paginator;

class Bootstrap5 extends Paginator
{

    public $rollPage=3;//分页栏每页显示的页数
    
    public $showPage=10;//总页数超过多少条时显示的首页末页
    //修改分页 计算总数
    protected function getTotal(){
        $html='<li><span>共<b>' . $this->lastPage .
            '</b>页'.$this->total().'条数据</span></li>';
        
        return $html;
    }
    /**
     * 上一页按钮
     * @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 getNextButton($text = '下一页')
    {
        if (!$this->hasMore) {
            return $this->getDisabledTextWrapper($text);
        }

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

        return $this->getPageLinkWrapper($url, $text);
    }
    
    /**
     * 首页按钮
     * @param string $text
     * @return string
     */
    protected function getFirstButton($text = '首页')
    {
        // $nowPage = $this->rollPage;//计算分页临时变量
        // //当  总页数大于定义的页数时  且  当前页数大于前几页时  显示首页
        // if ($this->lastPage > $this->showPage && $this->currentPage > $nowPage) {
            
        //     $url = $this->url(1);
            
        //     return $this->getPageLinkWrapper($url, $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 getLastButton($text = '末页')
    {
        $nowPage = $this->rollPage;//计算分页临时变量
        
        //当  总页数大于定义的页数时  且  当前页数小于最后的几页时  显示末页
        if ($this->lastPage > $this->showPage && $this->currentPage < ($this->lastPage - $nowPage)) {
            
            $url = $this->url($this->lastPage);
            
            return $this->getPageLinkWrapper($url, $text);
        }
    }
    //统计信息
    protected function info(){
        return "<p class='pageRemark'>共<b>" . $this->lastPage .
            "</b>页<b>" . $this->total . "</b>条数据</p>";
    }
    
    /**
     * 页码按钮
     * @return string
     */
    protected function getLinks()
    {
        if ($this->simple)
            return '';

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

        $rollPage = $this->rollPage;//分页栏每页显示的页数
        $nowPage = floor($rollPage/2);//计算分页临时变量
        
        if($this->lastPage <= $rollPage){
            $block['first'] = $this->getUrlRange(1, $this->lastPage);
        }else if($this->currentPage==0 || $this->currentPage<$rollPage){
            $block['first'] = $this->getUrlRange(1, $rollPage);
        }else{
            $n=floor(($this->currentPage+($rollPage-4))/($rollPage-2));
            $start=$n*($rollPage-2)-($rollPage-3);
            $end=$start+$rollPage-1;
            $end=$end>$this->lastPage ? $this->lastPage : $end;
            $block['first'] = $this->getUrlRange($start,$end);
        }
        $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 %s %s %s</ul>',
                    $this->getFirstButton(),
                    $this->getPreviousButton(),
                    $this->getLinks(),
                    $this->getNextButton(),
                    $this->getLastButton()
                );
            } else {
                $btn_cfg=Config::get('paginate.page_button');
                $btn_str='<ul class="pagination">';
                
                $btn_str.=$btn_cfg['first_page']?'%s2':'';
                $btn_str.=$btn_cfg['turn_page']?'%s3':'';
                $btn_str.='%s4';
                $btn_str.=$btn_cfg['turn_page']?'%s5':'';
                $btn_str.=$btn_cfg['last_page']?'%s6':'';
                $btn_str.=$btn_cfg['total_rows']?'%s1':'';
                $page_str=str_replace(array('%s1','%s2','%s3','%s4','%s5','%s6'),array($this->getTotal(),$this->getFirstButton(),$this->getPreviousButton(),$this->getLinks(),$this->getNextButton(),$this->getLastButton()),$btn_str);
                return $page_str;
                /*return sprintf(
                    '<ul class="pagination">%s %s %s %s</ul>',
                    $this->getTotal(),
                    $this->getPreviousButton(),
                    $this->getLinks(),
                    $this->getNextButton()
                );*/
            }
        }else{
            return sprintf(
                    '<ul class="pagination">%s %s %s %s %s</ul>',
                    $this->getFirstButton(),
                    $this->getPreviousButton(),
                    $this->getLinks(),
                    $this->getNextButton(),
                    $this->getLastButton()
                );
        }
    }

    /**
     * 生成一个可点击的按钮
     *
     * @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()
    {
        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);
    }
}

测试没问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时间轴-小文同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值