kartik\select2 Load properly selected itens AJAX

Hi guys again,

I 've tried and searched on forum and demos on kartik’s site. I’m using kartik-v/yii2-widget-select2 with multiple true, ajax and activeform with model. Probably it’s must be a problem with javascript.

It can’t load properly itens’ data from the model when already there are itens on select2 field form. Apparently it execute ‘templateSelection’ after form load with initial values, so i think that i’m formating the data wrongly, because appears “undefined (undefined)”.

Insert new itens are ok, the problem is when there are itens to load initially.

Follows my codes:

Form Image

6676

Capturar.PNG

Controller





    public function actionListaDisciplinas($q = null, $id = null)

    {

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


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


        if ( !is_null($q) )

        {

            $out['results'] = array();

            foreach ( Curso::find()->all() as $curso )

            {

                array_push( $out['results'], [

                    'nome' => $curso->nome,

                    'children' => Disciplina::find()

                        ->select(['disciplinas.id', 'disciplinas.nome', 'cursos.sigla', 'disciplinas.id_curso'])

                        ->innerJoinWith(['curso'], false)

                        ->where(['id_curso' => $curso->id])->andWhere(['like', 'disciplinas.nome', $q])->andWhere(['disciplinas.habilitado' => 1])

                        ->orderBy('nome')

                        ->asArray()

                        ->all()

                ]);

            }

        }

        else if ( $id > 0 )

        {

            $out['results'] = ['id' => $id, 'nome' => Disciplina::find($id)->nome];

        }


        return $out;

    }



View




    <?php

    $model->_disciplinas = $model->disciplinas; //

    $nomes = ArrayHelper::getColumn( $model->disciplinas, 'nome' );

    $url = Url::to(['lista-disciplinas']);


    echo $form->field($model, '_disciplinas')->widget( Select2::classname(), [

        'initValueText' => $nomes,

        'options' => [

            'placeholder' => 'Pesquise por disciplinas...',

            'multiple' => true,

        ],

        'pluginOptions' => [

            'multiple' => true,

            'allowClear' => false,

            'minimumInputLength' => 1,

            'ajax' => [

                'url' => $url,

                'dataType' => 'json',

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

                'results' => new JsExpression('function(data,page) { return {results:data.results};}')

            ],

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

            'templateResult' => new JsExpression('function(disc) { return disc.nome; }'),

            'templateSelection' => new JsExpression('function(disc) { return disc.nome + \' (\' + disc.sigla + \')\'; }'),

        ],

    ]);

    ?>