Problems with autocomplete in a form field

Hi,

i fiddle a while with this problem.

i want to use a form-field with an id of an related table, in this case order - customer. when i use this code with a string form-field, its working.

but i want an autocomplete field wher i see two field of the related table, select on item and get the id in my form-field.

in this code i get no suggesttion list displayes, the source list is displayed.

i tried for source:


'source' => ArrayHelper::getColumn($data, 'name'),

and


'source' => ArrayHelper::getColumn($data, 'id'),

and


'source' => $data,

this is working, but not what i want.


<?= $form->field($model, 'name')->widget(AutoComplete::classname(), [

                'clientOptions' => [

                    'source' => $data,

                    'autoFill'=>true,

                    'minLength'=>'0',

                ],

            ]) ?>






use yii\jui\AutoComplete;

use yii\web\JsExpression;


$data = Customer::find()

            ->select(['CONCAT(nachname," ",vorname) as name','CONCAT(nachname," ",vorname) as value', 'id as id'])->distinct()  

            ->orderBy(['name' => SORT_ASC])

            ->asArray()

            ->all();




<?= $form->field($model, 'customer_id')->widget(AutoComplete::classname(), [

              'clientOptions' => [

                    'source' =>  ArrayHelper::getColumn($data, 'name'),//$data,

                    'select' => new JsExpression("function( event, ui ) {

                                        $('#model-order-cust').val(ui.item.id);

                                        }"),

                    'autoFill'=>true,

                    'minLength'=>'0',

                ],

            ]) ?>



any Ideas? Hope to map show a string list an set an id to a form field? is this possible?

Hi,

First create a new public property in your model, say "$customer_nochname" will act as String representaion for the user from UI i.e autocomplete.

Put the $customer_id in inputHidden field.

When the select event occurs on autocomplete of "$customer_nochname" then assign the id to your input hidden field "$customer_id" as value.

Your model file may have to be this




class Yourmodel extends ActiveRecord{


public $customer_nochname;


//Add it to the right validation rules too


//Other Stuffs

.

.

.




Your view file may have to be this







use yii\jui\AutoComplete;

use yii\web\JsExpression;


$data = Customer::find()

            ->select(['CONCAT(nachname," ",vorname) as name','CONCAT(nachname," ",vorname) as value', 'id as id'])->distinct()  

            ->orderBy(['name' => SORT_ASC])

            ->asArray()

            ->all();




<?= $form->field($model, 'customer_nochname')->widget(AutoComplete::classname(), [

              'clientOptions' => [

                    'source' =>  ArrayHelper::getColumn($data, 'name'),//$data,

                    'select' => new JsExpression("function( event, ui ) {

                                          $('#model-customer-id').val(ui.item.id);

                                        }"),

                    'autoFill'=>true,

                    'minLength'=>'0',

                ],

            ]) ?>




<?= $form->field($model, 'customer_id',['id' => 'model-customer-id'])->hiddenInput()->label(false);  ?>