[SOLVED] CGridView DropDownList filter initial selection

Hi all,

I want to set an initial selection to the dropdownlist filter in a CGridView. How can I do this?

I could set a dropdownlist filter like this …




$itemFilter = array('1'=>'Item #1', '2'=>'Item #2', '3'=>'Item #3');

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

    'id' => 'foobar-grid',

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

    'filter' => $model,

    'columns' => array(

        array(

            'name' => 'item',

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

            'filter' => $itemFilter,

        ),

        ....



And I want to select ‘Item #1’ for the initial filter.

Do I have to write some javascript?

Thanks in advance.

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.

The filter values are in the current instance of the model…


'filter' => $model,

so in the controller… after the $model->unsetAttributes()… .you can assign any default value you need like


$model->itemName = <default value>;

Yes, it’s solved. :lol:

Thank you, mdomba, it works just as I wanted.

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.

Cheers,

Trejder

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.

/Tommy

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.

But this has to be implemented using PHP’s (Yii’s) getters/setters magic, right? As model doesn’t have field named “item”…

I expressed my thoughts wrongly. I was aware, what are you talking about. It was just not that obvious how CGridView’s filtering works this way.

Thanks for clearing this out to me.