kartik-v / yii2-widgets

Collection of useful widgets for Yii Framework 2.0
http://demos.krajee.com/widgets
Other
557 stars 177 forks source link

Select2 from Ajax can't selected #383

Open moysoft opened 4 years ago

moysoft commented 4 years ago

Steps to reproduce the issue

  1. In view

    $url = \yii\helpers\Url::to(['/periksa/karyawan-list']);
    ...
    <div class="row">
      <div class="col-md-12">
         <?= $form->field($model, 'nama_terperiksa')->widget(Select2::classname(), [
             'options' => ['multiple' => false, 'placeholder' => 'Search for employee ...'],
              '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(name) { return name.name; }'),
               'templateSelection' => new JsExpression('function (name) { return name.name; }'),
                    ],
                  ]);
    
             ?>
     </div>
    </div>
  2. in Controller :

    public function actionKaryawanList($q = null, $id = null)
    {
        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
    
        $out = ['results' => ['name' => '', 'nik' => '', 'email' => '']];
        if (!is_null($q)) {
    
            $urlTarget = 'https://sso.somesite.com/api/master/getKaryawan/' . $q . '/Th151stH3kEy';
            $response = file_get_contents($urlTarget);
    
            $data  = json_decode($response, TRUE);
            unset($response);
            unset($data["status"]);
            $out['results'] = array_values($data['result']);
    
            // } elseif ($id > 0) {
            //     $out['results'] = ['id' => $id, 'text' => City::find($id)->name];
        }
    
        return $out;
    }

    Expected behavior and actual behavior

When I follow those steps, I see...

https://prnt.sc/rwykv9

I can't select the list, and the list background color is yellow

I was expecting... I can select the list, and the list background color is white

Environment

Browsers

Operating System

Libraries

AbdulRahmanAbouzaid commented 1 month ago

I had the same issue and found that select2 is adding the following class select2-results__option--selectable to options so it can be selected. By reviewing the js code, I found that class maybe removed for some reasons: