TP5 分页样式[通俗易懂]

TP5 分页样式[通俗易懂]自定义分页类放到extend\page\,这里也可以自己决定,命名空间对了就行    在extend\page\下新建Page.php把以下代码粘过去<?phpnamespacepage;//+———————————————————————-//|ThinkPHP[WECAN…

大家好,又见面了,我是你们的朋友全栈君。

自定义分页类 放到extend\page\,这里也可以自己决定,命名空间对了就行     在extend\page\下新建Page.php把以下代码粘过去

<?php
namespace page;
// +----------------------------------------------------------------------
// | 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>
// +----------------------------------------------------------------------
use think\Paginator;
class Page extends Paginator
{
    //首页
    protected function home() {
        if ($this->currentPage() > 1) {
            return "<a href='" . $this->url(1) . "' title='首页'>首页</a>";
        } else {
            return "<p>首页</p>";
        }
    }
    //上一页
    protected function prev() {
        if ($this->currentPage() > 1) {
            return "<a href='" . $this->url($this->currentPage - 1) . "' title='上一页'>上一页</a>";
        } else {
            return "<p>上一页</p>";
        }
    }
    //下一页
    protected function next() {
        if ($this->hasMore) {
            return "<a href='" . $this->url($this->currentPage + 1) . "' title='下一页'>下一页</a>";
        } else {
            return"<p>下一页</p>";
        }
    }
    //尾页
    protected function last() {
        if ($this->hasMore) {
            return "<a href='" . $this->url($this->lastPage) . "' title='尾页'>尾页</a>";
        } else {
            return "<p>尾页</p>";
        }
    }
    //统计信息
    protected function info(){
        return "<p class='pageRemark'>共<b>" . $this->lastPage .
            "</b>页<b>" . $this->total . "</b>条数据</p>";
    }
    /**
     * 页码按钮
     * @return string
     */
    protected function getLinks()
    {
        $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(
                    '%s<div class="pagination">%s %s %s</div>',
                    $this->css(),
                    $this->prev(),
                    $this->getLinks(),
                    $this->next()
                );
            } else {
                return sprintf(
                    '%s<div class="pagination">%s %s %s %s %s %s</div>',
                    $this->css(),
                    $this->home(),
                    $this->prev(),
                    $this->getLinks(),
                    $this->next(),
                    $this->last(),
                    $this->info()
                );
            }
        }
    }
    /**
     * 生成一个可点击的按钮
     *
     * @param  string $url
     * @param  int    $page
     * @return string
     */
    protected function getAvailablePageWrapper($url, $page)
    {
        return '<a href="' . htmlentities($url) . '" title="第"'. $page .'"页" >' . $page . '</a>';
    }
    /**
     * 生成一个禁用的按钮
     *
     * @param  string $text
     * @return string
     */
    protected function getDisabledTextWrapper($text)
    {
        return '<p class="pageEllipsis">' . $text . '</p>';
    }
    /**
     * 生成一个激活的按钮
     *
     * @param  string $text
     * @return string
     */
    protected function getActivePageWrapper($text)
    {
        return '<a href="" class="cur">' . $text . '</a>';
    }
    /**
     * 生成省略号按钮
     *
     * @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);
    }
    /**
     * 分页样式
     */
    protected function css(){
        return '  <style type="text/css">
                .pagination p{
                    margin:0;
                    cursor:pointer
                }
                .pagination{
                    height:40px;
                    padding:20px 0px;
                }
                .pagination a{
                    display:block;
                    float:left;
                    margin-right:10px;
                    padding:2px 12px;
                    height:24px;
                    border:1px #cccccc solid;
                    background:#fff;
                    text-decoration:none;
                    color:#808080;
                    font-size:12px;
                    line-height:24px;
                }
                .pagination a:hover{
                    color:#077ee3;
                    background: white;
                    border:1px #077ee3 solid;
                }
                .pagination a.cur{
                    border:none;
                    background:#077ee3;
                    color:#fff;
                }
                .pagination p{
                    float:left;
                    padding:2px 12px;
                    font-size:12px;
                    height:24px;
                    line-height:24px;
                    color:#bbb;
                    border:1px #ccc solid;
                    background:#fcfcfc;
                    margin-right:8px;
                }
                .pagination p.pageRemark{
                    border-style:none;
                    background:none;
                    margin-right:0px;
                    padding:4px 0px;
                    color:#666;
                }
                .pagination p.pageRemark b{
                    color:red;
                }
                .pagination p.pageEllipsis{
                    border-style:none;
                    background:none;
                    padding:4px 0px;
                    color:#808080;
                }
                .dates li {font-size: 14px;margin:20px 0}
                .dates li span{float:right}
            </style>';
    }
}

