thinkPHP框架可添加js事件的分页类customPage.class.php完整实例


Posted in PHP onMarch 16, 2017

本文实例讲述了thinkPHP框架可添加js事件的分页类customPage.class.php。分享给大家供大家参考,具体如下:

用于ajax动态加载数据的分页类,分页事件可以动态添加,去除了a链接中的href地址。

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------
namespace Think;
class customPage{
  public $firstRow; // 起始行数
  public $listRows; // 列表每页显示行数
  public $parameter; // 分页跳转时要带的参数
  public $totalRows; // 总行数
  public $totalPages; // 分页总页面数
  public $rollPage  = 6;// 分页栏每页显示的页数
  public $lastSuffix = true; // 最后一页是否显示总页数
  private $p    = 'p'; //分页参数名
  private $url   = ''; //当前链接URL
  private $nowPage = 1;
  // 分页显示定制
  private $config = array(
    'header' => '<span class="rows">共 %TOTAL_ROW% 条记录</span>',
    'prev'  => '上一页',
    'next'  => '下一页',
    'first' => '1...',
    'last'  => '...%TOTAL_PAGE%',
    'theme' => '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%',
  );
  /**
   * 架构函数
   * @param array $totalRows 总的记录数
   * @param array $listRows 每页显示记录数
   * @param array $parameter 分页跳转的参数
   */
  public function __construct($totalRows, $listRows=20, $parameter = array()) {
    C('VAR_PAGE') && $this->p = C('VAR_PAGE'); //设置分页参数名称
    /* 基础设置 */
    $this->totalRows = $totalRows; //设置总记录数
    $this->listRows  = $listRows; //设置每页显示行数
    $this->parameter = empty($parameter) ? $_GET : $parameter;
    $this->nowPage  = empty($_GET[$this->p]) ? 1 : intval($_GET[$this->p]);
    $this->totalPages = ceil($this->totalRows / $this->listRows); //总页数
    if ($this->nowPage>$this->totalPages) {
      $this->nowPage = $this->totalPages ;
    }
    $this->nowPage  = $this->nowPage>0 ? $this->nowPage : 1;
    $this->firstRow  = $this->listRows * ($this->nowPage - 1);
  }
  /**
   * 定制分页链接设置
   * @param string $name 设置名称
   * @param string $value 设置值
   */
  public function setConfig($name,$value) {
    if(isset($this->config[$name])) {
      $this->config[$name] = $value;
    }
  }
  /**
   * 生成链接URL
   * @param integer $page 页码
   * @return string
   */
  private function url($page){
    return str_replace(urlencode('[PAGE]'), $page, $this->url);
  }
  /**
   * 组装分页链接
   * @return string
   */
  public function show() {
    if(0 == $this->totalRows) return '';
    /* 生成URL */
    $this->parameter[$this->p] = '[PAGE]';
    $this->url = U(ACTION_NAME, $this->parameter);
    /* 计算分页信息 */
    if(!empty($this->totalPages) && $this->nowPage > $this->totalPages) {
      $this->nowPage = $this->totalPages;
    }
    /* 计算分页临时变量 */
    $now_cool_page   = $this->rollPage/2;
    $now_cool_page_ceil = ceil($now_cool_page);
    $this->lastSuffix && $this->config['last'] = $this->totalPages;
    //上一页
    $up_row = $this->nowPage - 1;
    $up_page = $up_row > 0 ? '<a class="shang" id="'.$up_row.'"href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >上一页</a>' : '';
    //下一页
    $down_row = $this->nowPage + 1;
    $down_page = ($down_row <= $this->totalPages) ? '<a class="xia" id="'.$down_row.'" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >下一页</a>' : '';
    //第一页
    $the_first = '';
    if($this->totalPages > $this->rollPage && ($this->nowPage - $now_cool_page) >= 1){
      $the_first = '<a class="first" id="1" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >第一页</a>';
    }
    //最后一页
    $the_end = '';
    //if($this->totalPages > $this->rollPage && ($this->nowPage + $now_cool_page) < $this->totalPages){
    if($this->totalPages > $this->rollPage && ($this->nowPage + $now_cool_page) < $this->totalPages){
      $the_end = '<a class="end" id="'.$this->totalRows.'" href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >最后一页</a>';
    }
    for($i = 1; $i <= $this->rollPage; $i++){
        if(($this->nowPage - $now_cool_page) <= 0 ){
          $page = $i;
        }elseif(($this->nowPage + $now_cool_page - 1) >= $this->totalPages){
          $page = $this->totalPages - $this->rollPage + $i;
        }else{
          $page = $this->nowPage - $now_cool_page_ceil + $i;
        }
        if($page > 0 && $page != $this->nowPage){
          if($page <= $this->totalPages){
            $link_page .= ('<li id="'.$page.'">'.$page.'</li>');
          }else{
            break;
          }
        }else{
          if($page > 0 && $this->totalPages != 1){
            $link_page .= ('<li id="now" id="'.$page.'" class="current">'.$page.'</li>');
          }
        }
    }
    if (!empty($link_page)) $link_page = '<ul id="list">'.$link_page.'</ul>';
    //替换分页内容
    $page_str = str_replace(
      array('%HEADER%', '%NOW_PAGE%', '%UP_PAGE%', '%DOWN_PAGE%', '%FIRST%', '%LINK_PAGE%', '%END%', '%TOTAL_ROW%', '%TOTAL_PAGE%'),
      array($this->config['header'], $this->nowPage, $up_page, $down_page, $the_first, $link_page, $the_end, $this->totalRows, $this->totalPages),
      $this->config['theme']);
    $page_str = trim($page_str);
    if(!empty($page_str)) $page_str .= '<p></p><input value="" type="text"><a href="javascript:;" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" id="go" class="go">跳转</a>';
    return $page_str;
  }
}

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

