Yii Framework Forum: result by AJAX not selectable - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

result by AJAX not selectable [canceled]

#1 User is offline   tklustig 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 419
  • Joined: 20-April 17
  • Location:Hannover

Posted 02 March 2018 - 08:10 AM

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

Attached File(s)

  • Attached File  AJAX.jpg (273.42K)
    Number of downloads: 10

0

#2 User is offline   Silvio Porcellana 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 31-January 18

Posted 06 March 2018 - 04:48 AM

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" ]
?
0

#3 User is offline   tklustig 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 419
  • Joined: 20-April 17
  • Location:Hannover

Posted 06 March 2018 - 05:05 PM

View PostSilvio Porcellana, on 06 March 2018 - 04:48 AM, said:

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 unsolvedPosted Image


0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users