ThinkPHP自定义分页模板

默认分页模板

这里写图片描述

自定义分页模板

这里写图片描述

<?php

namespace tool;

use think\Paginator;

class TpPageHelper extends Paginator
{
    //TODO : 中间按钮个数
    protected $bnumber = 10;

    //TODO : 首页
    protected function getfirstButton($str = '')
    {
        if ($this->currentPage() <= 1) {
            return $this->getDisabledTextWrapper($str);
        }

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

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

    //TODO : 上一页
    protected function getPreviousButton($text = "&laquo;")
    {

        if ($this->currentPage() <= 1) {
            return $this->getDisabledTextWrapper($text);
        }

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

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

    //TODO : 页码
    protected function getLinks()
    {
        if ($this->total > $this->listRows) {
            if ($this->lastPage < $this->bnumber) {
                return $this->getUrlLinks($this->getUrlRange(1, $this->lastPage));
            } else {
                $min = 1;
                if ($this->currentPage > $this->bnumber / 2) $min = $this->currentPage - floor($this->bnumber / 2);
                if ($this->lastPage - $this->currentPage < $this->bnumber / 2) $min = $this->lastPage - $this->bnumber + 1;
                return $this->getUrlLinks($this->getUrlRange($min, $min + $this->bnumber - 1));
            }
        }
    }

    //TODO : 下一页
    protected function getNextButton($text = '&raquo;')
    {
        if (!$this->hasMore) {
            return $this->getDisabledTextWrapper($text);
        }

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

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

    //TODO : 末页
    protected function getlastButton($text = '')
    {
        if (!$this->hasMore) {
            return $this->getDisabledTextWrapper($text);
        }

        $url = $this->url($this->lastPage());

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

    //TODO : 渲染页
    public function render()
    {
        //数据是否足够分页
        if ($this->hasPages()) {
            return sprintf(
                '<ul class="btn-item fr">%s %s %s %s %s</ul>',
                $this->getfirstButton('首页'),
                $this->getPreviousButton('上一页'),
                $this->getLinks(),
                $this->getNextButton('下一页'),
                $this->getlastButton('末页')
            );
        }
    }

    //TODO : 生成禁用按钮
    protected function getDisabledTextWrapper($text)
    {
        return '<li class="disabled"><span>' . $text . '</span></li>';
    }

    //TODO : 生成普通按钮
    protected function getPageLinkWrapper($url, $page)
    {
        if ($page == $this->currentPage()) {
            return $this->getActivePageWrapper($page);
        }

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

    //TODO : 生成当前页按钮
    protected function getActivePageWrapper($text)
    {
        return '<li class="active"><span>' . $text . '</span></li>';
    }

    //TODO : 可点击按钮
    protected function getAvailablePageWrapper($url, $page)
    {
        return '<li><a href="' . htmlentities($url) . '">' . $page . '</a></li>';
    }

    //TODO : 批量生成页码按钮
    protected function getUrlLinks(array $urls)
    {
        $html = '';

        foreach ($urls as $page => $url) {
            $html .= $this->getPageLinkWrapper($url, $page);
        }

        return $html;
    }

}
  • 将 TpPageHelper.php 放在extend\tool下

这里写图片描述

  • 修改默认模板

这里写图片描述

相关阅读
  • 尽量不要有 最后一页, 跳到XXX页(一般只关心前几页的搜索结果)
  • MySQL分页优化
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

伟洪winni

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

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

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

打赏作者

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

抵扣说明:

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

余额充值