Skip to content

Can't type anything if select2 in modal #41

@embambols

Description

@embambols

After recent updates typing wasn't working in any of my select2 widgets that were inside bootstrap modals. Then I found this solution which seems to have worked for me: select2/select2#1436

It was working fine before I did the update on everything and now I don't know what the cause of the problem is (either update of select2 widget or yii2 update), but since that solution is from almost 2 years ago I have a feeling that the problem is somewhere in your widget, because it was updated very recently.

Activity

kartik-v

kartik-v commented on Jun 3, 2015

@kartik-v
Owner

You need to check the demos and docs pages for the widget.

The Select2 widget works perfectly with modal. You need to set the Modal markup correctly for it to work. Check the demo page and scroll down to button for example for Select2 inside a MODAL.

Note the important setting for the Modal dialog... the tabindex must NOT be set and must be set to false as shown.

embambols

embambols commented on Jun 5, 2015

@embambols
Author

Ok. Thanks. It's just that it worked fine before, even without setting 'tabindex' => false. Have quite few places to update now :)

sankam-nikolya

sankam-nikolya commented on May 6, 2016

@sankam-nikolya

thx

elviskudo

elviskudo commented on Jun 11, 2016

@elviskudo

sorry for open this issue again,
now i found that problem again
cannot any typing on this widget in modal
and i see tab-index=-1
how can i solve this problem, thanks

alvarolordelo

alvarolordelo commented on Apr 15, 2017

@alvarolordelo

how to set it required like HTML 5 validation
'required' => 'required' inside options isn't working
it should shown a required pop-up

field($model, 'end_cidade_id')->widget(\kartik\widgets\Select2::classname(), [ 'data' => \yii\helpers\ArrayHelper::map(\app\modulos\estoque\models\EndCidade::find()->where(['end_estado_id' => $model->end_estado_id])->orderBy('name')->asArray()->all(), 'id', 'name'), 'options' => [ 'required' => 'required', 'placeholder' => 'Selecione a cidade' ], 'pluginOptions' => [ 'allowClear' => true ], ]); ?>
kinandkwin

kinandkwin commented on May 9, 2017

@kinandkwin

Can't type anything if select2 in modal when using Firefox, Chrome works fine. Can you help me with this? thank you!

robov

robov commented on Aug 27, 2017

@robov

Although I have it working in a bootstap modal... it works in chrome... but not in firefox...
Any ideas ?
I was so flabbergasted by this that I did not see the previous post...

$.fn.modal.Constructor.prototype.enforceFocus = function() {};

worked for me

dzona

dzona commented on Oct 27, 2017

@dzona

$.fn.modal.Constructor.prototype.enforceFocus = $.noop;

kartik-v

kartik-v commented on Oct 27, 2017

@kartik-v
Owner

You may want to set dropdownParent property of Select2 to the Modal Dialog identifier for addressing this (have not tested this though... for example):

echo Select2::widget([ 
    'pluginOptions' => [
        'dropdownParent' => new yii\web\JsExpression('$("#modalDialogId")'), 
    ],
   // your other select2 widget settings
]);
lieszkol

lieszkol commented on May 29, 2018

@lieszkol

Kartik's last example is the PROPER solution of the three, but it should really be:

echo Select2::widget([ 
    'pluginOptions' => [
        'dropdownParent' => new JsExpression('$("#modalDialogId")'),
    ],
   // your other select2 widget settings
]);

(of course you should substitute modalDialogId with the actual ID of the modal dialog)

Summarizing it all:
The problem is Bootstrap Modal inserting some js that grabs the focus of anything not inside of the modal. The Select2 drop-down is actually rendered outside of the Modal DOM element. There are three ways to fix this:

  1. Make sure that the rendered modal does not have tabIndex="whatever" inside of it. This is the solution employed in Kartik's docs here: ()
Modal::begin([
'options' => [
'id' => 'kartik-modal',
'tabindex' => false, 
...

This is also the "accepted" solution here:
Unfortunately, it is a hack and has some side-effects like breaking the ESC key to exit the modal, and scrolling issues if the modal is very long.

  1. It is also possible to disable the Bootstrap Modal from trying to prevent focus from other elements when it's shown:
// Do this before you initialize any of your modals
$.fn.modal.Constructor.prototype.enforceFocus = function() {};

This is detailed also on the link above as well as here:
But again, this is a hack and has side-effects.

  1. The PROPER solution is to let Select2 know that it should attach itself to the modal (or one of it's children, like the modal body).
    This is also described in the two links above and mentioned above by Kartik.

To implement the proper solution that doesn't break intended bootstrap modal functionality, do what is written at the top of this post!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @dzona@alvarolordelo@elviskudo@kartik-v@robov

        Issue actions

          Can't type anything if select2 in modal · Issue #41 · kartik-v/yii2-widget-select2