Yii Framework Forum: Help With Live Search In Cgridview - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Help With Live Search In Cgridview Need to search more than one column Rate Topic: -----

#1 User is offline   shingionline 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 28-October 12

Posted 18 November 2012 - 10:33 AM

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()));
}

Attached File(s)


0

#2 User is offline   Reza m 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 148
  • Joined: 07-March 12

Posted 18 November 2012 - 11:38 AM

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()));
}

0

#3 User is offline   shingionline 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 13
  • Joined: 28-October 12

Posted 18 November 2012 - 12:00 PM

View PostReza m, on 18 November 2012 - 11:38 AM, said:

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 :rolleyes:
0

#4 User is offline   smiley 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 06-June 12

Posted 07 December 2012 - 12:05 AM

View Postshingionline, on 18 November 2012 - 12:00 PM, said:

Thank you so much, this works perfectly :rolleyes:



Thank U so much . This also helps me a lot.
0

#5 User is offline   Boil 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 04-May 13

Posted 13 June 2013 - 08:45 AM

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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users