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);
    }

    /**
     * 下一页按钮
     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

慕慕慕慕公子

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

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

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

打赏作者

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

抵扣说明:

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

余额充值