Setting CGridView page size

Hi

I have one table which records I would like to display in a gridview but without using pagination and keeping the filter and sorting functionalities.

If I use CActiveDataProvider I can set the


$dataProvider->pagination->pageSize = $model->count()

but I loose the filtering capabilities because an error message is displayed saying: [color="#0000FF"]CActiveDataProvider does not have a method named "getValidators".[/color]

In this case, if I set the CGridView->filter property to $model no errors are shown, but the filters do not work.

Example:


 

$dataProvider = new CActiveDataProvider($model);

/* The idea is to show all the table's records in the gridView without using the pager */

$dataProvider->pagination->pageSize = $model->count();


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

   			array(

              	'id'=>'contact-grid',

                  'dataProvider'=>$dataProvider,   // This helps to establish the pageSize property.

                  'filter'=>$model,	// This keeps the filters in the gridview but don't work.

                  'selectableRows'=>1,

                  'enableSorting'=>true,

                  'summaryText' => '{count} records(s) found.',

                  'columns'=>array(...

I wish to have filtering and sorting capabilities (as in any normal gridView) and show all the records with a code like this:




	$contacts = new Contact;

	$dataProvider = new CActiveDataProvider('Contact');

	$dataProvider->pagination->pageSize = $contacts->count();

	

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

              	array(

              	'id'=>'contact-grid',

              	'dataProvider'=>$dataProvider,

              	'filter'=>$dataProvider,

              	'selectableRows'=>1,

              	'enableSorting'=>true,

              	'enablePagination'=>false,

              	'summaryText' => '{count} records(s) found.',

              	'columns'=>array( ...



but this approach generates the error: [color="#0000FF"]CActiveDataProvider does not have a method named "getValidators".[/color]

Is there any way to show all the records in a gridView without using pagination?

Any ideas?

Hi,

there is a CDataProvider.setPagination method to disable pagination.

Try




$dataProvider = new CActiveDataProvider($model);

$dataProvider->setPagination(false);

// ...



If you just don’t want to display the pager define a template in your grid and leave it out.


'template' => '{items}{summary},

Enfield, the idea is not only to get rid of the pager. My goal is to show all of the table’s records but by default Yii shows only the first 10 records in a gridView.

I don’t want my gridView to show just a bunch of records, but all of them.

Sorry, I forgot to mention this detail in my original post.

[size="6"]SOLVED !![/size]

Ok, after reading this post, published by Trejder:

http://www.yiiframew…dpost__p__70850

I started to experiment on my own code and I realized what my problem was.

[size="3"]First[/size]

I didn’t need to create another CActiveDataProvider to feed my CGridView widget, because the variable $model (a CActiveRecord instance) provides an instance of CActiveDataProvider in its search method. So, for my model class I just had to make a small change to make sure that the search method returns all the records from the table:




public function search() {

   $criteria = new CDbCriteria;

   $criteria->compare('givenname',$this->givenname,true);

   $criteria->compare('familyname',$this->familyname,true);

   $criteria->compare('country',$this->country,true);

   return new CActiveDataProvider(get_class($this), array('criteria'=>$criteria, 'pagination'=>array('pageSize'=>$this->count())) );

}



Normally, the Gii tool generates this method to return something like this:


return new CActiveDataProvider(get_class($this), array('criteria'=>$criteria, ));

but in my case I had to add the pagination parameter and set its pageSize property to the total count of records in the table.

[size="3"]Second[/size]

In the controller, the method that calls the script that contains the CGridView widget can look like this:




public function actionDonations() {

   $model = new Contact('search');

   $model->unsetAttributes();  // clear any default values

   // This condition is to update the $model variable with the last change posted with the filters in the gridView.

   if (isset($_GET['Contact']))

  	$model->attributes = $_GET['Contact'];	

   $this->render('donations', array('model'=>$model));

}



[size="3"]Finally [/size]

The CGridView widget can use the normal $model variable as dataProvider:



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

   array(

 		'id'=>'contact-grid',

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

 		'filter'=>$model,

 		'selectableRows'=>1,

 		'enableSorting'=>true,

 		'enablePagination'=>false,

 		'summaryText' => '{count} records(s) found.',

 		'columns'=>array( ... )

   ));



With this little changes I get rid of the pager and show all records in the table.