如何创建自定义的Yii自动完成文本字段

问题描述:

我是新来的Yii。我需要编写自定义的Yii自动完整地填写知道CJuiAutocomplete是there.but我需要实现自己的自定义自动完成。任何人都可以PLS指导我还是帮我开发定制的自动完成文本框。取号,而在文本框显示名称。

I am new to yii. I need to write custom yii auto complete.I knew that CJuiAutocomplete is there.but I need to implement own custom auto complete. can anyone pls guide me or help me to develop custom autocomplete textfield. taking the id while displaying name in the textfield.

在此先感谢

下面是现场控制器的动作......

Here is an action in site controller...

public function actionAutoComplete($term){

    $query = Yourmodel::model()->findallbyattributes( array('somecolumn'=>$term));
    $list = array();        
    foreach($query as $q){
        $data['value']= $q['id'];
        $data['label']= $q['name'];

        $list[]= $data;
        unset($data);
    }

    echo json_encode($list);
}

和这里是在您的视图搜索表单:

and here is a search form in your view:

$form=$this->beginWidget('CActiveForm', array(
'id'=>'searchform',
'enableAjaxValidation'=>false,
'action' => '/'
)); ?>

    <fieldset>
        <div class="input-append">
        <?php

        echo CHtml::hiddenField('selectedvalue','');

         $this->widget('zii.widgets.jui.CJuiAutoComplete', array(
            'name'=>'searchbox',
            'value'=>'',
            'source'=>CController::createUrl('/site/autoComplete'),
            'options'=>array(
            'showAnim'=>'fold',         
            'minLength'=>'2',
            'select'=>'js:function( event, ui ) {
                        $("#searchbox").val( ui.item.label );
                        $("#selectedvalue").val( ui.item.value );
                        return false;
                  }',
            ),
            'htmlOptions'=>array(
            'onfocus' => 'js: this.value = null; $("#searchbox").val(null); $("#selectedvalue").val(null);',
            'class' => 'input-xxlarge search-query',
            'placeholder' => "Search...",
            ),
            ));
            echo '<button class="btn" type="submit">Submit</button>';   

        ?>
        </div>
    </fieldset>

<?php $this->endWidget(); ?>    
</form>