侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

thinkphp自定义分页

ThinkPHP  /  管理员 发布于 8年前   339

ThinkPHP5.1内置了分页实现,要给数据添加分页输出功能变得非常简单,可以直接在Db类查询的时候调用paginate方法。本文为大家介绍了thinkphp自定义分页样式的方法。

thinkphp5.1有很方便的分页类,用render方法即可渲染分页的html代码

但"<<"的上一页和">>"这样的的下一页有时无法满足项目多变的需求,有必要自己定义分页的显示,比如

首页 上一页 1 2 3 ... 7 8 下一页 末页

这样,然而官方的手册并没有提到自定义分页样式的方法,我开始也只是简单的把分页的html替换成上一页下一页的文字

后来又搜到可以自己定义一个类来完成这个需求,首先需要在config目录创建paginate.php,文件内容

<?phpreturn ['type'=>'app\index\pager\gcudPager'//自己的分页类可以随便放,只要命名空间写对];

然后复制"项目目录\thinkphp\library\think\paginator\driver\Bootstrap.php"到一个任意位置,改改命名空间,把paginate.php的type改成相应的命名空间,比如我就把文件复制到了"项目目录\application\index\pager\gcudPager.php",上面的type也是和这个路径对应的,然后把命名空间改成了"app\index\pager",对应的类名改成了gcudPager,这样就可以自行定义分页的形式了

首页的实现我是按照上一页来的,复制它的代码,略加修改

    /**首页按钮     * @param string $text     * @return string     */    protected function GetFirstButton($text='首页'){        if ($this->currentPage() <= 1) {            return $this->getDisabledTextWrapper($text);        }        $url = $this->url(1);        return $this->getPageLinkWrapper($url, $text);    }

逻辑很简单,就是判断下当前页数,手动把页数变量设置为1,同理可以复制下一页的代码改成末页

    /**末页按钮     * @param string $text     * @return string     */    protected function GetLastButton($text='末页'){        if (!$this->hasMore) {            return $this->getDisabledTextWrapper($text);        }        $url = $this->url($this->lastPage());        return $this->getPageLinkWrapper($url, $text);    }

其它上一页下一页也就是改个文本太简单不说,render函数部分需要把首页和末页按钮加进来

    /**     * 渲染分页html     * @return mixed     */    public function render()    {        if ($this->hasPages()) {            if ($this->simple) {                return sprintf(                    '<ul class="pager">%s %s</ul>',                    $this->getPreviousButton(),                    $this->getNextButton()                );            } else {                return sprintf(                    '<div class="page-captions">%s %s %s %s %s</div>',                    $this->GetFirstButton(),                    $this->getPreviousButton(),                    $this->getLinks(),                    $this->getNextButton(),                    $this->GetLastButton()                );            }        }    }

这样就弄完了,调用部分完全不用改,最后放上完整代码

<?php// +----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK ]// +----------------------------------------------------------------------// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.// +----------------------------------------------------------------------// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )// +----------------------------------------------------------------------// | Author: zhangyajun <[email protected]>// +----------------------------------------------------------------------namespace app\index\pager;use think\Paginator;class gcudPager extends Paginator{    /**首页按钮     * @param string $text     * @return string     */    protected function GetFirstButton($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 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 GetLastButton($text='末页'){        if (!$this->hasMore) {            return $this->getDisabledTextWrapper($text);        }        $url = $this->url($this->lastPage());        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);    }    /**     * 页码按钮     * @return string     */    protected function getLinks()    {        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(                    '<ul class="pager">%s %s</ul>',                    $this->getPreviousButton(),                    $this->getNextButton()                );            } else {                return sprintf(                    '<div class="page-captions">%s %s %s %s %s</div>',                    $this->GetFirstButton(),                    $this->getPreviousButton(),                    $this->getLinks(),                    $this->getNextButton(),                    $this->GetLastButton()                );            }        }    }    /**     * 生成一个可点击的按钮     *     * @param  string $url     * @param  int    $page     * @return string     */    protected function getAvailablePageWrapper($url, $page)    {        return '<a href="' . htmlentities($url) . '">' . $page . '</a>';    }    /**     * 生成一个禁用的按钮     *     * @param  string $text     * @return string     */    protected function getDisabledTextWrapper($text)    {        return '<a>' . $text . '</a>';    }    /**     * 生成一个激活的按钮     *     * @param  string $text     * @return string     */    protected function getActivePageWrapper($text)    {        return '<span>' . $text . '</span>';    }    /**     * 生成省略号按钮     *     * @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 ($this->currentPage() == $page) {            return $this->getActivePageWrapper($page);        }        return $this->getAvailablePageWrapper($url, $page);    }}

推荐教程:thinkphp教程

以上就是thinkphp自定义分页的详细内容,更多请关注其它相关文章!


  • 上一条:
    thinkphp5开启调试模式的方法
    下一条:
    thinkphp中常用的提交表单的方法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • thinkphp + mongodb项目中数据加载慢问题分析及解决(0个评论)
    • thinkphp6框架中封装redis操作类(0个评论)
    • thinkphp6框架中实现定时任务功能流程步骤(0个评论)
    • Thinkphp5.1框架中实现Session+Redis会话共享流程步骤(0个评论)
    • TP5框架版本5.0.10安全漏洞根据官方补丁修复,也是本站安全漏洞修复(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-10
    • 2017-12
    • 2018-01
    • 2018-02
    • 2020-03
    • 2021-07
    • 2021-12
    • 2022-05
    • 2022-06
    • 2022-09
    • 2023-01
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客