Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery ui Yii2-自动完成输入搜索_Jquery Ui_Yii_Autocomplete_Yii2 - Fatal编程技术网

Jquery ui Yii2-自动完成输入搜索

Jquery ui Yii2-自动完成输入搜索,jquery-ui,yii,autocomplete,yii2,Jquery Ui,Yii,Autocomplete,Yii2,我使用的是默认的Yii2。我怎样才能使它在用户键入时从数据库中读取值呢 这是我到目前为止的代码,但在创建页面时会执行查询: echo AutoComplete::widget([ 'name' => 'tradeName', 'model' => TradeNames::find()->select('name')->all(), 'options' => [

我使用的是默认的Yii2。我怎样才能使它在用户键入时从数据库中读取值呢

这是我到目前为止的代码,但在创建页面时会执行查询:

echo AutoComplete::widget([
                'name' => 'tradeName',
                'model' => TradeNames::find()->select('name')->all(),
                'options' => [
                    'class' => 'form-control'
                ],
                'clientOptions' => [
                        'source' => array_column(TradeNames::find()->select('name')->asArray()->all(), 'name'),

                        },
                    ],
            ]);
试试这个

use yii\jui\AutoComplete;
use yii\web\JsExpression;

<?php
            $data = TradeNames::find()
            ->select(['name as value', 'name as  label','id as id'])
            ->asArray()
            ->all();

            echo 'Trade Names' .'<br>';
            echo AutoComplete::widget([
                 'name' => 'tradeName',    
                 'id' => 'trade_name',
                 'clientOptions' => [
                    'source' => $data,
                    // 'minLength'=>'3', 
                    'autoFill'=>true,
                    'select' => new JsExpression("function( event, ui ) {
                    $('#memberssearch-family_name_id').val(ui.item.id);//#memberssearch-family_name_id is the id of hiddenInput.
                 }")],
                 ]);
            ?>


 <?= Html::activeHiddenInput($model, 'tradeName')?>
使用yii\jui\AutoComplete;
使用yii\web\JsExpression;
我听从了这个建议 并编写了下一个代码

<div id="autocomplete" class="ui-widget">
    <?= \yii\jui\AutoComplete::widget([
        'attribute' => 'attribute',
        'name' => 'tradeName',
        'clientOptions' => [
            'source' => \Yii::$container->get('JsExpression',['function(request, response) {
                  response( $.ui.autocomplete.filter( window.dataAsArray, extractLast( request.term ) ) );
            }']),
            'select' => \Yii::$container->get('JsExpression',['function(event, ui) {
                var terms = split( this.value );
                terms.pop();
                terms.push( ui.item.value );
                terms.push( "" );
                this.value = terms.join( ", " );
                return false;
            }']),
            'focus' => \Yii::$container->get('JsExpression',['function() {
                return false;
            }']),
        ]
    ]) ?>
</div>  

<script>
    window.dataAsArray = ['item1', 'item2', 'item3'];
    function split( val ) {
         return val.split( /,\s*/ );
    }

    function extractLast( term ) {
        return split( term ).pop();
    }
    $(document).ready( function() {
        $('#autocomplete').on('keydown', function( event ) {
            if ( event.keyCode === $.ui.keyCode.TAB && $( this ).autocomplete( "instance" ).menu.active ) {
                event.preventDefault();
            }
         });
    });
</script>