PHP tp6 ThinkPHP6 实现自定义页码样式

实现的效果:

左边显示当前页面,总页数和总条数。

当选择第一页或者首页时,自动隐藏首页和上一页按钮,下一页和尾页显示并可以点击,当前页数变为橙色且不可点击。

当选择中间的页数时,选择的页码不可点击,首页和上一页,以及下一页和尾页都可以点击并跳转。

当选择尾页或者到达最后一页时,首页和上一页显示并可以点击,自动隐藏下一页和尾页按钮,且页码不可点击。

提示:此页码样式需要layui.css,我在文章末尾提供了一个链接,可以直接引用。

因为要更改tp6默认的页码样式,所以我们首先新建CustomPaginate.php文件,放在app/admin目录下。然后将以下代码插入到CustomPaginate.php中。

主要的改动:

增加了首页和尾页以及总页码。

// 首页
protected function getfirst($text="首页"){
    if ($this->currentPage() <= 1) {
        return null;
    }
    $url = $this->url(1);
    return $this->getPageLinkWrapper($url, $text);
}
// 尾页
protected function getlast($text="尾页"){
    if ($this->currentPage() == $this->lastPage) {
        return null;
    }
    $url = $this->url($this->lastPage);
    return $this->getPageLinkWrapper($url, $text);
}

// 总页码
protected function getTotal(){
    return '<span class="layui-btn layui-btn-primary" style="margin-right: 10px;">第'.$this->currentPage.'页,共'.$this->lastPage.'页/'.$this->total().'条数据</span>';
}

可点击按钮处的样式修改为

class="layui-btn"

 激活按钮改为

<span class="layui-btn layui-btn-danger">' . $text . '</span>

为了美观,我这里舍弃了禁用按钮,也就是前面提到的,当页码为第一页时,将首页和上一页的禁用按钮改为隐藏。当页码为最后一页时,将尾页和下一页的禁用按钮改为隐藏。

然后改动最大的是渲染分页处。

<div class="layui-form-item">
    <div class="layui-inline">
    %s
        <div class="layui-btn-group">
            %s %s %s %s %s
        </div>
    </div>
</div>',
$this->getTotal(),//总页数
$this->getfirst(), //首页
$this->getPreviousButton(),//上一页
$this->getLinks(),//页码
$this->getNextButton(),//下一页
$this->getlast()//尾页

全部源码:

<?php
namespace app\admin;
use think\Paginator;
class CustomPaginate extends Paginator
{

    /**
     * 上一页按钮
     * @param string $text
     * @return string|null
     */
    protected function getPreviousButton(string $text = "上一页"): ?string
    {

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

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

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

    /**
     * 下一页按钮
     * @param string $text
     * @return string|null
     */
    protected function getNextButton(string $text = '下一页'): ?string
    {
        if (!$this->hasMore) {
            return null;
        }

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

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

    /**
     * 页码按钮
     * @return string
     */
    protected function getLinks(): string
    {
        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(
                    '<div class="layui-form-item"><div class="layui-inline"><div class="layui-btn-group">%s %s</div></div></div>',
                    $this->getPreviousButton(),
                    $this->getNextButton()
                );
            } else {
                return sprintf(
                    '<div class="layui-form-item"><div class="layui-inline">%s<div class="layui-btn-group">%s %s %s %s %s</div></div></div>',
                    $this->getTotal(),
                    $this->getfirst(),
                    $this->getPreviousButton(),
                    $this->getLinks(),
                    $this->getNextButton(),
                    $this->getlast()
                );
            }
        }
    }
    // 首页
    protected function getfirst($text="首页"){
        if ($this->currentPage() <= 1) {
            return null;
        }
        $url = $this->url(1);
        return $this->getPageLinkWrapper($url, $text);
    }
    // 尾页
    protected function getlast($text="尾页"){
        if ($this->currentPage() == $this->lastPage) {
            return null;
        }
        $url = $this->url($this->lastPage);
        return $this->getPageLinkWrapper($url, $text);
    }
    // 总页码
    protected function getTotal(){
        return '<span class="layui-btn layui-btn-primary" style="margin-right: 10px;">第'.$this->currentPage.'页,共'.$this->lastPage.'页/'.$this->total().'条数据</span>';
    }
    /**
     * 生成一个可点击的按钮
     *
     * @param  string $url
     * @param  string $page
     * @return string
     */
    protected function getAvailablePageWrapper(string $url, string $page): string
    {
        return '<a href="' . htmlentities($url) . '" class="layui-btn">' . $page . '</a>';
    }

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

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

    /**
     * 生成省略号按钮
     *
     * @return string
     */
    protected function getDots(): string
    {
        return $this->getDisabledTextWrapper('...');
    }

    /**
     * 批量生成页码按钮.
     *
     * @param  array $urls
     * @return string
     */
    protected function getUrlLinks(array $urls): string
    {
        $html = '';

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

        return $html;
    }

    /**
     * 生成普通页码按钮
     *
     * @param  string $url
     * @param  string    $page
     * @return string
     */
    protected function getPageLinkWrapper(string $url, string $page): string
    {
        if ($this->currentPage() == $page) {
            return $this->getActivePageWrapper($page);
        }
        return $this->getAvailablePageWrapper($url, $page);
    }

}

然后在 app目录下的provider.php文件新加一句

'think\Paginator'   => app\admin\CustomPaginate::class

表示使用自定义页码文件。

然后在页面中需要这样引用这个页码样式。

{$pages|raw}

这个样式需要导入layui.css.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

慕慕慕慕公子

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

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

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

打赏作者

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

抵扣说明:

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

余额充值