查看: 846|回复: 0

微擎移动端使用pagination函数自定义

[复制链接]

748

主题

39

回帖

3907

积分

管理员

总舵主

积分
3907
QQ
发表于 2024-5-26 13:14:08 | 显示全部楼层 |阅读模式
在site.php中定义:
  1. //页码
  2.         function mpagination($total, $pageIndex, $pageSize = 15, $url = '', $context = array('before' => 5, 'after' => 4, 'ajaxcallback' => '', 'callbackfuncname' => '')) {
  3.             global $_W;
  4.             $pdata = array(
  5.                     'tcount' => 0,
  6.                     'tpage' => 0,
  7.                     'cindex' => 0,
  8.                     'findex' => 0,
  9.                     'pindex' => 0,
  10.                     'nindex' => 0,
  11.                     'lindex' => 0,
  12.                     'options' => '',
  13.             );
  14.             if (empty($context['before'])) {
  15.                     $context['before'] = 5;
  16.             }
  17.             if (empty($context['after'])) {
  18.                     $context['after'] = 4;
  19.             }
  20.    
  21.             if ($context['ajaxcallback']) {
  22.                     $context['isajax'] = true;
  23.             }
  24.    
  25.             if ($context['callbackfuncname']) {
  26.                     $callbackfunc = $context['callbackfuncname'];
  27.             }
  28.    
  29.             $pdata['tcount'] = $total;
  30.             $pdata['tpage'] = (empty($pageSize) || $pageSize < 0) ? 1 : ceil($total / $pageSize);
  31.             if ($pdata['tpage'] <= 1) {
  32.                     return '';
  33.             }
  34.             $cindex = $pageIndex;
  35.             $cindex = min($cindex, $pdata['tpage']);
  36.             $cindex = max($cindex, 1);
  37.             $pdata['cindex'] = $cindex;
  38.             $pdata['findex'] = 1;
  39.             $pdata['pindex'] = $cindex > 1 ? $cindex - 1 : 1;
  40.             $pdata['nindex'] = $cindex < $pdata['tpage'] ? $cindex + 1 : $pdata['tpage'];
  41.             $pdata['lindex'] = $pdata['tpage'];
  42.    
  43.             if ($context['isajax']) {
  44.                     if (empty($url)) {
  45.                             $url = $_W['script_name'] . '?' . http_build_query($_GET);
  46.                     }
  47.                     $pdata['faa'] = 'href="javascript:;" page="' . $pdata['findex'] . '" ' . ($callbackfunc ? 'ng-click="' . $callbackfunc . '(\'' . $url . '\', \'' . $pdata['findex'] . '\', this);"' : '');
  48.                     $pdata['paa'] = 'href="javascript:;" page="' . $pdata['pindex'] . '" ' . ($callbackfunc ? 'ng-click="' . $callbackfunc . '(\'' . $url . '\', \'' . $pdata['pindex'] . '\', this);"' : '');
  49.                     $pdata['naa'] = 'href="javascript:;" page="' . $pdata['nindex'] . '" ' . ($callbackfunc ? 'ng-click="' . $callbackfunc . '(\'' . $url . '\', \'' . $pdata['nindex'] . '\', this);"' : '');
  50.                     $pdata['laa'] = 'href="javascript:;" page="' . $pdata['lindex'] . '" ' . ($callbackfunc ? 'ng-click="' . $callbackfunc . '(\'' . $url . '\', \'' . $pdata['lindex'] . '\', this);"' : '');
  51.             } else {
  52.                     if ($url) {
  53.                             $pdata['faa'] = 'href="?' . str_replace('*', $pdata['findex'], $url) . '"';
  54.                             $pdata['paa'] = 'href="?' . str_replace('*', $pdata['pindex'], $url) . '"';
  55.                             $pdata['naa'] = 'href="?' . str_replace('*', $pdata['nindex'], $url) . '"';
  56.                             $pdata['laa'] = 'href="?' . str_replace('*', $pdata['lindex'], $url) . '"';
  57.                     } else {
  58.                             $_GET['page'] = $pdata['findex'];
  59.                             $pdata['faa'] = 'href="' . $_W['script_name'] . '?' . http_build_query($_GET) . '"';
  60.                             $_GET['page'] = $pdata['pindex'];
  61.                             $pdata['paa'] = 'href="' . $_W['script_name'] . '?' . http_build_query($_GET) . '"';
  62.                             $_GET['page'] = $pdata['nindex'];
  63.                             $pdata['naa'] = 'href="' . $_W['script_name'] . '?' . http_build_query($_GET) . '"';
  64.                             $_GET['page'] = $pdata['lindex'];
  65.                             $pdata['laa'] = 'href="' . $_W['script_name'] . '?' . http_build_query($_GET) . '"';
  66.                     }
  67.             }
  68.    
  69.             $html = '<div><ul class="pagination pagination-centered">';
  70.             $html .= "<li><a {$pdata['faa']} class="pager-nav">首页</a></li>";
  71.             empty($callbackfunc) && $html .= "<li><a {$pdata['paa']} class="pager-nav">«上一页</a></li>";
  72.    
  73.                     if (!$context['before'] && 0 != $context['before']) {
  74.                     $context['before'] = 5;
  75.             }
  76.             if (!$context['after'] && 0 != $context['after']) {
  77.                     $context['after'] = 4;
  78.             }
  79.    
  80.             if (0 != $context['after'] && 0 != $context['before']) {
  81.                     $range = array();
  82.                     $range['start'] = max(1, $pdata['cindex'] - $context['before']);
  83.                     $range['end'] = min($pdata['tpage'], $pdata['cindex'] + $context['after']);
  84.                     if ($range['end'] - $range['start'] < $context['before'] + $context['after']) {
  85.                             $range['end'] = min($pdata['tpage'], $range['start'] + $context['before'] + $context['after']);
  86.                             $range['start'] = max(1, $range['end'] - $context['before'] - $context['after']);
  87.                     }
  88.                     for ($i = $range['start']; $i <= $range['end']; ++$i) {
  89.                             if ($context['isajax']) {
  90.                                     $aa = 'href="javascript:;" page="' . $i . '" ' . ($callbackfunc ? 'ng-click="' . $callbackfunc . '(\'' . $url . '\', \'' . $i . '\', this);"' : '');
  91.                             } else {
  92.                                     if ($url) {
  93.                                             $aa = 'href="'.$_W['siteroot'].'app/index.php?' . str_replace('*', $i, $url) . '"';
  94.                                     } else {
  95.                                             $_GET['page'] = $i;
  96.                                             $aa = 'href="'.$_W['siteroot'].'app/index.php?' . http_build_query($_GET) . '"';
  97.                                     }
  98.                             }
  99.                             if (!empty($context['isajax'])) {
  100.                                     $html .= ($i == $pdata['cindex'] ? '<li class="active">' : '<li>') . "<a {$aa}>" . $i . '</a></li>';
  101.                             } else {
  102.                                     $html .= ($i == $pdata['cindex'] ? '<li class="active"><a href="javascript:;">' . $i . '</a></li>' : "<li><a {$aa}>" . $i . '</a></li>');
  103.                             }
  104.                     }
  105.             }
  106.    
  107.             if ($pdata['cindex'] < $pdata['tpage']) {
  108.                     empty($callbackfunc) && $html .= "<li><a {$pdata['naa']} class="pager-nav">下一页»</a></li>";
  109.                     $html .= "<li><a {$pdata['laa']} class="pager-nav">尾页</a></li>";
  110.             }
  111.             $html .= '</ul></div>';
  112.    
  113.             return $html;
  114.     }
复制代码
可以使用$this->mpagination($total, $pindex, $psize);来调用。
道不同,不相为谋!理想要有,万一实现了呢?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注公众号

相关侵权、举报、投诉及建议等,请发 E-mail:2513533699@qq.com

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.|鲁ICP备19000917号|鲁公网安备37078302000320号

在本版发帖
关注公众号
QQ客服返回顶部