dougengqiu8031 2013-01-07 14:12 采纳率: 100%
浏览 128
已采纳

如何在yii中创建自定义自动填充文本字段

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

  • 写回答

2条回答 默认 最新

  • duanlan8763 2013-01-08 01:02
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号