Search multiple fields Gridview

Is there a way to have just one text field that is used to search all of the columns in a gridview?

Currently I’m using a separate CActiveForm to perform the search b/c I need a search button to show. Our clients are not going to intuitively know to press enter after typing into the filter. Here’s my form:




<?php

$form=$this->beginWidget('CActiveForm', array(

'id'=>'filterProperties',

	'action'=>Yii::app()->createUrl($this->route),

	'enableAjaxValidation'=>true,

	'method'=>'get',

)); ?>


	<div class="row">

		<?php echo 'Search: '; ?>

		<?php echo $form->textField($data,'name',array('size'=>40,'maxlength'=>50 )); ?>

		<?php echo CHtml::submitButton('Search'); ?>

		<?php echo '&nbsp|&nbsp&nbsp'; echo CHtml::link('Reset Filter', Yii::app()->createUrl($this->route)); ?>

	</div>

	

<?php $this->endWidget(); 




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

	'id'=>'properties-grid',

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

	'filterPosition'=>'header',

	'selectableRows'=>1,

	'selectionChanged'=>'function(id){ location.href = "'.$this->createUrl('view').'/id/"+$.fn.yiiGridView.getSelection(id);}',

	'columns'=>array(

		array('type'=>'html','name' => 'name','value'=>  '$data->getNameConfidential()'),

		array('type'=>'html','name' => 'date_opened','value'=>  '$data->getDateOpened()'),

		array('type'=>'html','name' => 'rooms','value'=>  'CHtml::encode($data->rooms)'),

		array('type'=>'html','name' => 'structure','value'=>  'CHtml::encode($data->structure)'),

		array('type'=>'html','name'=> 'price', 'value'=>'number_format($data->price,0)', )

	)

)); 

?>



Just do a search form like the one generated by gii for the admin view.

In this for you will collect only a text field, let’s say globalSearch.

Write your search function as follows:


$criteria->addCondition("field1 like '%:globalSearch%' OR field1 like  '%:globalSearch%' ...");

$criteria->params['

:globalSearch’]= $this->gloabalSearch;

Awesome, thanks for leading me in the right direction. I had to change the condition a bit and also add a line in the rules() in the model. Here’s my finished code:




if($this->filterProperties){

			

	$criteria ->addCondition("name LIKE CONCAT('%', :filterProperties , '%') OR city LIKE CONCAT('%', :filterProperties , '%')");

	$criteria->params = array(':filterProperties' => $this->filterProperties);

}



Apparently it is a bit tricky doing params with the LIKE function, will only work if you Concat the % signs and the param.

Added the rule:




array('filterProperties','length', 'max'=>70),



and my _grid-view.php:




<?php

$form=$this->beginWidget('CActiveForm', array(

'id'=>'filterProperties',

	'action'=>Yii::app()->createUrl($this->route),

	'enableAjaxValidation'=>true,

	'method'=>'get',


)); ?>


<div class="row">

	<?php echo 'Search: '; ?>

	<?php echo $form->textField($data,'filterProperties',array('size'=>40,'maxlength'=>70 )); ?>

	<?php echo CHtml::submitButton('Search'); ?>

	<?php echo '&nbsp|&nbsp&nbsp'; echo CHtml::link('Reset Filter', Yii::app()->createUrl($this->route)); ?>

</div>



Thanks again.

Dear jstockdale,

Could you show me full code of this example. I had google but can’t find anything I need.

Thank a lot!

Everything solve.

I add var: public $filterProperties




<?php


class User extends CActiveRecord

{

public $filterProperties;

	/**

	 * Returns the static model of the specified AR class.

	 * @param string $className active record class name.

	 * @return User the static model class

	 */



Thanks jstockdale!