Yii2 GridView 分页增加显示条数选择

Additional jQuery selector for selecting filter input fields
选择过滤器的字段附加的jQuery选择器。
views/*/index.php
<?php
$request = new \yii\web\Request;
echo Html::dropDownList('per_page', isset($request->get()['per_page']) ? $request->get()['per_page'] : $dataProvider->getPagination()->pageSize,  [10 => 10, 20 => 20, 50 => 50, 100 => 100]);
?>
\yii\web\Request用来获取当前分页的值,让前端选中。如果没有传,则读取$dataProvider中的分页配置,让前端选中。

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    'filterModel' => $searchModel,
    'filterSelector' => 'select[name="per_page"]',
    // ....
=?>
filterSelector加入后,改select的change将会被监听并将值传入后端。filterSelector还支持多个selector,例如:'filterSelector' => 'select[name="per_page"],input[name="xxx"]'

model/search/*
public search function
isset($params['per_page']) && $dataProvider->setPagination(['pageSize' => $params['per_page']]);
如果有传过滤参数per_page,则设置$dataProvider的Pagination

你可能感兴趣的:(Yii2 GridView 分页增加显示条数选择)