Set jqGrid edit options via setGridParam()

2019-05-03 23:33发布

问题:

I need to set some event handlers for jqGrid's edit events after the grid has been initialized. Namely, I need to handle the beforeShowForm edit event. I have tried this using setGridParam but it doesn't seem to be doing anything.

$('#mygrid').jqGrid('setGridParam', {
    edit: {
        beforeShowForm: function(formid) {
            // handle event
        }
    }
});

jqGrid's documentation is less than informative as to how these options are supposed to be set. How am I supposed to set these after-the-fact? I am aware that you can set this via the second argument for jqgrid(). I just need to do this after it has been created.

回答1:

You can not so easy to change parameters of edit events because the parameters are saved in internal variable of navGrid function. So you should unbind the click event to the "Edit" button and bind the new one event which call editGridRow method with all new parameters which you need. The new parameters can include event handler like beforeShowForm.

The corresponding code can be about the following:

var grid=$("#list");      // your jqGrid (the <table> element)
var grid_id = grid[0].id; // id of the <table> element like "list"
$("#edit_"+grid_id).unbind('click'); // unbind original 'click' handle
$("#edit_"+grid_id).click(function() {
    if (!$(this).hasClass('ui-state-disabled')) {
        var sr = grid[0].p.selrow;  // get id of selected row
        if (sr) {
            grid.jqGrid("editGridRow",sr,
                        { // here you should place all Edit parameters
                            beforeShowForm: function(formid) {
                                alert("In beforeShowForm()");
                            }
                        });
        } else {
            // display error message
            $.jgrid.viewModal("#alertmod",{gbox:"#gbox_"+grid_id,jqm:true});
            $("#jqg_alrt").focus();
        }
    }
    return false;
});

UPDATED: If you call somewhere editGridRow method directly and can not change the code you can make following

var grid=$("#list");      // your jqGrid (the <table> element)
var orgEditGridRow = grid.jqGrid.editGridRow; // save original function
$.jgrid.extend ({editGridRow : function(rowid, p){
    $.extend(p,
             { // modify some parameters of editGridRow
                 beforeShowForm: function(formid) {
                     alert("In new beforeShowForm()");
                 }
             });
    orgEditGridRow.call (this,rowid, p);
}});


回答2:

I know that this is a little late, but I ran into the same problem. After looking at the jqGrid source, here is what I did:

$.extend($.jgrid.edit, { beforeShowForm: function (frmmgr) {
    alert('insert code here');
}
});


标签: jquery jqgrid