Difference between #1 and #3 of
History Autocomplete

Changes

Title unchanged

History Autocomplete

Category unchanged

How-tos

Yii version unchanged

Tags unchanged

autocomplete

Content changed

[...]
Often you can solve with a foreign field, with an interface for add a new color. That's great if adding new color is a quite rare operation, if it happens too often and if the users are too stupid for remember how to add a color, it can be odd.

I propose an half way solution: a free text with autocomplete based on history values.

Add this to an CHtml extension:
 
 
 
 
 
```php 
class ZHtml extends CHtml
 
{
 
public function activeTextFieldHistory($model, $attribute, $htmlOptions=null)
 
{
 
 
$mod=$model;
 
$attr=$attribute;
 
self::resolveNameID($mod,$attr,$htmlOptions);
 
$route=array('/site/search', 'model'=>get_class($model), 'attribute'=>$attr);
 
if (isset($htmlOptions['filter']))
 
        {
 
            $route['filter']=$htmlOptions['filter'];
 
            unset($htmlOptions['filter']); 
 
        }
 
        
 
$this->widget('zii.widgets.jui.CJuiAutoComplete', 
 
array('model'=>$model,
 
  'attribute'=>$attr, 
 
  'htmlOptions'=>$htmlOpt
We will use CJuiAutoComplete, what we need is a efficient way for perform searches.
 
 
Add this to your components/extens
ions,:   'sourceUrl'=>$route));
 
}
 
}
 
```
 
 
You can pass additional configuration for the search (e.g, some more condition limiting the search in a project/group, sorting or whatever else)
 
 
And add this to your SiteController:

 
 
```php 
class SearchAction  extends CAction
{
 
 
 
```php 
public function actionSearch()
 
{
 

 
$model=$_GET['model'];
 
$attribute= $_GET['
public $model;
 
public $
attribute']; $param=addslashes($_GET['term']);
 
$results=array
public $criteria=array('limit'=>5);
 

 
public function run
(); {   $criteria=new CDbCriteria($this->criteria); $criteria->caddCondition="$("{$this->attribute} IS NOT NULL AND ${$this->attribute} LIKE '%$:param%' ";
 
if (isset($_GET['filter']))
 
$criteria->condition.=' AND '.$_GET['filter']
"); $criteria->group=$this->attribute; $criteria->order="COUNT(${$this->attribute}) desc"; $criteria->limit=5;
 
$records=
params[':param']='%'.$_GET['term'].'%';
 
 
 
$results=array();
 
foreach (
CActiveRecord::model($this->model)->findAll($criteria);
 

 
foreach ($records
as $record) $results[]=$record->${$this->attribute;
 

 
}; 
 
 
echo CJSON::encode($results);
 

 
 
```
  }
 
}
 
 
```
 
 
Now we can configure a search action in our controller like that:
 
 
 
 
 
```php 
public function actions()
 
{
 
return array(
 
'searchCompany'=>array(
 
'class'=>'SearchAction',
 
'model'=>'Request',
 
'attribute'=>'company',
 
)
 
);
 
}
 
```
 
 
 
 
You can pass additional configuration for the search (e.g, some more condition limiting the search in a project/group, sorting or whatever else) using the property $criteria.
Now you can create your search textfield as:
 
```php ZHtml::activeTextFieldHistory($model, 'color');<?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.
3 0
9 followers
Viewed: 20 406 times
Version: 1.1
Category: How-tos
Written by: zaccaria
Last updated by: Maurizio Domba Cerin
Created on: Jan 23, 2012
Last updated: 12 years ago
Update Article

Revisions

View all history