Cgridview Show The Results Only When Filtered

I want my CGridView show the results only when filtered, how can I do this? thank you

The filter values are submitted by $_GET to your actionAdmin, where you assign the submitted attributes to your model.

So you have to check if there are no empty attribute values assigned to your model in the actionAdmin:





public function actionAdmin()

	{

		$model=new MyModel('search');

		$model->unsetAttributes();  // clear any default values


                $isFiltered=false;

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

                {

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


                        foreach($_GET['MyModel'] as $key=>$value) //or foreach($model->attributes as ...)

                        {

                          if(!empty($value)

                          {

                              $isFiltered=true;

                              break;

                          }      

                        } 

		}


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

			'model'=>$model,

                        'isFiltered'=>isFiltered, //submit to your view

		));

	}






In your view you can assign a CArrayDataProvider with no data instead the model->search() if you want to show no data.





$this->widget('CGridView',array(

	....

	'dataProvider'=>$isFiltered ? $model->search() : new CArrayDataProvider(array()),

	...

           



It works like a charm!

Thank you very much Joblo