jqgrid info dialog function onClose

2019-03-01 05:59发布

问题:

i am showing the server error message with an info_dialog. I would like to fire a function, when the info_dialog ist getting closed. I have tried to do it with a mouse click, but it only fires after the dialog is already closed

first mouseclick: dialog closes, but alert is not fired

second and every following mouseclick: alert is fired.

I am using celledit. Anyone with an idea how i can fire a function, when the dialog ist getting closed? Thanks for your help.

errorCell:  function(serverresponse, status) {

    $.jgrid.info_dialog(
    $.jgrid.errors.errcap,
    serverresponse.responseText,
    $.jgrid.edit.bClose,
    { zIndex: 1500}
    );


    $(document).click(function() {
    alert( "Handler for .click() called." );
    });
}

回答1:

The method $.jgrid.info_dialog supports onClose callback which will be called on closing. The return value from the callback informs whether the closing is permitted. Just try the code

$.jgrid.info_dialog(
    $.jgrid.errors.errcap,
    serverresponse.responseText,
    $.jgrid.edit.bClose,
    {
        zIndex: 1500,
        onClose: function () {
            alert("inside onClose");
            return true; // allow closing
        }
    }
);

UPDATED: To catch closing of $.jgrid.info_dialog in case of clicking with the mouse outside of the dialog one have to do more complex trick.

var orgViewModal = $.jgrid.viewModal;
$.extend($.jgrid,{
    viewModal: function (selector, options) {
        if (options.onHide) {
            options.orgOnHide = options.onHide;
            options.onHide = function (h) {
                alert("inside onHide");
                return options.orgOnHide.call(this, h);
            }
        }
        return orgViewModal.call (this, selector, options);
    }
});

$.jgrid.info_dialog($.jgrid.errors.errcap, "Test message",$.jgrid.edit.bClose, {
    zIndex: 1500,
    onClose: function () {
        alert("inside onClose");
        return true; // allow closing
    }
});

In the first part of the code I use "subclassing" of $.jgrid.viewModal method (like I used in the answer, this one and some other). So I forward all calls to original $.jgrid.viewModal method with one exception. If $.jgrid.viewModal method are called with onHide callback parameter I forward to original $.jgrid.viewModal method modified implementation of the callback. It allows to catch closing of the dialog.

UPDATED 2: The demo shows the approach live.

Alternatively (instead of subclassing) you can just modify the lines

onHide: function(h) {
    h.w.hide().remove();
    if(h.o) { h.o.remove(); }
},

of info_dialog in jquery.jqGrid.src.js. You need just insert additional call of mopt.onClose if the option defined. Probably one should include additional callback onClosed because onClose can deny closing, but new callback called inside of onHide can't do this.



标签: jqgrid