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?
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
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:
It also supports ajax loaded data: http://demos.krajee.com/widget-details/select2#ajax
This can be solved with the help of a hidden field input.Hope this will help somebody!