本文介绍了在jqGrid中的loadBeforeSend上设置URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有大量报告的wcf服务,它将json数据返回给我的jqgrid。一切都按预期工作。但是,由于每个报表查询的用户输入数量很多,我选择使用与我在服务器上设置的系列输入模型匹配的json字符串。我不想在我的路由中弄乱冗长复杂的查询字符串。

I have a wcf service for a large number of reports that returns json data to my jqgrid. Everything works as expected. However, due to the large number of user inputs for each report query I have elected to use a json string that matches a series 'input models' I have setup on the server. I don't want to mess with long complicated query strings in my routes.

问题:如何添加jqGrid查询字符串params do我的json字符串我上传到服务器?我试过'loadBeforeSend',但我似乎无法覆盖ajax网址。我无法使用url参数的函数,因为网格参数尚不可用。有任何想法吗?谢谢。

Question: How can I add the jqGrid query string params do my json string I am uploading to the server? I have tried 'loadBeforeSend' but I can't seem to override the ajax url. I can't use a function for the url parameter because the grid params are not available yet. Any ideas? Thanks.

我的jqGrid函数(为简洁而缩短):

My jqGrid function (shortened for brevity):

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的附加参数,你可以使用jqGrid的 postData 参数。如果将 postData 定义为函数,则会获得最佳结果(请参阅了解详情。)

If you use mtype: 'GET' and neew just to set additional parameters which will be appended to URL you can use postData parameter of jqGrid. The best results you will have if define the postData as a function (see here for details).

还有一种方法是使用 将设置为网格DOM元素,您可以访问(并根据需要更改)jqGrid的 url 参数每 this.p.url 。您当然可以使用 $(this).jqGrid('setGridParam','url',yourNewUrl); 而不是直接更改 this.p .url

One more way is to use beforeRequest where this will be set to the grid DOM element and you can access (and change if needed) the url parameter of jqGrid per this.p.url. You can of course use $(this).jqGrid('setGridParam','url',yourNewUrl); instead of direct changing of this.p.url.

我不建议你使用 datatype 作为函数你的情况。顺便说一下,你不能使用使用情况 datatype 作为函数。

I don't recommend you to use datatype as function in your case. By the way you can't use beforeRequest in case of usage datatype as function.

这篇关于在jqGrid中的loadBeforeSend上设置URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 08:22