yii框架的分页类
class Pagination extends BaseObject implements Linkable
初始化: $pagination = new Pagination(['totalCount' => 100,'pageSize'=> 20]);
然后再html的模板里使用
<?php
if (!isset($pagination) && isset($pg)) {
$pagination = $pg;
}
if (isset($pagination->totalCount) && $pagination->totalCount > 0) :
$pageSize = $pagination->pageSize;
$page = $pagination->page + 1;
$totalCount = $pagination->totalCount;
$maxPage = ceil($totalCount / $pageSize);
$nowUrl = '/'.Yii::$app->request->pathInfo;
$gets = Yii::$app->request->get();
$index = $page * -1;
$len = 5;
if ($page > 3) {
$index = -3;
}
if ($maxPage - ($page + $index + $len) <= 3) {
$index = -8 - ($page - $maxPage);
$len = 8;
}
?>
<!-- <style>
.pagination li {
float: left;
min-width: 2em;
padding: 4px;
text-align: center;
}
.pagination li.active a {
color: red;
}
</style> -->
<div class="dt-panelfooter clearfix">
<div class="dataTables_paginate paging_simple_numbers">
<ul class="pagination">
<li class="<?= $page > 1 ? '' : 'disabled'; ?>"><a href="<?= $page > 1 ? url(array_merge([$nowUrl],$gets,['page' => $page - 1, 'per-page' => $pageSize])) : '[removed]void(0);'; ?>" aria-label="Previous"><!--<span aria-hidden="true">«</span>--> Previous</a></li>
<?php if ($page > 3 && $maxPage > 8 ) : ?>
<li><a href="<?= url(array_merge([$nowUrl],$gets,['page' => 1, 'per-page' => $pageSize])); ?>">1</a></li>
<?php if ($page !== 4) : ?>
<li><a href="<?= url(array_merge([$nowUrl],$gets,['page' => 2, 'per-page' => $pageSize])); ?>">2</a></li>
<?php if ($page !== 5) : ?>
<li class="none"><a href="###">...</a></li>
<?php endif; ?>
<?php endif; ?>
<?php endif; ?>
<?php for ($i = 0; $i < $len; $i++) :
$index++;
if ($page + $index > $maxPage || $page + $index < 1) {
continue;
}
?>
<li class="<?= $page + $index == $page ? 'active' : ''; ?>"><a href="<?= url(array_merge([$nowUrl],$gets,['page' => $page + $index, 'per-page' => $pageSize])); ?>"><?= $page + $index; ?></a></li>
<?php endfor; ?>
<?php if ($len !== 8) : ?>
<li class="none"><a href="###">...</a></li>
<li><a href="<?= url(array_merge([$nowUrl],$gets,['page' => $maxPage - 1, 'per-page' => $pageSize])); ?>"><?= $maxPage - 1; ?></a></li>
<li><a href="<?= url(array_merge([$nowUrl],$gets,['page' => $maxPage, 'per-page' => $pageSize])); ?>"><?= $maxPage; ?></a></li>
<?php endif; ?>
<li class="<?= $page >= $maxPage ? 'disabled' : ''; ?>"><a href="<?= $page < $maxPage ? url(array_merge([$nowUrl],$gets ,[ 'page' => $page + 1, 'per-page' => $pageSize]),$gets) : '[removed]void(0);'; ?>" aria-label="Next">Next <!--<span aria-hidden="true">»</span>--></a></li>
</ul>
</div>
</div>
<?php endif; ?>
最终生成了 翻页的 代码效果。