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:
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
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.
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
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