在 Yii2 中,ModelNameSearch.php 负责这一点。这就是我设置它的方式(不完全是,但这是简单的版本):
在控制器中
/**
* Sets the pagination for the list
* @return mixed
*/
public function actionPagination()
{
TagSearch::setPerPage(Yii::$app->request->queryParams['records']);
$this->redirect(['index']);
}
在标签搜索中
public function setPerPage($recordPerPage)
{
Yii::$app->session->set(self::className() . 'Pagination', $recordPerPage);
}
public function getPerPage()
{
return Yii::$app->session->get(self::className() . 'Pagination', 25),
}
.....................
public function search($params)
{
..............................
$dataProvider = new ActiveDataProvider([
'query' => $query,
'pagination' => [
'pageSize' => self::getPerPage()
],
]);
......................
}
在视图中随意做一个
<?= Html::dropDownList('pagination', TagSearch::getPerPage(), ['10' => '10 per page', '25' => '25 per page', '50' => '50 per page', '100' => '100 per page'], ['class' => "form-control input-sm pagination", 'data-change'=> Url::toRoute('pagination')]) ?>
并添加一个
$('select.pagination').on('change', function() {
document.location.href = $(this).attr('data-change') + '?records=' + $(this).val();
});
我需要自己检查变量的命名,上面的代码(我写的是复制粘贴和更改)可能有错误,但你明白了。