修改配置文件 config.php

//分页配置
    'paginate'               => [
        'type'      => 'page\Page',
        'var_page'  => 'page',
        'list_rows' => 15,
        'newstyle'  => true,
    ],

效果图

TP5 分页样式[通俗易懂]

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/160642.html原文链接:https://javaforall.net

(0)
上一篇 2022年7月17日 下午6:00
下一篇 2022年7月17日 下午6:16


相关推荐

  • Activiti7实战-入门

    Activiti7实战-入门Activiti7的使用和原理1.什么是工作流?1.1工作流介绍工作流:通过计算机对业务流程自动化执行管理。多个参与者按照某种预定规则自动进行传递文档、信息任务处理的过程。1.2工作流系统一个软件系统中具有工作流的功能,我们把它称为工作流系统。1.3工作流实现方式采用状态字段的值来跟踪流程的变化情况程序可以不用改变,业务流程可变化。1.4工作流原理分析2.什么是Activiti7Activiti是一个工作流引擎,activiti可以将业务系统中复杂的业务流程抽取

    2022年10月21日
    4
  • 如何理解极限的定义

    如何理解极限的定义极限是研究变量变化的过程,并通过变化的过程来把握变化的结果。一般来说一个函数某个点的结果是由函数确定了的,所以一个函数某个点的值一般就等于其极限。除非是提前,把那个点给挖走了,否则在那个变化过程中是没有什么办法能阻止变化的趋势的。但是也不能说极限就一定等于其函数值。 要理解好极限的定义,可以先从简单的,描述性的定义入手,然后再转到严格的数学定义上去。 描述性定义是这样的:当自变量x无

    2022年5月6日
    57
  • 安装tcping

    安装tcpingcping在windows和linux系统中都不是内建的命令,需要我们自己去下载下载命令wgethttps://sources.voidlinux.eu/tcping-1.3.5/tcping-1.3.5.tar.gzsudoyuminstallepel-releaseyuminstalltcpingtcpingwww.baidu.com443详细见图…

    2022年6月23日
    41
  • License授权方案「建议收藏」

    License授权方案「建议收藏」源码地址:https://github.com/sixj0/license解决的问题:将项目卖给其他公司,需要将jar包在客户的服务器上部署,为了避免客户将项目jar包进行二次售卖,或者…

    2022年7月26日
    18
  • C控制台应用程序

    C控制台应用程序使用 C 创建控制台应用程序的基本步骤 1 创建项目 2 编辑 C 源代码 3 编译运行 例题 在控制台输出 Helloworld 第一步 文件 新建 项目 选择 项目类型 为 VisualC 模板 为控制台应用程序 输入名称 例 1 1 选择存放路径 H C 程序 选择 创建新解决方案 第二步 编辑代码并保存

    2026年3月18日
    2
  • C语言详解 FILE文件操作

    C语言详解 FILE文件操作1 需要了解的概念需要理解的知识点包括 数据流 缓冲区 文件类型 文件存取方式 1 1 数据流 指程序与数据的交互是以流的形式进行的 进行 C 语言文件的存取时 都会先进行 打开文件 操作 这个操作就是在打开数据流 而 关闭文件 操作就是关闭数据流 1 2 缓冲区 Buffer 指在程序执行时 所提供的额外内存 可用来暂时存放做准备执行的数据 它的设置是为了提高存取效率 因为

    2026年3月17日
    1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号