Pragmatically select a row in CGridView

Hi,

On creation of a new model, say on creation of new Post I want the page to redirect to the admin view with the new record that was created selected. So there are 2 issues here:

  1. This new record may not be on the first page of the CGridView.So how do I pragmatically navigate to the page?

  2. How can I set the selection for the row displaying the new record

Thanks in advance.

You can set ordering in CGridView to ensure newest record is on the top like this:

in your model class:





public function search(){


	$criteria=new CDbCriteria;

	

	... your code...

	

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

		'criteria'=>$criteria,

		'sort'=>array(

			'defaultOrder'=>'id DESC',

		  )

	));

}




See more:

http://www.yiiframework.com/doc/api/1.1/CSort

Lubos

Thanks a lot for replying.

The problem is I don’t want to change the order. I want to go to the page and select the new record pragmatically.

I want the gridview to display records sorted on a different attribute.

When I use sorting…the dataProvider itself is sorted by the id and would be always sorted on the id. :(