select2 plugin and jquery ui modal dialogs

牧云@^-^@ 提交于 2019-12-03 11:52:57

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

The addition of some code I found at https://github.com/ivaynberg/select2/issues/1246 seems to have fixed the problem. Updated fiddle...

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

JAVASCRIPT

$('.dialog').dialog({
    modal: true,
    open: function () {
        if ($.ui && $.ui.dialog && !$.ui.dialog.prototype._allowInteractionRemapped && $(this).closest(".ui-dialog").length) {
            if ($.ui.dialog.prototype._allowInteraction) {
                $.ui.dialog.prototype._allowInteraction = function (e) {
                    if ($(e.target).closest('.select2-drop').length) return true;
                    return ui_dialog_interaction.apply(this, arguments);
                };
                $.ui.dialog.prototype._allowInteractionRemapped = true;
            }
            else {
                $.error("You must upgrade jQuery UI or else.");
            }
        }
    },
    _allowInteraction: function (event) {
        return !!$(event.target).is(".select2-input") || this._super(event);
    }
});
$('select').select2();

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

THIS WORKED FOR ME:

$("#modal").dialog({
    closeOnEscape: false,   
    resizable: false,
    height: 180,
    maxHeight: 180,
    width: 700,
    maxWidth: 700,
    modal: true,
    autoOpen: false,
    fluid: true,
    open: function () {
        if ($.ui && $.ui.dialog && !$.ui.dialog.prototype._allowInteractionRemapped && $(this).closest(".ui-dialog").length) {
            if ($.ui.dialog.prototype._allowInteraction) {
                $.ui.dialog.prototype._allowInteraction = function (e) {
                    if ($(e.target).closest('.select2-drop').length) return true;

                    if (typeof ui_dialog_interaction!="undefined") {
                        return ui_dialog_interaction.apply(this, arguments);
                    } else {
                        return true;
                    }
                };
                $.ui.dialog.prototype._allowInteractionRemapped = true;
            }
            else {
                $.error("You must upgrade jQuery UI or else.");
            }
        }
    },
    _allowInteraction: function (event) {
        return !!$(e.target).closest('.ui-dialog, .ui-datepicker, .select2-drop').length;
    }
});

None of the above seemed to work for me. I did however change the following in my dialog initialization:

dialog = $( "#my-dialog" ).dialog({
                autoOpen: false,
                width: 440,
                title: 'Test Dialog',
                ...
         });

form = dialog.find( "form" ).on( "submit", function( event ) {
    event.preventDefault();
});

dialog.dialog( "open" );

Basically, I took away the 'modal: true' parameter and it works.

Worked for me anyway :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!