1

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." );
    });
}
user3229474
  • 27
  • 2
  • 7

1 Answers1

0

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.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thanks for the answer. The problem is, this event only fires when i click on the close button, but not when i click anywhere else with mouse and the dialog closes. I am now showing the error in a div. – user3229474 Feb 07 '14 at 14:19
  • Thanks for your work you invest to help me! When the info_dialog is opening the first time, the closing is work like a charm. When the info_dialog opens a second time, i am unable to close it. The alerts are coming, but the window is not closing. I am getting 762 alerts. (Changed alert to console.log to get the number). – user3229474 Feb 07 '14 at 17:59
  • @user3229474: I appended my answer with **UPDATED 2** part. – Oleg Feb 07 '14 at 18:32
  • I have now added the $.extend function as normal javscript code and only the $.jqgrid.info_dialog to the errorcell event. I think it is working now! Before i had both functions in the errorcell event. Thank you for your help in this question and also for answering all the questions of the other people, it helped me a lot! – user3229474 Feb 07 '14 at 19:13