设置初始值后选择2,添加另一个值更改第一个值的文本

Select2 after setting initial value, adding an another value changes the text of first value

我正在使用 karthik yii2 widget 渲染 select2。我正在使用更新版本。 我尝试使用 initSelection 设置初始值,它工作正常。但是当我尝试添加另一个值时,它会将预选值更改为通过 initSelection 设置的 ID。

<?php
$initScript = <<< SCRIPT
function (element, callback) { 
  var id = $(element).val();
  if (id !== "") {
    var url = "{$url}";
    $.ajax(url.replace('idreplace', id), {dataType: "json"}).done(
      function(data) {
        callback(data.results);
      });
  }
}
SCRIPT;
 echo Select2::widget([
                          'language' => 'en',
                          'name' => 'to',
                           'value' => ['1'],
                          'options' => [
                            'placeholder' => 'Choose...',
                            'id' => "to",
                            'multiple' => true,
                            'data-validation'=>'required',
                          ],
                          'pluginOptions' => [
                                      'language' => [
                                         'errorLoading' => new JsExpression("function () { return 'Waiting for results...'; }"),
                                                    ],
                            'allowClear' => true,
                            'minimumInputLength' => 3,
                            'multiple' => true,
                            'ajax' => [
                              'url' => $url,
                              'dataType' => 'json',
                              'tags' => true,
                              'data' => new JsExpression('function(params) { return {search:params.term}; }'),
                              'results' => new JsExpression('function(data,page) {return { results:data.results }; }'),
                            ],
                            'initSelection' => new JsExpression($initScript)
                          ], 

                    ]);
                ?>

我终于解决了我的问题。 initselection 在 select2 4.0 中被弃用。我尝试使用 "initValueText"

设置初始值
 echo Select2::widget([
                      'language' => 'en',
                      'initValueText' => ['intialvaluetext'],//text of initial value
                      'name' => 'to',
                       'value' => ['1'],//matching id for initial value text
                      'options' => [
                        'placeholder' => 'Choose...',
                        'id' => "to",
                        'multiple' => true,
                        'data-validation'=>'required',
                      ],
                      'pluginOptions' => [
                                  'language' => [
                                     'errorLoading' => new JsExpression("function () { return 'Waiting for results...'; }"),
                                                ],
                        'allowClear' => true,
                        'minimumInputLength' => 3,
                        'multiple' => true,
                        'ajax' => [
                          'url' => $url,
                          'dataType' => 'json',
                          'tags' => true,
                          'data' => new JsExpression('function(params) { return {search:params.term}; }'),
                          'results' => new JsExpression('function(data,page) {return { results:data.results }; }'),
                        ],

                      ], 

                ]);
            ?>