how to set postData in jqgrid AFTER it has been co

2019-02-09 19:02发布

问题:

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'; } },