Question

In Yii2 I want one of my input field to be autocomplete when user starts to type.Below is my code which uses Jui Autocomplete.

 <?php
    $items= ArrayHelper::map(Company::find()->all(), 'c_id', 'name');
    echo AutoComplete::widget([
    'model' => $model,
    'attribute' => 'company',
    'clientOptions' => [
    'source' => $items,
     ],
    ]);?>

This is not working.When i printed my array, i got like

 Array ( [1] => abc [2] => xyz [4] => pqr )

I got it working when i manually set like

 $items=['abc','xyz','pqr'];

The reason may be my c_id's are not ordered?But i want to get the c_id value to be submitted!Any idea how to fix this?

Was it helpful?

Solution

This can be solved with the help of a hidden field input.Hope this will help somebody!

    <?php
    use yii\web\JsExpression;

    $data = Company::find()
    ->select(['name as value', 'name as  label','c_id as id'])
    ->asArray()
    ->all();

    echo AutoComplete::widget([
    'name' => 'Company',
    'id' => 'ddd',
    'clientOptions' => [
        'source' => $data,
        'autoFill'=>true,
        'minLength'=>'4',
        'select' => new JsExpression("function( event, ui ) {
                $('#user-company').val(ui.item.id);
            }")
        ],
     ]);
     ?>

    <?= Html::activeHiddenInput($model, 'company')?>

OTHER TIPS

Autocomplete just helps you fill the field with required value. If you need to submit c_id look to dropdownList or Select2 plugin.

Check this http://demos.krajee.com/widget-details/select2 yii2 widget for ideas. Here example code:

<?php
  use kartik\widgets\Select2;
  use app\models\Modelname;

  $model = new Modelname;

  $data = ['qwe1'=>'color1','key2'=>'color3']

  ?>

  <?= Html::beginForm() ?>

  <?= Select2::widget([
    'model' => $model,
    'attribute' => 'color',
    'data' => array_merge(["" => ""], $data),
    'options' => ['placeholder' => 'Select a state ...'],
    'pluginOptions' => [
      'allowClear' => true
    ],
  ]); ?>

  <?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>

  <?= Html::endForm() ?>

It also supports ajax loaded data: http://demos.krajee.com/widget-details/select2#ajax

I wanted to use the Jui Autocomplete so that when I click or focus on autocomplete textbox, it should display options.

I wrote following code and it seems to be working

 $floorOptionsArray = ['Basement', 'Ground Floor', 'First floor', 'Second floor', 'Third floor'];
//    $floorOptionsArray = array_combine($floorOptionsArray, $floorOptionsArray);

$model = new Customer();

echo $form->field($model, 'floor')
    ->widget(\yii\jui\AutoComplete::classname(), [
        'value' => (!empty($model->floor) ? $model->floor : ''),
        'clientOptions' => [
            'source' => $floorOptionsArray,
            'enabled' => true,
            'minLength' => 0
        ],

        'options' =>
            [
                'placeholder' => 'Floor',
                'class' => 'form-control autocomplete-input-bg-arrow ',

                'onclick' => "(function ( ) {
                      $( '#customer-floor' ).autocomplete( 'search', '' );
                                })();",

                'onfocus' => "(function ( ) {
                      $( '#customer-floor' ).autocomplete( 'search', '' );
                                })();",
            ],
    ])->label(true);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top