Cjuidatepicker Order A Gridview

Hi,

I’m a newb programmer who is trying to set up a webapp using Yii and I’m learning on the fly. My app is based on the Yii Blog.

I have this CRUD to enter events witha given date. For this date I’m using CJuiDatePicker in the form. In my Index action I can set a criteria to order by DATE ASC with the CActiveDataProvider but i’m wondering how i can do this for the Admin action.

Any ideas how I can decently order my entries by date for the admin gridview?

The default workflow is:

  1. In actionAdmin a new model is created

  2. This model will be populated with the submitted $_GET params form the CGridView filter inputs.





public function actionAdmin() 

{

   $model = new MyModelClass();

   $model->unsetAttributes();

   if(isset($_GET[MyModelClass'])) //assign the values from the filter inputs of the CGridView

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

  

  $this->render( ...)

}




  1. In the admin view the dataprovider will be created and assigned by $model->search() method;

There the currently assigned attributes (by $_GET in the actionAdmin) will be used to create the CDbCriteria for the dataprovider (by method ‘compare’).

So take a look at the model::search() method generated by gii.

Inside this method you can add all want to do with the models:

filtering, sorting … maybe dependent on a submitted field from the admin view.




$this->widget(CGridView',array(

	...

        'dataProvider'=>$model->search(), //the dataprovider with the filtered data

        ...







	public function search()

	{		

		$criteria=new CDbCriteria;


		$criteria->compare(....);

		$criteria->compare(....);

		

                $criteria->order = 'date ASC'; //your sort criteria





		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}