Yii Framework Forum: CGridView - how to set default page size? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

CGridView - how to set default page size? Rate Topic: -----

#1 User is offline   iceblock 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 20
  • Joined: 03-August 10

Posted 06 September 2010 - 01:12 AM

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 :(
0

#2 User is offline   darecki 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 13-October 10

Posted 26 October 2010 - 01:44 PM

I'd like to ask about the same. Anyone?
0

#3 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 26 October 2010 - 02:53 PM

View Postdarecki, on 26 October 2010 - 01:44 PM, said:

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
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#4 User is offline   darecki 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 13-October 10

Posted 26 October 2010 - 03:33 PM

View Posttri, on 26 October 2010 - 02:53 PM, said:

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

#5 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 26 October 2010 - 03:46 PM

View Postdarecki, on 26 October 2010 - 03:33 PM, said:

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
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#6 User is offline   darecki 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 13-October 10

Posted 26 October 2010 - 03:56 PM

View Posttri, on 26 October 2010 - 03:46 PM, said:

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

#7 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 26 October 2010 - 07:10 PM

View Postdarecki, on 26 October 2010 - 03:56 PM, said:

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

This post has been edited by tri: 26 October 2010 - 07:16 PM

Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#8 User is offline   intel352 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 196
  • Joined: 05-February 10
  • Location:Southport, NC

Posted 11 January 2013 - 03:38 PM

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

https://gist.github.com/4513672
Need live Yii support? - Join the #yii IRC channel on Freenode!
0

#9 User is offline   egulhan 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 22-May 12

Posted 20 April 2013 - 12:55 PM

View Postintel352, on 11 January 2013 - 03:38 PM, said:

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

https://gist.github.com/4513672


Good job! I like your custom widget factory class.
Php web deleloper
1

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users