How to trigger event changed kartik select2 in dynamic form?

Hi, i tried to trigger event changed in widget kartik select2, but it cannot work.
please help.
here is my codes.

<?= $form->field($detail, "[{$i}]item_id")->widget(Select2::class, [
       'data' => ArrayHelper::map(\app\models\ItemSparepart::find()->where(['listed' => 1])->all(), 'id', 'name'),
       'language' => 'en',
       'options' => ['placeholder' => 'Select item ...'],
       'pluginOptions' => [
            'allowClear' => true,
        ],
       'pluginEvents' => [
           "change" => "function() { log('change'); }",
       ],
  ]) ?>

I looked into browser’s console, and it was nothing happen.

<?= $form->field($model, 'field')->widget(Select2::class, [
    'data' => ....,
    'options' => [
        'placeholder' => Yii::t('app', 'Select Item'),
        'id' => 'my-id'
    ],
    'pluginOptions' => [
        'allowClear' => true,
    ],
    'pluginEvents' => [
        'select2:select' => 'function(e){ }',
        'select2:unselect' => 'function(e){ }',
    ]
]) ?>
1 Like

I have tried this code, and still not working

'pluginEvents' => [
   'select2:select' => 'function(e){ 
           alert($(this).val());
   }',
 ],

Alert does not get displayed? Is there an error in developer console?

not working. i tried these way and worked

'options' => [
     'placeholder' => 'Select item ...',
     "onChange" => "{
          //alert($(this).val());
          $.get('/item-sparepart/list-stock/id='+$(this).val(), function(data) {
              $('select#models-contact').html(data);
      });
   }"
 ],