kartik-v / yii2-widget-select2

Enhanced Yii2 wrapper for the Select2 jQuery plugin (sub repo split from yii2-widgets).
http://demos.krajee.com/widget-details/select2
Other
323 stars 145 forks source link

Can't display placeholder when using ajax to load options #221

Closed lucadima15 closed 5 years ago

lucadima15 commented 7 years ago

Hi all.

Usually placeholder works fine. But if I also use ajax to load options, placeholder doesn't display anything.

Code totally working:

<?= $form->field($model, 'idCity', [
                                                  'options' => [
                                                                'style' => 'margin-top: 10px;'
                                                               ],
                                                 ])->widget(Select2::classname(), [
                                                       'data'          => $cities,
                                                       'options'       => ['placeholder' => 'Select city ...'],
                                                       'pluginOptions' => [
                                                                           'allowClear' => true
                                                                          ]
                                                      ]);?>

Code working in ajax loading, but not displaying placeholder:

<?= $form->field($model, 'idCity', [
                                            'options' => [
                                                          'style' => 'margin-top: 10px;'
                                                         ],
                                           ])
             ->widget(Select2::classname(), [
                                             'options'       => ['placeholder' => 'Select city ...'],
                                             'pluginOptions' => [
                                                                 'allowClear' => true,
                                                                 'minimumInputLength' => 3,
                                                                 'ajax' => [
                                                                            'url'      => Url::to(['city/search']),
                                                                            'dataType' => 'json',
                                                                            'data'     => new JsExpression("function(params) { return {q:params.term} }"),
                                                                           ],
                                                                 'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
                                                                 'templateResult' => new JsExpression('function(city) { return city.name; }'),
                                                                 'templateSelection' => new JsExpression('function (city) { return city.name; }'),
                                                                ]
                                            ]);
    ?>
stale[bot] commented 5 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.