kartik-v/yii2-widget-select2 fail :/

Hello , I am trying to use this extension select2.

Hello , I am trying to use this extension.

but when I try selecionar brought some of the data can not select anything , clicking none are selected.

this is my code :

the view




//view


use kartik\select2\Select2;

use yii\web\JsExpression;

use yii\helpers\Url;

use yii\base\Action;

use yii\helpers\Json;





<?php


     // The controller action that will render the list

$url = \yii\helpers\Url::to(['/ge-tpers/auto/']);




// Script to initialize the selection based on the value of the select2 element

$initScript = <<< SCRIPT

  function (element, callback) {

    var id=\$(element).val();

    if (id!=="") {

        alert('id not null' + id);

      \$.ajax("{$url}&id=" + id, {

        dataType: "json"

      }).done(function(data) { callback(data.results);});

    }

  }

SCRIPT;

 

// The widget

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

    'options' => ['placeholder' => 'Buscar Tercero ...'],

    'pluginOptions' => [

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

        'allowClear' => true,

        'minimumInputLength' => 3,

        'ajax' => [

            'url' => $url,

            'dataType' => 'json',

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

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

        ],

        'initSelection' => new JsExpression($initScript)

    ],

]); 

?>




the controller




//controller


use frontend\models\GeTpers;

use yii\db\Query;

use yii\helpers\Json;


  public function actionAuto($search = null, $id = null) {

    header('Content-type: application/json');

    $out = ['more' => false];

    if (!is_null($search)) {

        $query = new Query;

        $query->select('pers_auxi, pers_nom1 AS text')

            ->from('ge_tpers')

            ->where('pers_nom1 LIKE "%' . $search .'%"')

            ->limit(20);

        $command = $query->createCommand();

        $data = $command->queryAll();

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

    }

    elseif ($id > 0) {

        $out['results'] = ['id' => $id, 'text' => GeTpers::find($id)->pers_nom1];

    }

    else {

        $out['results'] = ['id' => 0, 'text' => 'No matching records found'];

    }

    echo Json::encode($out);

    

}

6396

sasasssa.JPG

i cant select, any idea where this error ????

help pls

i also have a same problem plz help me

HiĆ¹

widget are great, especially kartik2 ones.

But for select2 i found easier to use the jquery library and then modify my Dropdown like this:

In your view.php




  <?= $form->field($model, 'yourvalue')->dropDownList(


			                    ArrayHelper::map( $array, 'id_array', 'fieldname' ),

				                    [ 	'prompt' =>'Select an potion',

				                    	'id'=>'search', 

				                    

				                    ]

			                    ) ?>



In your js




$("#search").select2();



i found the error in the scrip, i change &id for this ?id




$initScript = <<< SCRIPT

  function (element, callback) {

    var id=\$(element).val();

    if (id!=="") {

        alert('id not null' + id);

      \$.ajax("{$url}&id=" + id, {

        dataType: "json"

      }).done(function(data) { callback(data.results);});

    }

  }

SCRIPT;




 \$.ajax("{$url}&id=" + id, {

to


 \$.ajax("{$url}?id=" + id, {