How can you set the default option in the gridview filter dropdowns?

So, it would be nice to be able to set the default filter selected option for the filter dropdowns in gridview. Ideally, you could select any one of the options as well as re-title the top blank one that basically isn’t filtering at all. There doesn’t seem to be any way to do it outside of jquery hacks.

The other issue is that each time you sort or filter the rows in the grid, any jquery applied to those dropdowns is removed. I tried the 3 “afterUpdate” properties of the grid view widget, i.e. afterAjaxUpdate, beforeAjaxUpdate, and selectionChanged. none of them trigger a javascript function after you sort or filter the rows. Maybe I’m doing something wrong, but I’d also like to learn how to call a callback javascript function after those actions, even if there is a better way to solve the core problem than that.

Anyone?

You can set default options to the filter in your controller action, without any hack in javascript.

For example:




	public function actionAdmin()

	{

		$model=new Robot('search');

		$model->option='default value'; // set a default option

		if(isset($_GET['Robot']))

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


		$this->render('index',array(

			'model'=>$model,

		));

	}



If the user select something else for this filter, the default option will be overridden by $model->attributes=$_GET[‘Robot’];

Is that what you need?

yea, that’s exactly what i needed. thanks so much. but it still doesn’t let me state a default option that acts as a title for the dropdown that basically does no filtering. there must be a way to do that too. what do u suggest?

By default the filters in a gridview are text fields… are you doing a dropdown box for filters instead of text fields?

I’m attempting to clarify the solution for others who may need the same, the “$model->column” text was the change I made to emphasize that the column name should be substituted.




public function actionAdmin()

        {

                $model=new Robot('search');

                $model->column='default value'; // set a default option for the column

                if(isset($_GET['Robot']))

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


                $this->render('index',array(

                        'model'=>$model,

                ));

        }



Thanks again Zaccaria.

Cheers!