Kartik typeahead update value in hidden field

How to update selected value’s id in hidden field? i have used typeahead:selected event but not getting any soution.

My Response is as below


[{"id1":"value1","id2":"value2"..}]

Typeahead with remote search :




<?= $form->field($model,'city')->widget(Typeahead::className(),[

    'options' => ['placeholder' =>'Please enter a city'],

    'pluginOptions' => ['highlight'=>true],

    'scrollable' => true,

    'dataset' =>[

      [

        'prefetch'  => Url::to(['airports/city-selector']),

        'limit'    => 10,

        'remote'  => [

          'url'  => Url::to(['airports/city-selector']) . '?q=%QUERY',

          'wildcard' => '%QUERY'

        ]

      ]

    ],

    'pluginEvents' => [

      "typeahead:selected" => 'function(ev, resp) {console.log(ev,resp); $("#airports-city_id").val(resp.kod); }',

    ]

    

    

  ]);

Textbox


<?= $form->field($model, 'city_id')->textInput(['maxlength' => true]) ?>

Your response is probably not ok

My response:

$result[] = [
        'filter' => $profile->product_filters,
        'value' => $profile->profile_name
    ];
Yii::$app->response->format = Response::FORMAT_JSON;

The Widget settings:

echo \kartik\typeahead\Typeahead::widget( [
                    'id'            => 'profileautocomplete',
                    'name'          => 'profile',
                    'scrollable'    => true,
                    'pluginOptions' => [
                        'highlight' => true,
                        'minLength' => 2,
                    ],
                    'options'       => [
                        'placeholder' => Yii::t('common', 'Type in for suggestion..')
                    ],
                    'dataset'       => [
                        [
                            'display' => 'value',
                            'limit'   => 10,
                            'remote'  => [
                                'url'      => Url::base().'/profile/index?profile=%QUERY',
                                'wildcard' => '%QUERY',
                            ]
                        ]
                    ],
                    'pluginEvents' => [
                        "typeahead:select" => "function(e, val) {
                        jQuery('#searchbox').val(val.filter);
                        }",
                    ]
                ]
            );

In my example I used a real input field not a hidden and filled it’s value with the value of val.filter from the server. It is working.