Açıklama Yok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AjaxPage.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace think;
  3. class AjaxPage{
  4. public $firstRow; // 起始行数
  5. public $listRows; // 列表每页显示行数
  6. public $parameter; // 分页跳转时要带的参数
  7. public $totalRows; // 总行数
  8. public $totalPages; // 分页总页面数
  9. public $rollPage = 11;// 分页栏每页显示的页数
  10. public $lastSuffix = true; // 最后一页是否显示总页数
  11. private $p = 'p'; //分页参数名
  12. private $url = ''; //当前链接URL
  13. private $nowPage = 1;
  14. // 分页显示定制
  15. private $config = array(
  16. 'header' => '<span class="rows">共 %TOTAL_ROW% 条记录</span>',
  17. /*
  18. 'prev' => '<<',
  19. 'next' => '>>',
  20. 'first' => '1...',
  21. 'last' => '...%TOTAL_PAGE%',
  22. */
  23. 'prev' => '上一页',
  24. 'next' => '下一页',
  25. 'first' => '首页',
  26. 'last' => '尾页',
  27. 'theme' => '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%',
  28. );
  29. /**
  30. * 架构函数
  31. * @param array $totalRows 总的记录数
  32. * @param array $listRows 每页显示记录数
  33. * @param array $parameter 分页跳转的参数
  34. */
  35. public function __construct($totalRows, $listRows=20, $parameter = array()) {
  36. // config('VAR_PAGE') && $this->p = config('VAR_PAGE'); //设置分页参数名称
  37. /* 基础设置 */
  38. $p = I('get.p');
  39. $this->totalRows = $totalRows; //设置总记录数
  40. $this->listRows = $listRows; //设置每页显示行数
  41. $this->parameter = empty($parameter) ? input() : $parameter;
  42. $this->nowPage = empty($p) ? 1 : intval($p);
  43. $this->nowPage = $this->nowPage>0 ? $this->nowPage : 1;
  44. $this->firstRow = $this->listRows * ($this->nowPage - 1);
  45. }
  46. /**
  47. * 定制分页链接设置
  48. * @param string $name 设置名称
  49. * @param string $value 设置值
  50. */
  51. public function setConfig($name,$value) {
  52. if(isset($this->config[$name])) {
  53. $this->config[$name] = $value;
  54. }
  55. }
  56. /**
  57. * 生成链接URL
  58. * @param integer $page 页码
  59. * @return string
  60. */
  61. private function url($page){
  62. return str_replace(urlencode('[PAGE]'), $page, $this->url);
  63. }
  64. /**
  65. * 组装分页链接
  66. * @return string
  67. */
  68. public function show() {
  69. if(0 == $this->totalRows) return '';
  70. /* 生成URL */
  71. $this->parameter[$this->p] = '[PAGE]';
  72. $this->url = U(ACTION_NAME, $this->parameter);
  73. /* 计算分页信息 */
  74. $this->totalPages = ceil($this->totalRows / $this->listRows); //总页数
  75. if(!empty($this->totalPages) && $this->nowPage > $this->totalPages) {
  76. $this->nowPage = $this->totalPages;
  77. }
  78. /* 计算分页临时变量 */
  79. $now_cool_page = $this->rollPage/2;
  80. $now_cool_page_ceil = ceil($now_cool_page);
  81. $this->lastSuffix && $this->config['last'] = $this->totalPages;
  82. $this->config['last'] = '尾页';
  83. $up_page = $down_page = $the_first = $the_end = $link_page = "";
  84. if ($this->totalPages > 1) {
  85. //上一页
  86. $up_row = $this->nowPage - 1;
  87. $up_page = $up_row > 0 ? '<li id="example1_previous" class="paginate_button prev"><a class="prev" data-p="'.$up_row.'" href="javascript:void(0)">' . $this->config['prev'] . '</a></li>' : '<li id="example1_previous" class="paginate_button previous disabled"><a class="first" data-p="1" href="javascript:void(0)">' . $this->config['prev'] . '</a></li>';
  88. //下一页
  89. $down_row = $this->nowPage + 1;
  90. $down_page = ($down_row <= $this->totalPages) ? '<li id="example1_next" class="paginate_button next"><a class="next" data-p="'.$down_row.'" href="javascript:void(0)">' . $this->config['next'] . '</a></li>' : '<li id="example1_previous" class="paginate_button previous disabled"><a class="end" data-p="'.$this->totalPages.'" href="javascript:void(0)">' . $this->config['next'] . '</a></li>';
  91. //第一页
  92. $the_first = '';
  93. if($this->totalPages > $this->rollPage && ($this->nowPage - $now_cool_page) >= 1){
  94. $the_first = '<li id="example1_previous" class="paginate_button previous disabled"><a class="first" data-p="1" href="javascript:void(0)">' . $this->config['first'] . '</a></li>';
  95. }
  96. //最后一页
  97. $the_end = '';
  98. if($this->totalPages > $this->rollPage && ($this->nowPage + $now_cool_page) < $this->totalPages){
  99. $the_end = '<li id="example1_previous" class="paginate_button previous disabled"><a class="end" data-p="'.$this->totalPages.'" href="javascript:void(0)">' . $this->config['last'] . '</a></li>';
  100. }
  101. //数字连接
  102. for($i = 1; $i <= $this->rollPage; $i++){
  103. if(($this->nowPage - $now_cool_page) <= 0 ){
  104. $page = $i;
  105. }elseif(($this->nowPage + $now_cool_page - 1) >= $this->totalPages){
  106. $page = $this->totalPages - $this->rollPage + $i;
  107. }else{
  108. $page = $this->nowPage - $now_cool_page_ceil + $i;
  109. }
  110. if($page > 0 && $page != $this->nowPage){
  111. if($page <= $this->totalPages){
  112. $link_page .= '<li class="paginate_button"><a class="num" data-p="'.$page.'" href="javascript:void(0)">' . $page . '</a></li>';
  113. }else{
  114. break;
  115. }
  116. }else{
  117. if($page > 0 && $this->totalPages != 1){
  118. // $link_page .= '<span class="current">' . $page . '</span>';
  119. $link_page .= '<li class="paginate_button active"><a tabindex="0" data-dt-idx="1" aria-controls="example1" data-p="'.$page.'" href="javascript:void(0);">' . $page . '</a></li>';
  120. }
  121. }
  122. }
  123. }
  124. //替换分页内容
  125. $page_str = str_replace(
  126. array('%HEADER%', '%NOW_PAGE%', '%UP_PAGE%', '%DOWN_PAGE%', '%FIRST%', '%LINK_PAGE%', '%END%', '%TOTAL_ROW%', '%TOTAL_PAGE%'),
  127. array($this->config['header'], $this->nowPage, $up_page, $down_page, $the_first, $link_page, $the_end, $this->totalRows, $this->totalPages),
  128. $this->config['theme']);
  129. return "<div class='dataTables_paginate paging_simple_numbers'><ul class='pagination'>{$page_str}</ul></div>";
  130. }
  131. }