. . .

DropDown for pageSize in CGridView

Published: October 9, 2010

A convenient drop down to select page size and save in User state.

Step 1: On top of my controller action for the gridview (if you used CRUD, this is actionAdmin() ) i added:

// page size drop down changed
if (isset($_GET['pageSize'])) {
    Yii::app()->user->setState('pageSize',(int)$_GET['pageSize']);
    unset($_GET['pageSize']);  // would interfere with pager and repetitive page size change
}

 

Step2:  In the model (e.g. model/User.php) the data provider in search() is configured like:

return new CActiveDataProvider(get_class($this),array(
    'pagination'=>array(
        'pageSize'=> Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']),
    ),
    'criteria'=>$criteria,
));

 

Step 3:  Now the view (e.g. views/user/admin.php) :

<?php
// put this somewhere on top
$pageSize=Yii::app()->user->getState('pageSize',Yii::app()->params['defaultPageSize']); ?>
<?php
// we use header of button column for the drop down
// so change the CButtonColumn in columns array like this:
...
array(
    'class'=>'CButtonColumn',
    'header'=>CHtml::dropDownList('pageSize',$pageSize,array(20=>20,50=>50,100=>100),array(
        // change 'user-grid' to the actual id of your grid!!
        'onchange'=>"$.fn.yiiGridView.update('user-grid',{ data:{pageSize: $(this).val() }})",
    )),
),

 

Step 4: And finally the application parameter

// Accessable with Yii::app()->params['paramName']
'params'=>array (
    'defaultPageSize'=>20,

 

Source: http://www.yiiframework.com/forum/index.php?/topic/8994-dropdown-for-pagesize-in-cgridview/  

Don't forget to share this post

    Let's Build Digital Excellence Together


    • Cost Efficient Solutions.
    • Minimal Timelines.
    • Effective Communication.
    • High Quality Standards.
    • Lifetime Support.
    • Transparent Execution.
    • 24/7 Availability.
    • Scalable Teams.

    Join Our 200+ Happy Clients Across Globe


    Free Consultation.

      Do you need help in strategy, innovation or growth of your startup/business? Experts from our team will get in touch with you.

      Please do not post jobs/internships inquiries here.