I want to filter an GridView by using an autocomplete field since the table of values has over 4000 entries and a simple dropdown wouldn’t help. what i’ve tried so far lots of ways. closest i got was this way :
View file:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'activitate-grid',
'afterAjaxUpdate' => 'reinstallDatePicker',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
array(
'name'=>'id_angajat',
'filter'=>CHtml::listData(Angajati::model()->findAll(array('order'=>'nume')), 'id_angajat', 'nume'),
'value' => '$data->idAngajat->nume',
),
array(
'name'=>'id_produs',
'filter'=>$this->widget('CAutoComplete',
array(
//name of the html field that will be generated
'name'=>'Activitate[id_produs]',
//replace controller/action with real ids
'url'=>array('prodLookup'),
'max'=>10, //specifies the max number of items to display
//specifies the number of chars that must be entered
//before autocomplete initiates a lookup
'minChars'=>2,
'delay'=>500, //number of milliseconds before lookup occurs
'matchCase'=>false, //match case when performing a lookup?
//any additional html attributes that go inside of
//the input field can be defined here
'htmlOptions'=>array('size'=>'20', 'placeholder'=>'cauta produs'),
'methodChain'=>".result(function(event,item){\$(\"#Activitate_id_produs\").val(item[1]);})",
)), true),
..........
Controller code:
public function actionProdLookup()
{
if(Yii::app()->request->isAjaxRequest && isset($_GET['q']))
{
/* q is the default GET variable name that is used by
/ the autocomplete widget to pass in user input
*/
$denumire = $_GET['q'];
// this was set with the "max" attribute of the CAutoComplete widget
$limit = min($_GET['limit'], 50);
$criteria = new CDbCriteria;
$criteria->condition = "denumire LIKE :sterm";
$criteria->params = array(":sterm"=>"%$denumire%");
$criteria->limit = $limit;
$prodArray = Produse::model()->findAll($criteria);
$returnVal = '';
foreach($prodArray as $prod)
{
$returnVal .= $prod->getAttribute('denumire').'|'
.$prod->getAttribute('id_produs')."\n";
}
echo $returnVal;
}
}
This works perfect in any form. in my gridview it appears above the table and it dosen’t work. really need help since i’ve tried to fix it for 2 days and can’t seem to figure it out.