yii DropDown for pageSize in CGridView

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 isactionAdmin() ) i added:

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

 

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

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

 

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

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

 

Step 4: And finally the application parameter

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

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值