Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select2 plugin and jquery ui modal dialogs

I am using the Select2 plugin, but the built in search functionality doesn't work when the plugin is used with a jQuery modal dialog. I have a fiddle that shows the problem at...

http://jsfiddle.net/jeljeljel/s3AFx/

Notice the Search box will not accept the focus. There is supposed to be a workaround with the _allowInteraction event (http://api.jqueryui.com/dialog/#method-_allowInteraction), but it isn't working for me.

Can anyone help to see how to make this Fiddle work?

Also, this SO post (select2 plugin works fine when not inside a jquery modal dialog) discusses the same issue, but the suggested fixes are not working for me.

HTML

<div class="dialog">
    <select>
        <option>A tall ship was seen a</option>
        <option>A tall ship was seen b</option>
        <option>A tall ship was seen c</option>
        <option>A tall ship was seen d</option>
        <option>A tall ship was seen e</option>
        <option>A tall ship was seen f</option>
    </select>
</div>

JAVASCRIPT

$('.dialog').dialog({
    modal: true,
    _allowInteraction: function (event) {
        return !!$(event.target).is(".select2-input") || this._super(event);
    }
});
$('select').select2();
like image 250
John Livermore Avatar asked Nov 05 '13 11:11

John Livermore


People also ask

Why Select2 is not working in modal?

Select2 does not function properly when I use it inside a Bootstrap modal. This issue occurs because Bootstrap modals tend to steal focus from other elements outside of the modal. Since by default, Select2 attaches the dropdown menu to the <body> element, it is considered "outside of the modal".

Is Select2 jQuery?

Select2 is a jQuery based replacement for select boxes. It supports searching, remote data sets, and infinite scrolling of results.

Why Select2 is not working?

select2 is not a function" jQuery error occurs for multiple reasons: Forgetting to include the select2 library. Loading the select2 library before the jQuery library. Loading the jQuery library twice.


2 Answers

There's a new version of the fix bigwavesoftware gives for select2 4.0 from the github issue thread about this problem:

if ($.ui && $.ui.dialog && $.ui.dialog.prototype._allowInteraction) {
    var ui_dialog_interaction = $.ui.dialog.prototype._allowInteraction;
    $.ui.dialog.prototype._allowInteraction = function(e) {
        if ($(e.target).closest('.select2-dropdown').length) return true;
        return ui_dialog_interaction.apply(this, arguments);
    };
}

This only needs to be run before any modal dialogs that will have select2 in them are created; you don't need to do anything special inside of the dialog options like in bigwavesoftware's solution.

JSFiddle of this fix in action

like image 81
Gavin.Paolucci.Kleinow Avatar answered Sep 19 '22 21:09

Gavin.Paolucci.Kleinow


I solved this problem by adding this code to JS

$.ui.dialog.prototype._allowInteraction = function(e) {
    return !!$(e.target).closest('.ui-dialog, .ui-datepicker, .select2-drop').length;
};

I found this here https://github.com/ivaynberg/select2/issues/1246#issuecomment-17428249

like image 35
lukasz Avatar answered Sep 22 '22 21:09

lukasz