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

3

10

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

Titus answered 31/5, 2011 at 8:43 Comment(0)
S
12

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',...).

Swart answered 31/5, 2011 at 9:47 Comment(4)
thanks for the test:function() part, that will be helpfull, otherwise, I have already implemented everything according to the order you wrote. It works now, but it was not at the first, and I don't know what have I changed :)Titus
@obrad: You don't posted your code which you currently use. If something not works "at the first" you should examine what code exactly works "at the first". I supposed that you could have such kind of problems and included in my answer the part with the usage of datatype with the 'local' initially which will prevent the first grid loading. After the value which you need to use inside of test:function() will be known you can change the datatype to 'json' and force grid loading with .trigger('reloadGrid',...).Swart
I did not posted it because it is generated, but suppose I should have posted generated code. However, it works now. Thank you for your help.Titus
@obrad: You are welcome! I prefer to have an code example, because jqGrid have too much options and many "unimportant" options which will be not included in the question could require to rewrite the solution. For example grouping, treegrids, subgrids, xml or json, local or remote data or a mix with respect of loadonce:true, 'multiselect:true', different versions of editing mode (cell, inline and form editing) and so on. So one have to guess or ask many additional question. In any way your problem is solved and I am glad to read about this.Swart
O
4
$("#grid id").setGridParam({ postData: {data:dataval} });
$("#grid id").trigger('reloadGrid', [{page:1,data:dataval}]);ntn
Ouzo answered 20/12, 2012 at 8:3 Comment(0)
A
1

Here is the method i used

postData: { 'testKey': function () { return 'testvals'; } },
Ablebodied answered 5/10, 2013 at 18:45 Comment(1)
Wrapping a global var to return from a function like that worked perfect so that the trigger reloadGrid pulled the current value of the var and not the value it had when the grid was first created. tks. So if I had var testvals, then the postdata would be like so: postData: { 'testKey': function () { return testvals ; } },Addle

© 2022 - 2024 — McMap. All rights reserved.