PHP 相关文章推荐
require(),include(),require_once()和include_once()区别
Mar 27 PHP
解析二进制流接口应用实例 pack、unpack、ord 函数使用方法
Jun 18 PHP
PHP循环函数使用介绍之PHP基础入门教程
Sep 21 PHP
php获取文件内容最后一行示例
Jan 09 PHP
PHP实现微信公众平台音乐点播
Mar 20 PHP
php基于双向循环队列实现历史记录的前进后退等功能
Aug 08 PHP
非常全面的php日期时间运算汇总
Nov 04 PHP
再推荐十款免费的php开发工具
Nov 09 PHP
PHP 根据key 给二维数组分组
Dec 09 PHP
Laravel 实现密码重置功能
Feb 23 PHP
PHP中将一个字符串部分字符用星号*替代隐藏的实现代码
Sep 08 PHP
laravel5.6实现数值转换
Oct 23 PHP
ThinkPHP3.2框架使用addAll()批量插入数据的方法
Mar 16 #PHP
Yii1.1中通过Sql查询进行的分页操作方法
Mar 16 #PHP
php生成无限栏目树
Mar 16 #PHP
CodeIgniter框架常见用法工作总结
Mar 16 #PHP
PHP常用算法和数据结构示例(必看篇)
Mar 15 #PHP
thinkPHP数据查询常用方法总结【select,find,getField,query】
Mar 15 #PHP
wordpress网站转移到本地运行测试的方法
Mar 15 #PHP
You might like
追求程序速度,而不是编程的速度
2008/04/23 PHP
drupal 代码实现URL重写
2011/05/04 PHP
php支付宝手机网页支付类实例
2015/03/04 PHP
thinkPHP5 ACL用户权限模块用法详解
2017/05/10 PHP
phpMyAdmin通过密码漏洞留后门文件
2018/11/20 PHP
HR vs ForZe BO3 第二场 2.13
2021/03/10 DOTA
用js 让图片在 div或dl里 居中,底部对齐
2008/01/21 Javascript
JavaScript 学习点滴记录
2009/04/24 Javascript
jQuery父级以及同级元素查找介绍
2013/09/04 Javascript
javascript实现yield的方法
2013/11/06 Javascript
在每个匹配元素的外部插入新元素的方法
2013/12/20 Javascript
jQuery中使用data()方法读取HTML5自定义属性data-*实例
2014/04/11 Javascript
js数组的基本操作(很全自己整理的)
2014/10/16 Javascript
JS访问SWF的函数用法实例
2015/07/01 Javascript
JS实现在状态栏显示打字效果完整实例
2015/11/02 Javascript
详解javascript new的运行机制
2016/01/26 Javascript
jQuery针对input的class属性写了多个值情况下的选择方法
2016/06/03 Javascript
JS移动端/H5同时选择多张图片上传并使用canvas压缩图片
2017/06/20 Javascript
详解react-webpack2-热模块替换[HMR]
2017/08/03 Javascript
Vue+element-ui 实现表格的分页功能示例
2018/08/18 Javascript
vue写h5页面的方法总结
2019/02/12 Javascript
详解小程序开发经验:多页面数据同步
2019/05/18 Javascript
小程序组件之自定义顶部导航实例
2019/06/12 Javascript
Python的装饰器用法学习笔记
2016/06/24 Python
numpy找出array中的最大值,最小值实例
2018/04/03 Python
Python 数据处理库 pandas 入门教程基本操作
2018/04/19 Python
基于python的多进程共享变量正确打开方式
2018/04/28 Python
Python3基于sax解析xml操作示例
2018/05/22 Python
pygame游戏之旅 如何制作游戏障碍
2018/11/20 Python
python+selenium实现自动化百度搜索关键词
2019/06/03 Python
解决pytorch GPU 计算过程中出现内存耗尽的问题
2019/08/19 Python
HTML文本属性&amp;颜色控制属性的实现
2019/12/17 HTML / CSS
实习心得体会
2014/01/02 职场文书
工作所在部门证明
2014/09/21 职场文书
参观邀请函范文
2015/02/02 职场文书
JS实现简单九宫格抽奖
2022/06/28 Javascript