当前位置:Gxlcms > php框架 > thinkPHP框架可添加js事件的分页类customPage.class.php完整实例

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

时间:2021-07-01 10:21:17 帮助过:34人阅读

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

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

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

更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《thinkPHP模板操作技巧总结》、《ThinkPHP常用方法总结》、《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《Zend FrameWork框架入门教程》、《smarty模板入门基础教程》及《PHP模板技术总结》。

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

人气教程排行