jQuery 构建后如何在jqgrid中设置postData?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6185016/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 20:31:17  来源:igfitidea点击:

how to set postData in jqgrid AFTER it has been constructed?

jqueryjqgridjqgrid-asp.net

提问by Goran Obradovic

I generate my jqgrid from model class which I pass into view. I get constructed and working jqgrid. However, I want to set postData on one view, where I use jqGrid, from script in that view, after I call helper for creating jqgrid, without having to change whole partial view which creates jqgrid.

我从传递到视图中的模型类生成我的 jqgrid。我得到构建和工作 jqgrid。但是,我想在我使用 jqGrid 的一个视图上设置 postData,在我调用帮助程序创建 jqgrid 之后,从该视图的脚本中使用 jqGrid,而不必更改创建 jqgrid 的整个局部视图。

I tried running

我试过跑步

$("#@Model.Id").jqGrid('setGridParam', { postData: { test: 233} });

and

$("#@Model.Id").setGridParam({ postData: { test: 233} });

but without error or any result. If I set postData in jqgrid parameters (in partial view where it is constructed, it works.

但没有错误或任何结果。如果我在 jqgrid 参数中设置 postData(在构造它的局部视图中,它可以工作。

I also checked that grid exists, added

我还检查了网格是否存在,添加

console.log($("#@Model.Id").size());

before the first line, and it shows 1.

在第一行之前,它显示 1。

UPDATE: This .setGirdParam function started to work for me for no apparent reason, so I will accept answer if someone can give some insight what can prevent this from working. Thanks

更新:这个 .setGirdParam 函数开始为我工作,没有明显的原因,所以如果有人能给出一些可以阻止它工作的见解,我会接受答案。谢谢

回答by Oleg

You didn't include the definition of jqGrid in your question and we can't see the placewhere the setGridParamis called. First of all you should use setGridParamafterthe jqGrid is created, but beforethe request will be sent. If you will change the postDatathe nextjqGrid request can use the new parameter. So one typically uses

你不包括在你的问题中的jqGrid的定义,我们不能看到的地方setGridParam被调用。首先,您应该setGridParam创建 jqGrid之后,但发送请求之前使用。如果您将更改postData下一个jqGrid 请求可以使用新参数。所以通常使用

$("#@Model.Id").trigger('reloadGrid', [{page:1}]);

see here.

看到这里

I suppose that the best option for you will be the usage of function testproperty of the postDataas the function:

我想对你来说最好的选择是使用函数的函数test属性postData作为函数:

$("#@Model.Id").jqGrid({
    // ... other jqGrid parameters ...
    postData: {
        test: function() {
            // the code can by dynamic, read contain of some elements 
            // on the page use "if"s and so on and return the value which 
            // should be posted to the server
            return 233;
        }
    }
    // other jqGrid parameters ...
});

See herefor details. In this way you can implement practically anyscenario.

有关详细信息,请参见此处。通过这种方式,您几乎可以实现任何场景。

By the way if you don't want that jqGrid to send any request to the server till the event is happened you can use datatype:'local'at the initialization time. Then if you want that the grid should be filled you can use setGridParamto change the datatypefrom 'local'to 'json'(or 'xml') and call .trigger('reloadGrid',...).

顺便说一句,如果您不希望 jqGrid 在事件发生之前向服务器发送任何请求,您可以datatype:'local'在初始化时使用。然后,如果您希望填充网格,您可以使用setGridParamdatatypefrom更改'local''json'(或'xml')并调用.trigger('reloadGrid',...).

回答by user1761013

$("#grid id").setGridParam({ postData: {data:dataval} });
$("#grid id").trigger('reloadGrid', [{page:1,data:dataval}]);ntn

回答by user2675617

Here is the method i used

这是我使用的方法

postData: { 'testKey': function () { return 'testvals'; } },