Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

jquery - postData method not executing function

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...