result by AJAX not selectable

Hi guys,

following code will give me all records using AJAX request as intended. Unfortunately, I am not able to adopt one selected record into formular. I just can select them, not take them(Look at my attachement,please). I assume some error in templateResult or templateSelection. Any idea, where is bug?

Here is code of formuar:





 <?php

                                $url = \yii\helpers\Url::to(['berufe']);

                                ?><?=

                                $form->field($model, 'id_ba_xml_gelernter_beruf_1', ['addon' => [

                                        'prepend' => ['content' => 'gelernter Beruf 1']]])->widget(\kartik\widgets\Select2::classname(), [                                  

                                    'options' => ['placeholder' => 'Berufe suchen ...'],

                                    'pluginOptions' => [

                                        'allowClear' => true,

                                        'minimumInputLength' => 3,

                                        'language' => [

                                            'errorLoading' => new JsExpression("function () { return 'Waiting for results...'; }"),

                                        ],

                                        'ajax' => [

                                            'url' => $url,

                                            'dataType' => 'json',

                                            'data' => new JsExpression('function(params) { return {q:params.term}; }')

                                        ],

                                        'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),

                                        'templateResult' => new JsExpression('function(id_ba_xml_gelernter_beruf_1) { return id_ba_xml_gelernter_beruf_1.text; }'),

                                        'templateSelection' => new JsExpression('function (id_ba_xml_gelernter_beruf_1) { return id_ba_xml_gelernter_beruf_1.text; }'),

                                    ],

                                ])->label(false);

                                ?>



Here is ControllerCode which works pretty fine





    public function actionBerufe($q = null, $id = null) {

        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;

        $out = ['results' => ['id' => '', 'text' => '']];

        if (!is_null($q)) {

            $query = new \yii\db\Query();

            $query->select('ID, Bezeichnung_neutral_kurz  AS text')

                    ->from('yii2_ba.ba_berufe')

                    ->where(['like', 'Bezeichnung_neutral_kurz', $q])

                    ->limit(20);

            $command = $query->createCommand();

            $data = $command->queryAll();

            $out['results'] = array_values($data);

        } elseif ($id > 0) {

            $out['results'] = ['id' => $id, 'text' => \common\modules\basis\models\BaBerufe::find($id)->Bezeichnung_neutral_kurz ];

        }

        return $out;

    }



Thanks in advance for any help,which could solve my problem

Could it be that here


$out['results'] = array_values($data);

you are retuning a one-dimensional array while for each result you should return an object


[ id => "id", text => "text" ]

?

I dont’t think it’s Controllercode, which is faulty, 'cause I will get records as intended. I just can’t adopt them into formular. Apart from that, ur suggestion will give me an internal server error




[ id => "id", text => "text" ]



text is not defined.

P.S.: Controller code is from kartik, so it should be all right!

Problem still is unsolved