ThinkPHP3.2 Ajax分页

来源:互联网 发布:linux httplib2 编辑:程序博客网 时间:2024/05/18 01:52
  1. TP官方出过一款内容管理系统,onthink.翻代码发现里面的分页写的真心不错,分享出来大家参考.
  2.   ---感谢麦当苗儿
  3. <?php  
  4. // +----------------------------------------------------------------------  
  5. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]  
  6. // +----------------------------------------------------------------------  
  7. // | Copyright (c) 2006-2013 http://thinkphp.cn All rights reserved.  
  8. // +----------------------------------------------------------------------  
  9. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )  
  10. // +----------------------------------------------------------------------  
  11. // | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>  
  12. // +----------------------------------------------------------------------  
  13. namespace Think;  
  14.   
  15. class AjaxPage{  
  16.     public $firstRow// 起始行数  
  17.     public $listRows// 列表每页显示行数  
  18.     public $parameter// 分页跳转时要带的参数  
  19.     public $totalRows// 总行数  
  20.     public $totalPages// 分页总页面数  
  21.     public $rollPage   = 11;// 分页栏每页显示的页数  
  22.     public $lastSuffix = true; // 最后一页是否显示总页数  
  23.   
  24.     private $p       = 'p'//分页参数名  
  25.     private $url     = ''//当前链接URL  
  26.     private $nowPage = 1;  
  27.   
  28.     // 分页显示定制  
  29.     private $config  = array(  
  30.         'header' => '<span class="rows">共 %TOTAL_ROW% 条记录</span>',  
  31.         'prev'   => '<<',  
  32.         'next'   => '>>',  
  33.         'first'  => '1...',  
  34.         'last'   => '...%TOTAL_PAGE%',  
  35.         'theme'  => '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%',  
  36.     );  
  37.   
  38.     /** 
  39.      * 架构函数 
  40.      * @param array $totalRows  总的记录数 
  41.      * @param array $listRows  每页显示记录数 
  42.      * @param array $parameter  分页跳转的参数 
  43.      */  
  44.     public function __construct($totalRows$listRows$parameter = array()) {  
  45.         C('VAR_PAGE') && $this->p = C('VAR_PAGE'); //设置分页参数名称  
  46.         /* 基础设置 */  
  47.         $this->totalRows  = $totalRows//设置总记录数  
  48.         $this->listRows   = $listRows;  //设置每页显示行数  
  49.         $this->parameter  = empty($parameter) ? $_GET : $parameter;  
  50.         $this->nowPage    = empty($_GET[$this->p]) ? 1 : intval($_GET[$this->p]);  
  51.         $this->firstRow   = $this->listRows * ($this->nowPage - 1);  
  52.     }  
  53.   
  54.     /** 
  55.      * 定制分页链接设置 
  56.      * @param string $name  设置名称 
  57.      * @param string $value 设置值 
  58.      */  
  59.     public function setConfig($name,$value) {  
  60.         if(isset($this->config[$name])) {  
  61.             $this->config[$name] = $value;  
  62.         }  
  63.     }  
  64.   
  65.     /** 
  66.      * 生成链接URL 
  67.      * @param  integer $page 页码 
  68.      * @return string 
  69.      */  
  70.     private function url($page){  
  71.         return str_replace(urlencode('[PAGE]'), $page$this->url);  
  72.     }  
  73.   
  74.     /** 
  75.      * 组装分页链接 
  76.      * @return string 
  77.      */  
  78.     public function show($c=CONTROLLER_NAME,$a=ACTION_NAME) {  
  79.         if(0 == $this->totalRows) return '';  
  80.   
  81.         /* 生成URL */  
  82.         $this->parameter[$this->p] = '[PAGE]';  
  83.         $this->url = U($c.'/'.$a$this->parameter);  
  84.         /* 计算分页信息 */  
  85.         $this->totalPages = ceil($this->totalRows / $this->listRows); //总页数  
  86.         if(!empty($this->totalPages) && $this->nowPage > $this->totalPages) {  
  87.             $this->nowPage = $this->totalPages;  
  88.         }  
  89.   
  90.         /* 计算分页零时变量 */  
  91.         $now_cool_page      = $this->rollPage/2;  
  92.         $now_cool_page_ceil = ceil($now_cool_page);  
  93.         $this->lastSuffix && $this->config['last'] = $this->totalPages;  
  94.   
  95.         //上一页  
  96.         $up_row  = $this->nowPage - 1;  
  97.         $up_page = $up_row > 0 ? '<a class="prev" href="javascript:void(0)" title="' . $up_row . '">' . $this->config['prev'] . '</a>' : '';  
  98.   
  99.         //下一页  
  100.         $down_row  = $this->nowPage + 1;  
  101.         $down_page = ($down_row <= $this->totalPages) ? '<a class="next" href="javascript:void(0)" title="' . $down_row . '">' . $this->config['next'] . '</a>' : '';  
  102.   
  103.         //第一页  
  104.         $the_first = '';  
  105.         if($this->totalPages > $this->rollPage && ($this->nowPage - $now_cool_page) >= 1){  
  106.             $the_first = '<a class="first" href="javascript:void(0)" title="' . 1 . '">' . $this->config['first'] . '</a>';  
  107.         }  
  108.   
  109.         //最后一页  
  110.         $the_end = '';  
  111.         if($this->totalPages > $this->rollPage && ($this->nowPage + $now_cool_page) < $this->totalPages){  
  112.             $the_end = '<a class="end" href="javascript:void(0)" title="' . $this->totalPages . '">' . $this->config['last'] . '</a>';  
  113.         }  
  114.   
  115.         //数字连接  
  116.         $link_page = "";  
  117.         for($i = 1; $i <= $this->rollPage; $i++){  
  118.             if(($this->nowPage - $now_cool_page) <= 0 ){  
  119.                 $page = $i;  
  120.             }elseif(($this->nowPage + $now_cool_page - 1) >= $this->totalPages){  
  121.                 $page = $this->totalPages - $this->rollPage + $i;  
  122.             }else{  
  123.                 $page = $this->nowPage - $now_cool_page_ceil + $i;  
  124.             }  
  125.             if($page > 0 && $page != $this->nowPage){  
  126.   
  127.                 if($page <= $this->totalPages){  
  128.                     $link_page .= '<a class="num" href="javascript:void(0)" title="' . $page . '">' . $page . '</a>';  
  129.                 }else{  
  130.                     break;  
  131.                 }  
  132.             }else{  
  133.                 if($page > 0 && $this->totalPages != 1){  
  134.                     $link_page .= '<span class="current">' . $page . '</span>';  
  135.                 }  
  136.             }  
  137.         }  
  138.   
  139.         //替换分页内容  
  140.         $page_str = str_replace(  
  141.             array('%HEADER%''%NOW_PAGE%''%UP_PAGE%''%DOWN_PAGE%''%FIRST%''%LINK_PAGE%''%END%''%TOTAL_ROW%''%TOTAL_PAGE%'),  
  142.             array($this->config['header'], $this->nowPage, $up_page$down_page$the_first$link_page$the_end$this->totalRows, $this->totalPages),  
  143.             $this->config['theme']);  
  144.         return "<div>{$page_str}</div>";  
  145.     }  
  146. }