沃梦达 / 编程问答 / php问题 / 正文

如何在 yii 中创建自定义自动完成文本字段

how to create custom autocomplete textfield in yii(如何在 yii 中创建自定义自动完成文本字段)

本文介绍了如何在 yii 中创建自定义自动完成文本字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 yii 的新手.我需要编写自定义 yii 自动完成.我知道 CJuiAutocomplete 在那里.但我需要实现自己的自定义自动完成.任何人都可以指导我或帮助我开发自定义自动完成文本字段.在文本字段中显示名称时获取 id.

提前致谢

解决方案

这是站点控制器中的一个动作...

公共函数 actionAutoComplete($term){$query = Yourmodel::model()->findallbyattributes(array('somecolumn'=>$term));$list = 数组();foreach($query as $q){$data['value']= $q['id'];$data['label']= $q['name'];$list[]= $data;未设置($数据);}回声 json_encode($list);}

这是您认为的搜索表单:

$form=$this->beginWidget('CActiveForm', array('id'='=>'搜索表单','enableAjaxValidation'=>false,'动作' =>'/'));?><字段集><div class="input-append"><?phpecho CHtml::hiddenField('selectedvalue','');$this->widget('zii.widgets.jui.CJuiAutoComplete', array('名称'=>'搜索框','值'=>'','source'=>CController::createUrl('/site/autoComplete'),'选项'=>数组('showAnim'='折叠','minLength'='2','选择'=>'js:函数(事件,用户界面){$("#searchbox").val( ui.item.label );$("#selectedvalue").val( ui.item.value );返回假;}',),'htmlOptions'=>数组('焦点' =>'js: this.value = null;$("#searchbox").val(null);$("#selectedvalue").val(null);','类' =>'input-xxlarge 搜索查询','占位符' =>搜索...",),));echo '<button class="btn" type="submit">Submit</button>';?>

</fieldset><?php $this->endWidget();?></表单>

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.

Thanks in advance

解决方案

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>

这篇关于如何在 yii 中创建自定义自动完成文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在 yii 中创建自定义自动完成文本字段

基础教程推荐