tags:

views:

769

answers:

2

I have a JQGrid that's already been initialized. How can I add an event handler to it? I've tried

grid.setGridParam({
    onSelectRow: function(rowid, status) {
        alert("onSelectRow");
    }
});

but this doesn't do anything (no error, but no alert on select either).


Update

Turns out the code above actually works fine - although as @jitter points out the new API syntax is preferred. My problem was that grid was referring to the wrong object. For some reason in the gridComplete event handler, $(this) does not return a reference to the grid, but $("#" + this.id) does.

// handles the gridComplete event
gridInitialized = function() {
    var grid = $("#" + this.id); 
    grid.jqGrid("setGridParam", { onSelectRow: selectRow });
};
+1  A: 

Just add a trigger to reload the grid, like this:

grid.setGridParam({
    onSelectRow: function(rowid, status) {
        alert("onSelectRow");
    }
}).trigger("reloadGrid");

According to the doc's, it should reload the grid, but it doesn't' happen for me if I create the grid, and a bit later add this function.

If I put this code in an onclick handler for a link, it does trigger a reload of the grid.

Thanks - it looks like `trigger("reloadGrid")` actually isn't necessary - see my update above. Also if you put this in the `gridComplete` handler - which is where I'm doing this - it will send you into an endless loop.
Herb Caudill
+4  A: 

The correct way to do this (+ using the new API syntax) is this. Doesn't need a .trigger("reloadGrid")

grid.jqGrid("setGridParam", {
    onSelectRow: function(rowid, status) {
        alert("onSelectRow");
    }
});
jitter
Thanks for pointing the improved syntax - actually it turns out that my code above was fine, but the `grid` variable I was using was referring to the wrong thing.
Herb Caudill