PaginationHelper.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace app\common\helpers;
  3. /**
  4. * Created by PhpStorm.
  5. * Author: 芸众商城 www.yunzshop.com
  6. * Date: 2017/2/27
  7. * Time: 下午1:11
  8. */
  9. class PaginationHelper
  10. {
  11. /**
  12. * 获取分页导航HTML
  13. * @param int $total 总记录数
  14. * @param int $pageIndex 当前页码
  15. * @param int $pageSize 每页显示条数
  16. * @param string $url 要生成的 url 格式,页码占位符请使用 *,如果未写占位符,系统将自动生成
  17. * @param array $context
  18. * @return string
  19. */
  20. public static function show($total, $pageIndex, $pageSize = 15, $url = '', $context = []) {
  21. !$context && $context = ['before' => 5, 'after' => 4, 'ajaxcallback' => '', 'callbackfuncname' => ''];
  22. $pdata = [
  23. 'tcount' => 0,
  24. 'tpage' => 0,
  25. 'cindex' => 0,
  26. 'findex' => 0,
  27. 'pindex' => 0,
  28. 'nindex' => 0,
  29. 'lindex' => 0,
  30. 'options' => ''
  31. ];
  32. $context['isajax'] = false;
  33. if (isset($context['ajaxcallback']) && $context['ajaxcallback']) {
  34. $context['isajax'] = true;
  35. }
  36. $callbackfunc = '';
  37. if (isset($context['callbackfuncname']) && $context['callbackfuncname']) {
  38. $callbackfunc = $context['callbackfuncname'];
  39. }
  40. $pdata['tcount'] = $total;
  41. $pdata['tpage'] = (empty($pageSize) || $pageSize < 0) ? 1 : ceil($total / $pageSize);
  42. if ($pdata['tpage'] <= 1) {
  43. return '';
  44. }
  45. $cindex = $pageIndex;
  46. $cindex = min($cindex, $pdata['tpage']);
  47. $cindex = max($cindex, 1);
  48. $pdata['cindex'] = $cindex;
  49. $pdata['findex'] = 1;
  50. $pdata['pindex'] = $cindex > 1 ? $cindex - 1 : 1;
  51. $pdata['nindex'] = $cindex < $pdata['tpage'] ? $cindex + 1 : $pdata['tpage'];
  52. $pdata['lindex'] = $pdata['tpage'];
  53. if ($context['isajax'] === true) {
  54. if (empty($url)) {
  55. $url = \YunShop::app()->script_name . '?' . http_build_query($_REQUEST);
  56. }
  57. $pdata['faa'] = 'href="javascript:;" page="' . $pdata['findex'] . '" '. ($callbackfunc ? 'onclick="'.$callbackfunc.'(\'' . $url . '\', \'' . $pdata['findex'] . '\', this);return false;"' : '');
  58. $pdata['paa'] = 'href="javascript:;" page="' . $pdata['pindex'] . '" '. ($callbackfunc ? 'onclick="'.$callbackfunc.'(\'' . $url . '\', \'' . $pdata['pindex'] . '\', this);return false;"' : '');
  59. $pdata['naa'] = 'href="javascript:;" page="' . $pdata['nindex'] . '" '. ($callbackfunc ? 'onclick="'.$callbackfunc.'(\'' . $url . '\', \'' . $pdata['nindex'] . '\', this);return false;"' : '');
  60. $pdata['laa'] = 'href="javascript:;" page="' . $pdata['lindex'] . '" '. ($callbackfunc ? 'onclick="'.$callbackfunc.'(\'' . $url . '\', \'' . $pdata['lindex'] . '\', this);return false;"' : '');
  61. } else {
  62. if ($url) {
  63. $pdata['faa'] = 'href="?' . str_replace('*', $pdata['findex'], $url) . '"';
  64. $pdata['paa'] = 'href="?' . str_replace('*', $pdata['pindex'], $url) . '"';
  65. $pdata['naa'] = 'href="?' . str_replace('*', $pdata['nindex'], $url) . '"';
  66. $pdata['laa'] = 'href="?' . str_replace('*', $pdata['lindex'], $url) . '"';
  67. } else {
  68. $_REQUEST['page'] = $pdata['findex'];
  69. $pdata['faa'] = 'href="' . \YunShop::app()->script_name . '?' . http_build_query($_REQUEST) . '"';
  70. $_REQUEST['page'] = $pdata['pindex'];
  71. $pdata['paa'] = 'href="' . \YunShop::app()->script_name . '?' . http_build_query($_REQUEST) . '"';
  72. $_REQUEST['page'] = $pdata['nindex'];
  73. $pdata['naa'] = 'href="' . \YunShop::app()->script_name . '?' . http_build_query($_REQUEST) . '"';
  74. $_REQUEST['page'] = $pdata['lindex'];
  75. $pdata['laa'] = 'href="' . \YunShop::app()->script_name . '?' . http_build_query($_REQUEST) . '"';
  76. }
  77. }
  78. $html = '<div><ul class="pagination pagination-centered">';
  79. if ($pdata['cindex'] > 1) {
  80. $html .= "<li><a {$pdata['faa']} class=\"pager-nav\">首页</a></li>";
  81. $html .= "<li><a {$pdata['paa']} class=\"pager-nav\">&laquo;上一页</a></li>";
  82. }
  83. //页码算法:前5后4,不足10位补齐
  84. if (!$context['before'] && $context['before'] != 0) {
  85. $context['before'] = 5;
  86. }
  87. if (!$context['after'] && $context['after'] != 0) {
  88. $context['after'] = 4;
  89. }
  90. if ($context['after'] != 0 && $context['before'] != 0) {
  91. $range = array();
  92. $range['start'] = max(1, $pdata['cindex'] - $context['before']);
  93. $range['end'] = min($pdata['tpage'], $pdata['cindex'] + $context['after']);
  94. if ($range['end'] - $range['start'] < $context['before'] + $context['after']) {
  95. $range['end'] = min($pdata['tpage'], $range['start'] + $context['before'] + $context['after']);
  96. $range['start'] = max(1, $range['end'] - $context['before'] - $context['after']);
  97. }
  98. for ($i = $range['start']; $i <= $range['end']; $i++) {
  99. if (true === $context['isajax']) {
  100. $aa = 'href="javascript:;" page="' . $i . '" '. ($callbackfunc ? 'onclick="'.$callbackfunc.'(\'' . $url . '\', \'' . $i . '\', this);return false;"' : '');
  101. } else {
  102. if ($url) {
  103. $aa = 'href="?' . str_replace('*', $i, $url) . '"';
  104. } else {
  105. $_REQUEST['page'] = $i;
  106. $aa = 'href="?' . http_build_query($_REQUEST) . '"';
  107. }
  108. }
  109. $html .= ($i == $pdata['cindex'] ? '<li class="active"><a href="javascript:;">' . $i . '</a></li>' : "<li><a {$aa}>" . $i . '</a></li>');
  110. }
  111. }
  112. if ($pdata['cindex'] < $pdata['tpage']) {
  113. $html .= "<li><a {$pdata['naa']} class=\"pager-nav\">下一页&raquo;</a></li>";
  114. $html .= "<li><a {$pdata['laa']} class=\"pager-nav\">尾页</a></li>";
  115. }
  116. $html .= "<li><input type='text' id='jump' style='width: 25px; height: 25px;'></li>";
  117. $html .= "<li><a onclick =getkey(this,{$pdata['tpage']}) class=\"pager-nav\" style='float: right;' >跳转</a></li>";
  118. $html .= '</ul></div>';
  119. return $html;
  120. }
  121. }