Pagesize On Cgridview

Hi everyone,

I’ve a small problem.

I’m using yii-user extension.

I have in UserController.php the following function




	public function actionIndex()

	{

		$dataProvider=new CActiveDataProvider('User', array(

			'pagination'=>array(

				'pageSize'=>20,

			),

			'criteria'=>array(

				'condition'=>'status>'.User::STATUS_BANED,

			),

		));


		$this->render('index',array(

			'dataProvider'=>$dataProvider,

		));

	}



… and the following view (index.php)




$this->widget('zii.widgets.grid.CGridView', array(

	'dataProvider'=>$dataProvider,

	'itemsCssClass' => 'border-lines',

	'htmlOptions' => array('class' => 'border-lines-table user-table'),

	'ajaxUpdate'=>'false',

	'summaryText'=>'',

        'pager'=> array(

		'header'=>'',

		'footer'=>'',

		'htmlOptions' => array('class'=>'pagination pagination-lg'),

		'prevPageLabel'  => '<',

		'nextPageLabel'  => '>',

		'firstPageLabel' => '&laquo;',

		'lastPageLabel'  => '&raquo;',

		'selectedPageCssClass' => 'active',

		'maxButtonCount' => 10

	),		

	'columns'=>array(

		array(

			'name' => 'username',

			'type'=>'raw',

			'value' => 'CHtml::link(CHtml::encode($data->username),array("user/view","id"=>$data->id))',

		),

		array(

			'name' => 'createtime',

			'value' => 'date("d.m.Y H:i",$data->createtime)',

		),

		array(

			'name' => 'lastvisit',

			'value' => '(($data->lastvisit)?date("d.m.Y H:i",$data->lastvisit):UserModule::t("Not visited"))',

		),

	),

));



Number of items (users) per page is always 10 even if I set 20.

Anyone can help me? Thanks

Assuming you are using the search method in your model, add this to your gridview:

View:




array(

    'class'=>'CButtonColumn',

    'header'=>CHtml::dropDownList('pageSize',$pageSize,array(10=>10,20=>20,50=>50,100=>100),array('onchange'=>"$.fn.yiiGridView.update('modelname-grid',{ data:{pageSize: $(this).val() }})")),

);



Change ‘modelname-grid’ to contain your model name (lowercase).

Add this to the appropriate action in your controller.

Controller:




        $pageSize = Yii::app()->user->getState('pageSize', Yii::app()->params['defaultPageSize']);

        if (isset($_GET['pageSize'])) {

            $pageSize = $_GET['pageSize'];

            Yii::app()->user->setState('pageSize', (int) $_GET['pageSize']);

            unset($_GET['pageSize']);

        }


        $this->render('index', array(

            'model' => $model,

            'pageSize' => $pageSize,

        ));



Update your search method as follows:

Model:




        return new CActiveDataProvider($this, array(

            'criteria' => $criteria,

            'pagination' => array(

                'pageSize' => Yii::app()->user->getState('pageSize', Yii::app()->params['defaultPageSize']),

            ),

        ));



You can now change the page size on the fly.

Thanks, but I’m not using the search method.

I would set 20 records per page and no other options.

How can I do?

I’ve used this option with search method in other Controller and it works fine; not here. Why?