Yii CJuiAutoComplete Source

Yii CJuiAutoComplete Source.This tutorial will help you to understand yii cjuiautocomplete with ajax. Here I wrote the code for CJuiAutoComplete. You can download the source code for yii cjuiautocomplete

CJuiAutoComplete


<?php
$this->widget('zii.widgets.jui.CJuiAutoComplete',array(
    'name'=>'normal',
    'source'=>array('ac1','ac2','ac3'),    
    'options'=>array(
        'minLength'=>'1',
    ),
    'htmlOptions'=>array(
        'style'=>'height:20px;',
    ),
));
?>

Ajax CJuiAutoComplete

Auto Complete View


<?php
//In autocompleteview.php
$this->widget('zii.widgets.jui.CJuiAutoComplete', array(
    'name'=>'city1',
    'source'=>$this->createUrl('site/autocomplete'),
    // additional javascript options for the autocomplete plugin
    'options'=>array(
        'minLength'=>'1',
    ),
    'htmlOptions'=>array(
        'style'=>'height:20px;'
    ),
));
?>

SiteController.php


// Write the following code in your SiteController.php
<?php
class SiteController extends Controller
{
    public function actionAutocomplete () {
        $value=$_REQUEST['term'];
        if($value=='a')
            $return_array=array('ac1', 'ac2', 'ac3');
        else if($value=='b')
            $return_array=array('bb1', 'bbbb2', 'bbb3');
        else if($value=='c')
            $return_array=array('c1', 'c2', 'c3');
        else if($value=='d')
            $return_array=array('d1', 'ddd2', 'dddd3');
                
        echo CJSON::encode($return_array);
    }
}