Yii Framework Forum: Setting CGridView page size - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Setting CGridView page size How can I show all table's records in a gridview without pager? Rate Topic: ****- 1 Votes

#1 User is offline   jogasa21 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 09-March 11
  • Location:Costa Rica / Cambodia

Posted 17 February 2012 - 05:02 AM

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: CActiveDataProvider does not have a method named "getValidators".

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: CActiveDataProvider does not have a method named "getValidators".

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

Any ideas?
0

#2 User is offline   kokomo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 289
  • Joined: 23-July 10

Posted 17 February 2012 - 05:18 AM

Hi,
there is a CDataProvider.setPagination method to disable pagination.

Try
$dataProvider = new CActiveDataProvider($model);
$dataProvider->setPagination(false);
// ...

0

#3 User is offline   enfield 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 288
  • Joined: 12-January 11

Posted 17 February 2012 - 11:06 AM

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

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

0

#4 User is offline   jogasa21 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 09-March 11
  • Location:Costa Rica / Cambodia

Posted 19 February 2012 - 08:21 PM

View Postenfield, on 17 February 2012 - 11:06 AM, said:

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

#5 User is offline   jogasa21 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 27
  • Joined: 09-March 11
  • Location:Costa Rica / Cambodia

Posted 21 February 2012 - 03:34 AM

SOLVED !!

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.

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

Second...
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));
}


Finally ...
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.
4

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

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