Displaying Certain rows in CGridView

Hello, on one of my /admin pages I want to display all the rows if the user is a superAdmin, and some if the user is a regular admin. How would I go about doing this? I would assume it has something to do with the dataprovider, which is set to $model->search. How can I edit search to check certain conditions?

public function search()


		$criteria=new CDbCriteria;

		$criteria->with = array('program','user');

		$criteria->compare('program.prog_name', $this->pid);



		return new CActiveDataProvider(get_class($this), array(



				'defaultOrder'=>'uid ASC',




For example, if SOME_VARIABLE = 2, show only rows with STATUS = X. Thanks for the help!

Just add your condition with an additional $criteria->compare() using the SOME_VARIABLE like


   $criteria->compare('STATUS', 2);

That’s for whole columns, I want to omit certain rows. However, I have figured out a working solution. It’s probably not the best way, but I went into the CGridView.php file and edited the renderTableRow() function. In it, I checked if the current controller was the one in question and then performed my checks using the dataProvider information:

if(Yii::app()->controller->getId() == "ABCDEFG") { 

if($this->dataProvider->data[$row]->SOME_ATTRIBUTE == "SOME_VALUE") $valid = true;


if(Yii::app()->controller->getId() != "ABCDEFG" || $valid) {

//row display code here


This way the expression has do be evaluated for every row… not to mention that you changed a core file (it’s better to extend to do this)…

wouldn’t be faster if you add that condition to the CDbCriteria…

Adding the compare in your search() function will cause the dataProvider to not return the rows that match that criteria, effectively filtering them out of the grid (the whole row, not just a column.).

If you put the criteria check in the visible param of the grid column, then yes,it would only hide the column. But what you’ve done here is take something that could (and should) be handled by the database and bypassed it so that you’re getting all those rows back and skipping them after. Mdomba’s solution is exactly the one you need and should work perfectly as long as you’re putting the check in the right place.

Oh alright I’ll try it out

Awesome, I got it working. However, the filters in my CGridView seem to completely ignore the rules I set in search. Here is my CGridView setup:

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






                        'name' => 'pid',

                        'header' => 'Program',

                        'value' => '($data->program->prog_name)',

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




                        'name' => 'uid',

                        'header' => 'User',

                        'value' => '($data->user->username)',






When I load the page it shows the appropriate rows, but if you type something into ANY filter it can show many or all of the rows. How do I make the filter adhere to the rules set by search? Thanks.

How are you populating that dataProvider? Usually, if you’re using the search function of the model, it’d be listed there as ‘dataProvider’=>$model->search()

Yeh I’m using $model->search()…I read in another topic that the AJAX filter ignores the original search restrictions…that’s pretty disappointing. I’ll have to hack a solution in there unless someone knows a good way to do it? Thanks.