thinkphp自定义分页

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

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

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

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

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

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

<?php return [ '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 <448901948@qq.com> // +----------------------------------------------------------------------  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自定义分页的详细内容,更多内容请关注技术你好其它相关文章!

来源链接:
免责声明:
1.资讯内容不构成投资建议,投资者应独立决策并自行承担风险
2.本文版权归属原作所有,仅代表作者本人观点,不代表本站的观点或立场
标签: ThinkPHP
上一篇:php获取远程图片并下载保存到本地的方法分析 下一篇:thinkphp5开启调试模式的方法

相关资讯