[solved] CPagination on a CArrayDataProvider

Hi everyone. I’m trying to add a pager to a grid but having unexpected results.

The pager was working prior to adding CPagination::applyLimit when I was sending all 500 results to the grid.

Now the pager doesn’t display even with ‘enablePagination’ set to true since I began limiting the query to 50 results.

Controller:




$model = Review::model()->isFlagged();

$criteria = $model->dbCriteria;


// Get count

$count = $model->count();


// Init pagination

$pagination = new CPagination($count);

$pagination->pageSize = 50;

$pagination->applyLimit($criteria);


// Grab records w/ criteria

$reviews = $model->findAll($criteria);


$this->render( 'listReviews', array( 'reviews' => $reviews, 'pagination' => $pagination ) );



listReviews.php




$dataProvider=new CArrayDataProvider($reviews, array(

	'id'=>'id',

	'sort'=>array(

		'attributes'=>array(

			 'id',

		),

	),

	'pagination' => $pagination,

));


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

	'dataProvider'=>$dataProvider,

	'columns'=>array(

		array(

			'name' => 'Deactivate',

			'class' => 'CCheckBoxColumn',

			'value' => '$data->id',

			//'selectableRows' => null,

			'cssClassExpression' => '"sc-new-review-checkbox"',

			'id' => 'deactivateReview',

		),

		array(

			'name' => 'Title',

			'value' => 'CHtml::link( $data->title ." ", array( "review/view", "id"=>$data->id ) )',

			'type' => 'html',

			'cssClassExpression' => '"sc-new-review-title"',

		),

	),

	'selectableRows' => 2,

	'itemsCssClass' => 'sc-new-reviews',

	'enablePagination' => true,

));



Thanks,

Frank

P.S. This is my first post here. Yii is absolutely awesome.

Hey, I believe I found the issue and I have a solution (using CActiveDataProvider now). Will post back later with more info.

Thanks anyway.

why dont you use CActiveDataProvider?

create your DataProvider in your controller

Hey, I’m not 100% sure why it didn’t work but here is what I think. CArrayDataProvider expects all records to be given to it. If you are on the second page, it DataProvider will skip over the records for the first page and only give the ones for the second to the Grid. Well since I was limiting my query early and only giving the results of a single page, whenever I went to the second page it skipped over all the records thinking they were for the first page. Hope this makes sense.

Anyway the solution was to use CActiveDataProvider so here is the new code:

Controller:




$dataProvider = Review::model()->needsFirstReview()->search();

$dataProvider->pagination->pageSize = 500;

$this->render( 'listReviews', array( 'dataProvider' => $dataProvider ) );



listReviews.php




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

	'dataProvider'=>$dataProvider,

	'columns'=>array(

		array(

			'name' => 'Deactivate',

			'class' => 'CCheckBoxColumn',

			'value' => '$data->id',

			//'selectableRows' => null,

			'cssClassExpression' => '"sc-new-review-checkbox"',

			'id' => 'deactivateReview',

		),

		array(

			'name' => 'Title',

			'value' => 'CHtml::link( $data->title ." ", array( "review/view", "id"=>$data->id ) )',

			'type' => 'html',

			'cssClassExpression' => '"sc-new-review-title"',

		),

	),

	'selectableRows' => 2,

	'itemsCssClass' => 'sc-new-reviews',

	'enablePagination' => true,

));