views:

218

answers:

3

I'm new to javascript, jQuery and jqGrid, so this is probably a pretty basic question.

Why does the event fire correctly here, triggering the alert:

        $("#list").jqGrid({loadComplete: alert('load complete')
            });

but not here when the alert is inside a function?

        $("#list").jqGrid({loadComplete:
                               function() {
                                    alert('load complete');
                               }
            });

I'm guessing that there's something pretty basic that I don't (yet) understand.

Thanks. --Jeff

+1  A: 

It takes a function, when you pass it the alert itself, you're calling the alert right then and trying to assign the result to the handler. Instead you need an actual handler (an anonymous function in the case of your second call).

It's not that the first is firing correctly on the event, it's actually firing as soon as that line of code runs, it sounds like your code isn't firing the loadComplete handler at all, which is a separate issue with jqGrid.

Nick Craver
so, the 2nd version is actually the correct form, and there's a jqGrid issue with the event?
jalperin
@jalperin - It looks like that event's not firing, yes, do you have an example page?
Nick Craver
I'll set one up in a few minutes.
jalperin
http://ccclients.com/TEST/TEST.php
jalperin
A: 

I tried to do the same thing recently (same position as you, essentially new to everything), loadComplete never worked for me. Investigating now, now that I understand it a bit more:

Looking at the source, it appears loadComplete only fires if your datatype is "script" or "xmlstring", which makes it pretty useless. And strange.

If you're trying to use it to do something before data is entered into the grid, I'd recommend making your datatype a function, and performing your own ajax / whatever operations in there. When the data comes back, just do grid.addData(data), and it'll populate the grid.

If you're trying to do something after the data has been entered, use gridComplete instead, it fires reliably.

Groxx
Sorry, but I strictly disagree with you. I included the detailed comment on your answer in my answer. The usage of datatype a function for loading standard datatypes come from the time before jqGrid 3.6 was released. Events like `serializeGridData` allow you to solve only the problem of data conversions without changing of the standard way of all operation during requesting of data.
Oleg
@Oleg: I'll keep that one in mind, thanks! I too have never had loadComplete fire for me however, despite gridComplete working reliably at all times - do you have an explanation for that?
Groxx
If you post the code example which not work or post a url, where I can see an example which is not work, I'll try find the reason and a way to fix the problem.
Oleg
+2  A: 

After you publish the link http://ccclients.com/TEST/TEST.php I understand what error you make. You should not separate jqGrid definition in two calls:

jQuery("#list").jqGrid({
    datatype: 'xml',
    mtype: 'GET',
    loadonce: true,
    // other parameters
    caption: 'My first grid', 
    xmlReader: { 
        root: "export", 
        row: "row",
        repeatitems: false
    }
 });

and

$("#list").jqGrid({loadComplete: 
            function() {
                alert('load complete')
            }
});

but define loadComplete as a part of one call of $("#list").jqGrid({ ... }); like following:

jQuery("#list").jqGrid({
    datatype: 'xml',
    mtype: 'GET',
    loadonce: true,
    // other parameters
    caption: 'My first grid', 
    xmlReader: { 
        root: "export", 
        row: "row",
        repeatitems: false
    },
    loadComplete: function(data) {
        alert('load complete');
    }
 });

If you do have to set an event handler later you should use setGridParam method (See http://stackoverflow.com/questions/3148320/add-an-event-handler-to-jqgrid-after-instantiation/3149534#3149534)

Moreover I strictly disagree with the answer of Groxx. The function loadComplete will be called for all datatypes (inclusive 'xml', 'json', 'local' and so on). How you can see in the documentation under http://www.trirand.com/jqgridwiki/doku.php?id=wiki:events#execution_order the function loadComplete is the perfect place to make some modification in the grid after the data are loader (or refreshed). I use this function permanently. The usage of datatype a function is a last ways if you need to load very exotic data (neither xml nor json etc). For loading of xml and json data there are a lot of customization features in jqGrid (see http://stackoverflow.com/questions/2675625/setting-the-content-type-of-requests-performed-by-jquery-jqgrid/2678731#2678731 for example). So you can customize the jQurey.ajax call and convert the data used as input and output of jQurey.ajax practically like you want.

Oleg
Thanks. This is very helpful and answers my specific question, but still leaves me with a more general question: How do I "pre-process" an xml or json server response BEFORE the grid is drawn since loadComplete doesn't seem to do that. For example, if the ajax response from the server contains a custom error message, I'd like to display the error message instead of the grid
jalperin
I set up a separate question for this here: http://stackoverflow.com/questions/3522165/how-to-pre-process-an-ajax-response-before-displaying-in-jqgrid
jalperin