I have a table with 4 columns and have implemented a live search box which updates the CGridView after each stroke. It works well for searching and filtering based on one column eg: first_name. Please will you let me know how i can search all 4 columns at the same time so the filter is made if keyword matches data in any column. (please see images)
My code is as follows:-
Model
public function search()
{
$criteria = new CDbCriteria;
$criteria -> select = 'first_name, last_name, sex, age';
$criteria -> compare('first_name',$this->first_name,true,);
$criteria -> compare('last_name',$this->last_name,true);
$criteria -> compare('sex',$this->sex,true);
$criteria -> compare('age',$this->sex,true);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
View
<?php
Yii::app()->clientScript->registerScript('search', "
$('input#keyword').keyup(function(){
$.fn.yiiGridView.update('yw0', {
data: $(this).serialize()
});
return false;
});
");
?>
<input type="text" id="keyword" name="keyword" />
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider'=>$dataProvider,
'columns'=>array(
'first_name',
'last_name',
'sex',
'age',
),
));
?>
Controller
public function actionIndex($keyword = '') {
$model = new User($scenario='search');
$model -> unsetAttributes();
$model -> first_name = $keyword;
$this->render('index',array('dataProvider' => $model->search()));
}
Attached File(s)
-
image1.gif (9.14K)
Number of downloads: 13 -
image2.gif (6.51K)
Number of downloads: 12 -
image3.gif (5.3K)
Number of downloads: 12

Help















