thinkPHP5框架分页样式类完整示例


Posted in PHP onSeptember 01, 2018

本文实例讲述了thinkPHP5分页样式类。分享给大家供大家参考,具体如下:

在配置文件中改路径

把这段代码放入extend文件下 可以在这个文件下创建个page文件

在分页的控制器方法中

->paginate(12,false,[ 'type'=> 'page\Page','var_page'=>'page']);
<?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) . '" rel="external nofollow" 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=" rel="external nofollow" 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;
        border:1px #cccccc solid;
        background:#fff;
        text-decoration:none;
        color:#808080;
        font-size:12px;
        line-height:24px;
      }
      .pagination a:hover{
        color:#009688;
        background: white;
        border:1px #009688 solid;
      }
      .pagination a.cur{
        border:none;
        background:#009688;
        color:#fff;
      }
      .pagination p{
        float:left;
        padding:2px 12px;
        font-size:12px;
        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>';
  }
}

希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。

PHP 相关文章推荐
自己动手做一个SQL解释器
Oct 09 PHP
php email邮箱正则
Oct 08 PHP
php小偷相关截取函数备忘
Nov 28 PHP
如何在smarty中增加类似foreach的功能自动加载数据
Jun 26 PHP
destoon实现商铺管理主页设置增加新菜单的方法
Jun 26 PHP
js代码实现微博导航栏
Jul 30 PHP
浅谈PHP检查数组中是否存在某个值 in_array 函数
Jun 13 PHP
原生php实现excel文件读写的方法分析
Apr 25 PHP
php使用QueryList轻松采集js动态渲染页面方法
Sep 11 PHP
PHP+Ajax简单get验证操作示例
Mar 02 PHP
使用 PHP Masked Package 屏蔽敏感数据的实现方法
Oct 15 PHP
PHP论坛实现积分系统的思路代码详解
Jun 01 PHP
php操作mongodb封装类与用法实例
Sep 01 #PHP
thinkphp集成前端脚手架Vue-cli的教程图解
Aug 30 #PHP
Django中通过定时任务触发页面静态化的处理方式
Aug 29 #PHP
PHP使用SMTP邮件服务器发送邮件示例
Aug 28 #PHP
PHP实现websocket通信的方法示例
Aug 28 #PHP
Yii2压缩PHP中模板代码的输出问题
Aug 28 #PHP
PHP实现的XXTEA加密解密算法示例
Aug 28 #PHP
You might like
比较全的PHP 会话(session 时间设定)使用入门代码
2008/06/05 PHP
兼容PHP5的PHP目录管理函数库
2008/07/10 PHP
PHP反射使用实例和PHP反射API的中文说明
2014/07/02 PHP
smarty高级特性之对象的使用方法
2015/12/25 PHP
Zend Framework教程之模型Model用法简单实例
2016/03/04 PHP
[JS源码]超长文章自动分页(客户端版)
2007/01/09 Javascript
Track Image Loading效果代码分析
2007/08/13 Javascript
Javascript 去除数组的重复元素
2010/05/04 Javascript
IE6浏览器下resize事件被执行了多次解决方法
2012/12/11 Javascript
onkeydown事件解决按回车键直接提交数据的需求
2013/04/11 Javascript
JSONP跨域的原理解析及其实现介绍
2014/03/22 Javascript
jQuery中even选择器的定义和用法
2014/12/23 Javascript
JS+CSS实现TreeMenu二级树形菜单完整实例
2015/09/18 Javascript
用nodeJS搭建本地文件服务器的几种方法小结
2017/03/16 NodeJs
AngularJs导出数据到Excel的示例代码
2017/08/11 Javascript
利用nginx + node在阿里云部署https的步骤详解
2017/12/19 Javascript
nodejs(officegen)+vue(axios)在客户端导出word文档的方法
2018/07/31 NodeJs
javascript实现移动端触屏拖拽功能
2020/07/29 Javascript
JS检测浏览器开发者工具是否打开的方法详解
2020/10/02 Javascript
[47:52]DOTA2-DPC中国联赛正赛 iG vs LBZS BO3 第二场 3月4日
2021/03/11 DOTA
Python ljust rjust center输出
2008/09/06 Python
python Django框架实现自定义表单提交
2016/03/25 Python
Python编程中实现迭代器的一些技巧小结
2016/06/21 Python
Python实现的多进程和多线程功能示例
2018/05/29 Python
flask-restful使用总结
2018/12/04 Python
python3.x提取中文的正则表达式示例代码
2019/07/23 Python
python 判断三个数字中的最大值实例代码
2019/07/24 Python
Pytorch的mean和std调查实例
2020/01/02 Python
Python自动发送和收取邮件的方法
2020/08/12 Python
加拿大约会网站:EliteSingles.ca
2018/01/12 全球购物
英国现代家具和装饰网站:PN Home
2018/08/16 全球购物
通息工程毕业生自荐信
2013/10/16 职场文书
校运会入场式解说词
2014/02/10 职场文书
开工典礼致辞
2015/07/29 职场文书
MySQL命令行操作时的编码问题详解
2021/04/14 MySQL
分享一个vue实现的记事本功能案例
2022/04/11 Vue.js