Hi there,
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()));
}
reza.m
(Reza Mms)
November 18, 2012, 4:38pm
2
in model
public $keyword;
//add this new attribute to your search rule
public function rule()
{
return array(
//other rules
array('keyword','safe', 'on'=>'search'),
);
}
public function search()
{
$criteria = new CDbCriteria;
$criteria -> select = 'first_name, last_name, sex, age';
$criteria -> compare('first_name',$this->keyword,true,);
$criteria -> compare('last_name',$this->keyword,true,'OR');
$criteria -> compare('sex',$this->keyword,true,'OR');
$criteria -> compare('age',$this->keyword,true,'OR');
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
in controller
public function actionIndex($keyword = '') {
$model = new User($scenario='search');
$model -> unsetAttributes();
$model -> keyword = $keyword;
$this->render('index',array('dataProvider' => $model->search()));
}
in model
public $keyword;
//add this new attribute to your search rule
public function rule()
{
return array(
//other rules
array('keyword','safe', 'on'=>'search'),
);
}
public function search()
{
$criteria = new CDbCriteria;
$criteria -> select = 'first_name, last_name, sex, age';
$criteria -> compare('first_name',$this->keyword,true,);
$criteria -> compare('last_name',$this->keyword,true,'OR');
$criteria -> compare('sex',$this->keyword,true,'OR');
$criteria -> compare('age',$this->keyword,true,'OR');
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
in controller
public function actionIndex($keyword = '') {
$model = new User($scenario='search');
$model -> unsetAttributes();
$model -> keyword = $keyword;
$this->render('index',array('dataProvider' => $model->search()));
}
Thank you so much, this works perfectly
smiley
(Vinif)
December 7, 2012, 5:05am
4
Thank U so much . This also helps me a lot.
Boil
(Boluch)
June 13, 2013, 1:45pm
5
Thank you shingionline, for a good simple and lightweight start, and thank you Reza m for even better corrections.
All together the topic helped me understand everything and now it works! Thanks!
Boil