postData method not executing function(postData 方法不执行函数)
问题描述
我有两个 jqGrid.在第一个网格中,我选择一行,第二个网格根据第一个网格的 id 刷新数据.至少它应该是这样工作的.
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;
}
在 Chrome 中,我尝试调试函数 getLobid() 但它从未执行过.发送的 postData 请求:{ lobId:null }.
In Chrome I try to debug the function, getLobid() but it is never executed. The postData request sent: { lobId:null }.
如果我将上面的代码更改为 '{ lobId: ' + 1 + ' }' 它可以工作,所以一定有什么错误导致这个函数无法执行.在 Chrome JS 控制台中执行 BudgetCore.getLobId() 工作正常.
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.
推荐答案
你应该使用
postData: {
lobId: function () {
return $(BudgetCore.GridTables.Lob).jqGrid('getGridParam', 'selrow');
}
}
有关详细信息,请参阅答案.
See the answer for more details.
已更新:如果你需要在 serializeGridData
内额外使用 JSON.stringify
那么你不能使用更简单的 <代码>serializeGridData:
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); }
您应该使用我在 答案:
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);
}
这篇关于postData 方法不执行函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!