Changes
Title
unchanged
History Autocomplete
Category
unchanged
How-tos
Yii version
unchanged
Tags
unchanged
autocomplete
Content
changed
[...]
public $model;
public $attribute;
public $criteria=array('limit'=>5);
public function run()
{
{
$criteria=new CDbCriteria($this->criteria);
$criteria->addCondition("{$this->attribute} IS NOT NULL AND {$this->attribute} LIKE :param");
$criteria->group=$this->attribute;
$criteria->order="COUNT({$this->attribute}) desc";
$criteria->params[':param']='%'.$_GET['term'].'%';
$results=array();
foreach (CActiveRecord::model($this->model)->findAll($criteria) as $record)
$results[]=$record->{$this->attribute};
echo CJSON::encode($results);
}
}
}[...]
```php
public function actions()
{
return array(
'searchCompany'=>array(
'class'=>'SearchAction',
'model'=>'Request',
'attribute'=>'company',
)
);
}
```[...]
```php
<?php $this->widget('zii.widgets.jui.CJuiAutoComplete',
array('model'=>$model,
'attribute'=>'company',
'htmlOptions'=> array('maxlength'=>300, 'class'=>'text'),
'sourceUrl'=>'searchCompany'));?>
```
This method is quite efficient and usually customer like it, because is based on the laziness of the users.