CGridView - how to set default page size?

Is there a way to globally set the default number of data displayed in CGridView? It’s currently showing 10 rows and currently I’m repeating the code in all view files that display grids:




....

$dataProvider->pagination->pageSize = 50;

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

        'id' => 'some-grid',

        'dataProvider' => $dataProvider,

.....



I tried to add to config file under ‘widgetFactory’:




...

    'widgets' => array(

        'CGridView' => array(

            'pageSize' => 50,

        ),

    ),

.....



and of course it’s not working :(

I’d like to ask about the same. Anyone?

You can initialize the pagination property on …DataProvider instantiation




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


$dp = new CActiveDataProvider(

  'someModel',

  array(..., 'pagination'=>array('pageSize'=>$pageSize))

);



/Tommy

Thanks for a quick respond. That is a partial solution. What I need is how to change the default page size value from 10 to another number, to avoid setting this value in every data provider, etc. I don’t believe that the only method is to change the


const DEFAULT_PAGE_SIZE

in yii/framework/web/CPagination.php file.

There’s a number of possible solutions but what I suggested was to add a parameter ‘defaultPageSize’ to the params section of protected/config/main.php.

You should never change any framework core files but you can subclass them. If you don’t want to add the code I suggested (without the intermediate variable assignment), you may want to subclass …DataProvider instead.

Of course you can still change the page size in a particular view, like you did before.

/Tommy

That’s all true and I both know and understand it. But it’s very strange for me that we can change other global parameters, like for example:


        'widgetFactory' => array(

            'widgets' => array(

                'CLinkPager' => array(

                    'maxButtonCount' => 15,

                ),

            ),

        ),

in the mentioned config/main.php file, but we can’t do it with the page size this way.

I extended CGridView and overrode init() like this




<?php

Yii::import('zii.widgets.grid.CGridView');


class MyExtGridView extends CGridView

{

  public function init()

  {

    if($this->dataProvider!==null)

      $this->dataProvider->pagination->pageSize = $this->pager[pageSize];

    parent::init();

  }

}



Now you can use




'widgetFactory'=>array(

  'class'=>'CWidgetFactory',

  'widgets' => array(

    'MyExtGridView' => array(

      'pager' => array(

        'pageSize' => 50,

      ),

    ),

  ),

),



(no promise this will work in all cases)

/Tommy

I’ve provided an alternative, global approach for setting pagesize, here:

Good job! I like your custom widget factory class.

And, if you extensively use your model’s search methods to generate the dataproviders for your grids - as I do




'dataProvider'=>$studentlog->search(),



you can add the page size as a parameter to that function and use it at will




	public function search($pagesize=null)

	{

		// @todo Please modify the following code to remove attributes that should not be searched.


		$criteria=new CDbCriteria;


...


        $pagesize = ($pagesize==null)?10:$pagesize;


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

                        'pagination'=>array('pageSize'=>$pagesize),


		));




adding, of course, the parameter in your view’s grid parameters





'dataProvider'=>$studentlog->search(5),




Hi,

If you need to add capability to change page size dynamically, you can see it here,

Be aware, I think .live is deprecated in the latest yii, if not wrong it is started from 1.1.16. For the old version, you can use this in admin.php




<?php

Yii::app()->clientScript->registerScript('initPageSize', <<<EOD

    $('.change-pageSize').live('change', function() {

        $.fn.yiiGridView.update('item-grid',{ data:{ pageSize: $(this).val() }})

    });

EOD

        , CClientScript::POS_READY);

?>



But, in the latest yii, use this




<?php

Yii::app()->clientScript->registerScript('initPageSize', <<<EOD

    $(document).on('change', '.change-pageSize', function() {

        $.fn.yiiGridView.update('member-grid',{ data:{ pageSize: $(this).val() }})

    });

EOD

        , CClientScript::POS_READY);

?>



This is due to the different jQuery being supported in the yii framework.

This script has purposed to trigger the page size changes.

When i was using Yii 1, i always extended my models because modifying it would cause lots of trouble if we had to regenerate it with gii… and to do this i would do like this




class CUser extends User

{

    public static function model($className = __CLASS__)

    {

        return parent::model($className);

    }


    public function search($pageSize = 5, $pageVar = 'page')

    {

        $criteria=new CDbCriteria;

        // this ccriteria can be used in many ways, you could also add another parameter which is an instance of a criteria and merge it here if you wish.

        return new CActiveDataProvider($this, array(

            'criteria'=>$criteria,

            'pagination' => array(

                'pageSize' => $pageSize,

                'pageVar' => $pageVar

            ),

            'sort' => array(

                'defaultOrder' => 't.id desc',

            )

        ));

    }


    // other functions here


}




and when i had to use dataprovider of a model i would use the function search() on it :)

you can modify the search function to your taste