【问题标题】:Select2 after setting initial value, adding an another value changes the text of first valueSelect2 设置初始值后,添加另一个值会更改第一个值的文本
【发布时间】:2016-06-06 05:28:21
【问题描述】:

我正在使用 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)
                          ], 

                    ]);
                ?>

【问题讨论】:

  • 谁能帮我解决这个问题?

标签: php select2 yii2 yii2-widget


【解决方案1】:

终于解决了我的问题。 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 }; }'),
                        ],

                      ], 

                ]);
            ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-03
    • 1970-01-01
    • 2016-10-25
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多