Seems to be hard to achieve anyway. Even with JavaScript / jQwery you need to catch that item, you want to make selected by default and you can’t do this, because I don’t see anyway for referencing items in filters drop-down list, since for building it, a simple array value->item is used.
If I’m not mistaken, in current implementation, framework is not ready for operations like you ask for. But maybe someone else will see a solution.
Hey guys, Either I’m really, really tired (nearly midnight here) or I’ve got a little bit lost! :[ I mean… what are you talking about?
What is relation between setting anything in controller to having default value selected in CGridView’s filters?
I don’t have access to Yii/PHP from where I’m writing, so I can’t test any piece of code, but if I’m not mistaken - controller has nothing to do with initial (default) value of particular filter in particular CGridView column. In my example, I fed CGridView always with model->search() method (data provider) and - again, if I’m not mistaken - this means that my CGridView is being filled on view render stage, far after controller executing particular action. So, how then controller can influence what is preselected in filters drop down list?
Again - I’m tired and this is my just purely theoretical bla, bla. I don’t have access to PHP. Probably, if I would this would become very obvious. But right now - it isn’t.
The model instance passed from the controller will be assigned to the filter property of the CGridView widget. The dataprovider property of the widget will be assigned the result of $model->search(), with filtering applied. Note that search() is called on the actual filter instance.
It’s in the actionAdmin() function of a gii-genarated CRUD controller. It’s quite simple.
/**
* list for administration
*/
public function actionAdmin()
{
$model = new Foobar('search');
$model->unsetAttributes(); // clear any default values
+ $model->item = 1; // select the default value for item ... just added
if (isset($_GET['Foobar']))
{
$model->attributes = $_GET['Foobar'];
}
$this->render('admin',array(
'model'=>$model,
));
}
I just had to add the 3rd line in the function … $model->item = 1 … to set the default value of the target attribute. And this can be overwritten by $_GET[‘Foobar’] when you call this action later from the view.
It also works for a textField filter, not only for a dropDownList filter.