Yii2分页

1、controller action
use yii\data\Pagination;
$query = Article::find()->where([‘status’ => 1]);
$countQuery = clone $query;
$pages = new Pagination([‘totalCount’ => $countQuery->count()]);
$models = $query->offset($pages->offset)
->limit($pages->limit)
->all();
return $this->render(‘index’, [
‘models’ => $models,
‘pages’ => $pages,
]);
2、View
use yii\widgets\LinkPager;
//循环展示数据
foreach ($models as $model) {
// ……
}
//显示分页页码
echo LinkPager::widget([
‘pagination’ => $pages,
])

首先,我们把上下页的按钮修改成中文
$pages,
‘nextPageLabel’ => ‘下一页’,
‘prevPageLabel’ => ‘上一页’,
]); ?>

如果你不想要显示上下页,可以将prevPageLabel和nextPageLabel设置为false
$pages,
‘nextPageLabel’ => false,
‘prevPageLabel’ => false,
]); ?>

默认不显示首页也尾页,如果你需要,可以这样设置
$pages,
‘firstPageLabel’ => ‘首页’,
‘lastPageLabel’ => ‘尾页’,
]); ?>

如果你的数据过少,不够2页,默认不显示分页,如果你需要,设置hideOnSinglePage=false即可
$pages,
‘hideOnSinglePage’ => false,
]); ?>

默认显示的页码为10页,可以设置maxButtonCount为你想要展示的页数
$pages,
‘maxButtonCount’ => 5,
]); ?>
有些人不喜欢默认的样式,想要分页带上自己的样式,可以设置options,不要忘了自行实现pre,next,disabled等样式
$pages,
‘options’ => [‘class’ => ‘m-pagination’],
]); ?>

默认的分页路由是下面这样子的,我们看看能做点什么
/controller/action?page=2&per-page=20

首先,我们是必须要指定总条数totalCount的,没这个参数,分页也是没办法实现的
$pages = new Pagination([
‘totalCount’ => $totalCount,
]);
默认分页的数量是20,你可以设置pageSize为你想要的
$pages = new Pagination([
‘totalCount’ => $totalCount,
‘pageSize’ => 5,
]);

从上面的分页路由我们可以看到,默认带的有每页的数量per-page 如果你不想显示该参数,设置pageSizeParam=false就好

$pages = new Pagination([
‘totalCount’ => $totalCount,
‘pageSizeParam’ => false,
]);

我们也可以看到,默认的页面取决于参数page,如果你想改变该参数为p,设置pageParam=p就好
$pages = new Pagination([
‘totalCount’ => $totalCount,
‘pageParam’ => ‘p’,
]);

如果你的分页存在于首页,相信你肯定想要/?p=1而不是/site/index?p=1,我们看看怎么隐藏掉路由
$pages = new Pagination([
‘totalCount’ => $totalCount,
‘route’ => false,
]);

可能你会发现分页类Pagination有一个bug,假如我们只有1页的数据,但是手动更改地址栏的page=20的时候,也会显示page=1的数据?当然,这在大部分接口API中就很让人厌烦。但是,这并非bug,而是一种友好的验证。设置validatePage=false即可避免掉该问题
$pages = new Pagination([
‘totalCount’ => $totalCount,
‘validatePage’ => false, ]);

接下来扩展LinkPager组件实现的。

‘;
public $nextPageLabel = ‘‘;
public $currentCountPageLabel = ‘第 {currentPage} 页 / 共 {countPage} 页’;
public $currentCountPageClass = ‘page-number’;
public $hideOnSinglePage = false;
public function init () {
parent::init();
}
public function run () {
$pageCount = $this->pagination->getPageCount();
if ($pageCount < 2 && $this->hideOnSinglePage) {
return ”;
}
$buttons = [];
$currentPage = $this->pagination->getPage();
// prev page
if ($this->prevPageLabel !== false) {
if (($page = $currentPage – 1) < 0) { $page = 0; } $buttons[] = $this->renderPageButton($this->prevPageLabel, $page, $this->prevPageCssClass, $currentPage <= 0, false); } // current page / count page if ($this->currentCountPageLabel !== false && $pageCount) {
$currentCountPageLabel = str_replace([‘{currentPage}’, ‘{countPage}’], [$currentPage+1, $pageCount], $this->currentCountPageLabel);
$buttons[] = Html::tag(‘span’, $currentCountPageLabel, array(‘class’ => $this->currentCountPageClass));
}
// next page
if ($this->nextPageLabel !== false) {
if (($page = $currentPage + 1) >= $pageCount – 1) {
$page = $pageCount – 1;
}
$buttons[] = $this->renderPageButton($this->nextPageLabel, $page, $this->nextPageCssClass, $currentPage >= $pageCount – 1, false);
}
return Html::tag(‘nav’, implode(“\n”, $buttons), $this->options);
}
protected function renderPageButton($label, $page, $class, $disabled, $active)
{
$options = [‘class’ => empty($class) ? $this->pageCssClass : $class];
if ($active) {
Html::addCssClass($options, $this->activePageCssClass);
}
if ($disabled) {
return false;
}
$linkOptions = $this->linkOptions;
$linkOptions += $options;
$linkOptions[‘data-page’] = $page;
return Html::a($label, $this->pagination->createUrl($page), $linkOptions);
}
}

调用MLinkPager实现分页效果像下面这样即可
use frontend\components\MLinkPager;
$pages,
]); ?>

发表评论

电子邮件地址不会被公开。 必填项已用*标注