filteryii2page-size

How do I Dynamically show the pageSize in yii2?


For now I have set static pagesize in my search function, but now I need to create dropdown for user to change the pagesize from front end. For e.g like 10,20,50,100.

I got the code but it is for yii previous version.


Solution

  • If I had to do it I will use the following approach.

    enter image description here

    The main thing to do is to include the id of the drop-down to the filterSelector option so that every time the gridview is filtered the dropdown value is also submitted and also whenever you change the dropdown.

    Your GridView will look like below

    GridView::widget([
        'dataProvider' => $dataProvider,
        'layout'=>'{summary}'.Html::activeDropDownList($searchModel, 'myPageSize', [1 => 10, 2 => 20, 50 => 50, 100 => 100],['id'=>'myPageSize'])."{items}<br/>{pager}",
        'filterModel' => $searchModel,
        'filterSelector' => '#myPageSize',
        'columns'=>[
           // your column configurations.......
        ]
    ]);
    

    Here we are setting the default page size equal to the minimum option inside the drop-down we created, otherwise it would be updated when ever you change the dropdown.

    Now try changing the dropdown you will see it working. Hope this helps