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

落花浮王杯 提交于 2019-12-18 16:57:07

问题


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.

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.

I also checked that grid exists, added

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

before the first line, and it shows 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


回答1:


You didn't include the definition of jqGrid in your question and we can't see the place where the setGridParam is called. First of all you should use setGridParam after the jqGrid is created, but before the request will be sent. If you will change the postData the next jqGrid request can use the new parameter. So one typically uses

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

see here.

I suppose that the best option for you will be the usage of function test property of the postData as the function:

$("#@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 here for details. In this way you can implement practically any scenario.

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 setGridParam to change the datatype from 'local' to 'json' (or 'xml') and call .trigger('reloadGrid',...).




回答2:


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



回答3:


Here is the method i used

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


来源:https://stackoverflow.com/questions/6185016/how-to-set-postdata-in-jqgrid-after-it-has-been-constructed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!