我有一个用于大量报告的 wcf 服务,该服务将 json 数据返回到我的 jqgrid。一切都按预期工作。但是,由于每个报告查询都有大量用户输入,我选择使用与我在服务器上设置的一系列“输入模型”相匹配的 json 字符串。我不想在我的 route 弄乱长而复杂的查询字符串。
问题: 如何将 jqGrid 查询字符串参数添加到我正在上传到服务器的 json 字符串中?我试过“loadBeforeSend”,但我似乎无法覆盖 ajax url。我不能为 url 参数使用函数,因为网格参数尚不可用。有任何想法吗?谢谢。
我的 jqGrid 函数(为简洁起见缩短):
function loadGrid() {
var tbl = $('#tbl');
tbl.jqGrid({
loadBeforeSend: function () {
var ai = {
dateFrom: dbDateTime($('#at-datefrom').val()),
dateTo: dbDateTime($('#at-dateto').val()),
sidx: tbl.getGridParam('sortname'),
sord: tbl.getGridParam('sortorder'),
page: tbl.getGridParam('page'),
rows: tbl.getGridParam('rowNum')
};
var newUrl = getBaseURL() + 'ReportingService.svc/report/?json=' + JSON.stringify(ai);
tbl.jqGrid().setGridParam({ url: newUrl });//Everything works perfect up to this point. All the values are in my json string and my url is correct.
},
url: '', //Empty because I need to override it
datatype: 'json',
mtype: 'GET',
ajaxGridOptions: { contentType: 'application/json' },
loadError: function (xhr, status, error) { alert(status + "" + error); }
}).navGrid('#attendance-pager', { edit: false, add: false, del: false });
}
最佳答案
如果您使用 mtype: 'GET'
并且 neew 只是为了设置将附加到 URL 的附加参数,您可以使用 postData
jqGrid 的参数。如果定义 postData
,您将获得最好的结果作为一个函数(详见 here)。
另一种方法是使用 beforeRequest其中 this
将被设置为网格 DOM 元素,您可以访问(并在需要时更改)url
根据 this.p.url
的 jqGrid 参数.您当然可以使用 $(this).jqGrid('setGridParam','url',yourNewUrl);
而不是直接更改 this.p.url
.
我不推荐你使用datatype
作为你的情况的功能。顺便说一下,你不能使用 beforeRequest使用情况datatype
作为函数。
关于javascript - 在 jqGrid 的 loadBeforeSend 上设置 Url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6299829/