PageAjax.class.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. // ================================================================================
  3. // ██╗ ██╗██╗ ██╗██╗
  4. // ██║ ██║╚██╗ ██╔╝██║
  5. // ███████║ ╚████╔╝ ██║
  6. // ██╔══██║ ╚██╔╝ ██║
  7. // ██║ ██║ ██║ ███████╗
  8. // ╚═╝ ╚═╝ ╚═╝ ╚══════╝
  9. // 2018-02-13
  10. // ================================================================================
  11. /* 描述:php 异步分页
  12. * 技术:voidx()
  13. * 目的:无刷新分页+传递参数
  14. */
  15. namespace Think;
  16. class PageAjax{
  17. public $firstRow; // 起始行数
  18. public $listRows; // 列表每页显示行数
  19. public $parameter; // 分页跳转时要带的参数
  20. public $totalRows; // 总行数
  21. public $totalPages; // 分页总页面数
  22. public $rollPage = 5;// 分页栏每页显示的页数
  23. public $lastSuffix = true; // 最后一页是否显示总页数
  24. private $p = 'p'; //分页参数名
  25. private $url = ''; //当前链接URL
  26. private $nowPage = 1;
  27. private $module = 1;
  28. // 分页显示定制
  29. private $config = array(
  30. 'header' => '<span class="rows">共 %TOTAL_ROW% 条记录</span>',
  31. 'first' => '首页',
  32. 'prev' => '上一页',
  33. 'next' => '下一页',
  34. 'last' => '末页',
  35. 'theme' => '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%',
  36. );
  37. /**
  38. * 架构函数
  39. * @param array $totalRows 总的记录数
  40. * @param array $listRows 每页显示记录数
  41. * @param array $parameter 分页跳转的参数
  42. * @param array $module div的ID
  43. */
  44. public function __construct($totalRows, $listRows=20, $parameter = array(),$module='#bm-data') {
  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->nowPage = $this->nowPage>0 ? $this->nowPage : 1;
  52. $this->firstRow = $this->listRows * ($this->nowPage - 1);
  53. $this->module =$module;
  54. }
  55. /**
  56. * 定制分页栏每页显示的页数
  57. * @param int $rollPage 设置值
  58. */
  59. public function setRollPage($rollPage) {
  60. if(isset($rollPage)){
  61. $this->rollPage = $rollPage;
  62. }
  63. }
  64. /**
  65. * 定制分页链接设置
  66. * @param string $name 设置名称
  67. * @param string $value 设置值
  68. */
  69. public function setConfig($name,$value) {
  70. if(isset($this->config[$name])) {
  71. $this->config[$name] = $value;
  72. }
  73. }
  74. /**
  75. * 生成链接URL
  76. * @param integer $page 页码
  77. * @return string
  78. */
  79. private function url($page){
  80. return str_replace(urlencode('[PAGE]'), $page, $this->url).$this->module;
  81. }
  82. /**
  83. * 组装分页链接
  84. * @return string
  85. */
  86. public function show() {
  87. if(0 == $this->totalRows) return '';
  88. /* 生成URL */
  89. $this->parameter[$this->p] = '[PAGE]';
  90. $this->url = U(ACTION_NAME, $this->parameter);
  91. /* 计算分页信息 */
  92. $this->totalPages = ceil($this->totalRows / $this->listRows); //总页数
  93. if(!empty($this->totalPages) && $this->nowPage > $this->totalPages) {
  94. $this->nowPage = $this->totalPages;
  95. }
  96. /* 计算分页零时变量 */
  97. $now_cool_page = $this->rollPage/2;
  98. $now_cool_page_ceil = ceil($now_cool_page);
  99. //上一页
  100. $up_row = $this->nowPage - 1;
  101. $up_page = $up_row > 0 ? '<a class="prev" href="javascript:voids(\'' .$this->url($up_row). '\')">' . $this->config['prev'] . '</a>' : '<a class="prev" href="javascript:;">' . $this->config['prev'] . '</a>';
  102. //下一页
  103. $down_row = $this->nowPage + 1;
  104. $down_page = ($down_row <= $this->totalPages) ? '<a class="next" href="javascript:voids(\''.$this->url($down_row). '\')">' . $this->config['next'] . '</a>' : '<a class="next" href="javascript:;">' . $this->config['next'] . '</a>';
  105. //第一页
  106. $the_first = '<a class="first" href="javascript:voids(\''.$this->url(1).'\')">' . $this->config['first'] . '</a>';
  107. //最后一页
  108. $the_end = '<a class="end" href="javascript:voids(\'' .$this->url($this->totalPages). '\')">' . $this->config['last'] . '</a>';
  109. //数字连接
  110. $link_page = "";
  111. for($i = 1; $i <= $this->rollPage; $i++){
  112. if(($this->nowPage - $now_cool_page) <= 0 ){
  113. $page = $i;
  114. }elseif(($this->nowPage + $now_cool_page - 1) >= $this->totalPages){
  115. $page = $this->totalPages - $this->rollPage + $i;
  116. }else{
  117. $page = $this->nowPage - $now_cool_page_ceil + $i;
  118. }
  119. if ($page>0) {
  120. if($page != $this->nowPage){
  121. if($page <= $this->totalPages){
  122. $link_page .= '<a class="num" href="javascript:voids(\''.$this->url($page).'\')">' . $page . '</a>';
  123. }else{
  124. break;
  125. }
  126. }else{
  127. $link_page .= '<span class="current">' . $page . '</span>';
  128. }
  129. }
  130. }
  131. //替换分页内容
  132. $page_str = str_replace(
  133. array('%HEADER%', '%NOW_PAGE%', '%UP_PAGE%', '%DOWN_PAGE%', '%FIRST%', '%LINK_PAGE%', '%END%', '%TOTAL_ROW%', '%TOTAL_PAGE%'),
  134. array($this->config['header'], $this->nowPage, $up_page, $down_page, $the_first, $link_page, $the_end, $this->totalRows, $this->totalPages),
  135. $this->config['theme']);
  136. return '<div class="page">'.$page_str.'</div>';
  137. }
  138. }