jqgrid - postData submits previous request params and data
Asked Answered
H

3

6

I have been using postData to set the request params dynamically. On click of some external button, I change the grid URL and also set some additional params in postData like below. JqGrid seems to append all these params and it's data for all the subsequent requests. Is there a way we can control or avoid these params being sent every time?

My grid definition:

jQuery(function() {
    $('#grid').jqGrid({
    url: 'rates.html',
    postData: {
        name: function() { return $("#name").val(); },
        rate: function() { return $("#rate").val(); },
                .....
        }
        ....
    });
});

Here in the post request: I see that name, rate params are going along with other standard jqGrid parameters like sortname, sidx, rows, etc...

Now if on click of an external button, if I change the grid URL like below

$('#changeReqBtn').click(function() {
       $('#grid').setGridParam({ url: 'changeReq.html', 
                              postData: { msgIds: msgIds } });
       $('#grid').trigger("reloadGrid");     

});

Now jqGrid sends name, rate, msgIds params

Now if I change the URL back to rates.html say for example, on click of refresh icon, jqGrid sends the previous msgIds param and also the previous values. I don't want to send the previous request params in the new request when the URL changes. Is there a way we can achieve this?

Hairworm answered 17/10, 2012 at 3:53 Comment(0)
D
5

If I correct understand your problem you should avoid usage of setGridParam and do the following instead. You can use $('#grid').jqGrid("getGridParam", "postData") to get the reference to the internal parameter postData. For example,

var myPostData = $('#grid').jqGrid("getGridParam", "postData");

So you can use delete myPostData.msgIds to delete any property of the method previously added by setGridParam.

Distinction answered 17/10, 2012 at 15:24 Comment(5)
Thank you Oleg. Looks like we need to manually remove the object parameter once the reload is done. I was manually resetting the param to null once the reload is done, deletion is a better approach. Thanks..Hairworm
I've tried this but myPostData is always undefined - is there something missing on my side?Blum
@Matt: It's important when and in which context you try to use the code. It's better that you post the code, which you use and which not works like expected, in the new question.Distinction
@Oleg: Yes, you are right. I finally made it work for me, I've posted it here - hopefully it will help someone to save time; jqGrid can really be tricky!Blum
@RangaNirmana: You are welcome! I recommend you to do the following too: var p = $('#grid').jqGrid("getGridParam"); It get you reference to internal object of jqGrid with all internal parameters. You can get any specific parameter like p.postData or p.rowNum and modify it if required. You don't need to use setGridParam anymore.Distinction
B
1

I had the same issue of preserving postdata parameters from previous requests.

I fixed it by clearing postData first and then setting postData with new parameters.

clearing postData => $('#grid1').setGridParam({ postData: ""});

setting postData with new parameters =>

var formValues = {searchVal: "abc", country: "US"} $('#grid1').setGridParam({ postData: formValues});

Basset answered 7/8, 2015 at 4:53 Comment(0)
B
0

I was able to solve it this way:

function triggerRefresh(wsParams) {
    // determine ws path
    var myGrid=$('#myjsTreeView');
    var urlData = "/myWebService.asmx/myWebServiceMethod";
    // note: you need to pass url, datatype, postdata
    var gridParam = { url: urlData, datatype: "json", postData: wsParams, page: 1 };                        
    myGrid.jqGrid('setGridParam', gridParam).trigger("reloadGrid");
} // function

The difference is that I specify datatype, the url and the page altogether in my request - which made it work!

I call this function like so:

var myparams = $.toJSON({ para1: someValue, para2: someOtherValue });
triggerRefresh(myparams);

when I require the data to be updated. Omitting the datatype for example will cause the refresh to fail silently.

Blum answered 15/7, 2013 at 13:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.