1

I have two jqGrids. In the first grid I select a row and the second grid refreshes with data based on the id of the first grid. At least that is how it is supposed to work.

//This is code from the second grid
postData: '{ lobId: ' + BudgetCore.getLobId() + ' }',

//Snippet from BudgetCore...
getLobId: function () {
    var row = jQuery(BudgetCore.GridTables.Lob).jqGrid('getGridParam', 'selrow');
    return row;
}

In Chrome I try to debug the function, getLobid() but it is never executed. The postData request sent: { lobId:null }.

If I change the code above to '{ lobId: ' + 1 + ' }' it works, so there must be something wrong that is causing this function not to execute. In the Chrome JS console executing BudgetCore.getLobId() works fine.

Dan
  • 79
  • 1
  • 2
  • 13

1 Answers1

2

You should use

postData: {
    lobId: function () {
        return $(BudgetCore.GridTables.Lob).jqGrid('getGridParam', 'selrow');
    }
}

See the answer for more details.

UPDATED: If you need to use JSON.stringify additionally inside of serializeGridData then you can't use more the simplest version of serializeGridData:

serializeGridData: function (postData) { return return JSON.stringify(postData); }

Instead of that you should use a little more complex version of serializeGridData which I described in the answer:

serializeGridData: function (postData) {
    var propertyName, propertyValue, dataToSend = {};
    for (propertyName in postData) {
        if (postData.hasOwnProperty(propertyName)) {
            propertyValue = postData[propertyName];
            if ($.isFunction(propertyValue)) {
                dataToSend[propertyName] = propertyValue(); // call the function
            } else {
                dataToSend[propertyName] = propertyValue;
            }
        }
    }
    return JSON.stringify(dataToSend);
}
Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • I tried this, but ASP.NET asmx requires the post data be surrounded by ' '. I should have mentioned this in the question. – Dan Oct 31 '12 at 16:25
  • @Dan: Sorry, but you are wrong. ASMX accept `"` which are the only charachers allowed for quoting in JSON. If you need to send the information to ASMX you use `serializeGridData` with call of `JSON.stringify` and it's the origin **why** functions can't be used. In the case you need just use modified version of `serializeGridData`. See [the answer](http://stackoverflow.com/a/6296601/315935) for the corresponding code. – Oleg Oct 31 '12 at 16:37
  • I will try this, just note that in this same grid (the second one) I have a subgrid with postData: '{ lobId: ' + BudgetCore.getLobId() + ' }', and it works fine. – Dan Oct 31 '12 at 16:40
  • @Dan: The difference is that you **create** new subgrid everytime. So the code `postData: '{ lobId: ' + BudgetCore.getLobId() + ' }'` will be executed **multiple** times. By the way the correct code will be at least `postData: JSON.stringify({lobId: BudgetCore.getLobId()})`. On the other side you create grid **once**. So the code `postData: '{ lobId: ' + BudgetCore.getLobId() + ' }'` will be executed once during **creating time** of jqGrid. All next requests just uses the same string value of `postData`. – Oleg Oct 31 '12 at 16:44