AjaxPage.class.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 AjaxPage{
  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. // 分页显示定制
  28. private $config = array(
  29. 'header' => '<span class="rows">共 %TOTAL_ROW% 条记录</span>',
  30. 'first' => '首页',
  31. 'prev' => '上一页',
  32. 'next' => '下一页',
  33. 'last' => '末页',
  34. 'theme' => '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%',
  35. );
  36. /**
  37. * 架构函数
  38. * @param array $totalRows 总的记录数
  39. * @param array $listRows 每页显示记录数
  40. * @param array $parameter 分页跳转的参数
  41. */
  42. public function __construct($totalRows, $listRows=20, $parameter = array()) {
  43. C('VAR_PAGE') && $this->p = C('VAR_PAGE'); //设置分页参数名称
  44. /* 基础设置 */
  45. $this->totalRows = $totalRows; //设置总记录数
  46. $this->listRows = $listRows; //设置每页显示行数
  47. $this->parameter = empty($parameter) ? $_GET : $parameter;
  48. $this->nowPage = empty($_GET[$this->p]) ? 1 : intval($_GET[$this->p]);
  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. $this->totalPages = ceil($this->totalRows / $this->listRows); //总页数
  81. if(!empty($this->totalPages) && $this->nowPage > $this->totalPages) {
  82. $this->nowPage = $this->totalPages;
  83. }
  84. /* 计算分页零时变量 */
  85. $now_cool_page = $this->rollPage/2;
  86. $now_cool_page_ceil = ceil($now_cool_page);
  87. //上一页
  88. $up_row = $this->nowPage - 1;
  89. $up_page = $up_row > 0 ? '<a class="prev" href="javascript:voidx(' .$up_row. ')">' . $this->config['prev'] . '</a>' : '<a class="prev" href="javascript:;">' . $this->config['prev'] . '</a>';
  90. //下一页
  91. $down_row = $this->nowPage + 1;
  92. $down_page = ($down_row <= $this->totalPages) ? '<a class="next" href="javascript:voidx('.$down_row. ')">' . $this->config['next'] . '</a>' : '<a class="next" href="javascript:;">' . $this->config['next'] . '</a>';
  93. //第一页
  94. $the_first = '<a class="first" href="javascript:voidx(1)">' . $this->config['first'] . '</a>';
  95. //最后一页
  96. $the_end = '<a class="end" href="javascript:voidx(' .$this->totalPages. ')">' . $this->config['last'] . '</a>';
  97. //数字连接
  98. $link_page = "";
  99. for($i = 1; $i <= $this->rollPage; $i++){
  100. if(($this->nowPage - $now_cool_page) <= 0 ){
  101. $page = $i;
  102. }elseif(($this->nowPage + $now_cool_page - 1) >= $this->totalPages){
  103. $page = $this->totalPages - $this->rollPage + $i;
  104. }else{
  105. $page = $this->nowPage - $now_cool_page_ceil + $i;
  106. }
  107. if ($page>0) {
  108. if($page != $this->nowPage){
  109. if($page <= $this->totalPages){
  110. $link_page .= '<a class="num" href="javascript:voidx('.$page .')">' . $page . '</a>';
  111. }else{
  112. break;
  113. }
  114. }else{
  115. $link_page .= '<span class="current">' . $page . '</span>';
  116. }
  117. }
  118. }
  119. //替换分页内容
  120. $page_str = str_replace(
  121. array('%HEADER%', '%NOW_PAGE%', '%UP_PAGE%', '%DOWN_PAGE%', '%FIRST%', '%LINK_PAGE%', '%END%', '%TOTAL_ROW%', '%TOTAL_PAGE%'),
  122. array($this->config['header'], $this->nowPage, $up_page, $down_page, $the_first, $link_page, $the_end, $this->totalRows, $this->totalPages),
  123. $this->config['theme']);
  124. return '<div class="page">'.$page_str.'</div>';
  125. }
  126. }