dpntq48842 2016-06-06 05:28
浏览 201
已采纳

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

I'm rendering select2 using karthik yii2 widget. I'm using the updated version. I tried to set initial value using initSelection, it works fine. But when i try to add an another value it changes the pre selected value to ID which was set through initSelection.

<?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)
                          ], 

                    ]);
                ?>
  • 写回答

1条回答 默认 最新

  • douban5644 2016-06-14 06:36
    关注

    Finally i resolved my issue. initselection is deprecated in select2 4.0 . i tried to set the initial value using "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 }; }'),
                            ],
    
                          ], 
    
                    ]);
                ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable