views:

94

answers:

1

how to send jqGrid data in json format to server? DO I have to use any external library or script to achieve that?

Thanks!

update1: extra licensePlateNumber should not be there

[
    {
        "licensePlateNumber": ""
    },
    {
        "licensePlateNumber": "0000000000000029000721804",
        "sku": "795127",
        "description": "",
        "caseQuantity": "24",
        "isHeld": "false",
        "expirationDate": "Jul 22, 2010 12:00:00 AM"
    },
    {
        "licensePlateNumber": "0000000000000029000722323",
        "sku": "795127",
        "description": "",
        "caseQuantity": "24",
        "isHeld": "false",
        "expirationDate": "Jul 22, 2010 12:00:00 AM"
    },
    {
        "licensePlateNumber": "0000000000000029000722669",
        "sku": "795127",
        "description": "",
        "caseQuantity": "24",
        "isHeld": "false",
        "expirationDate": "Jul 22, 2010 12:00:00 AM"
    }
]
+1  A: 

You approatch from your other question is OK, but jQuery.ajax has problems to serialize arrays. The most reliable and standard way (see here and here as examples) which I see is to serialize all jqGrid data to JSON (for example with respect of JSON.stringify function:

$("#sendButton").click(function(){
    var gridData = jQuery("#list").getRowData();
    var postData = JSON.stringify(gridData);
    alert("JSON serialized jqGrid data:\n" + postData);
    $.ajax({
        type: "POST",
        url: "/cpsb/internalOrderList.do",
        data : {
            jgGridData: postData,
            customData: "bla bla"
        },
        dataType:"json",
        contentType: "application/json; charset=utf-8",
        success: function(response, textStatus, xhr) {
            alert("success");
        },
        error: function(xhr, textStatus, errorThrown) {
            alert("error");
        }
    });
});

the names of parameters jgGridData, customData and so on you can choose how you as like.

Oleg
@Oleg thanks! so in customData i can append the earliest and latest time ? ....
paul
@Oleg I have a quick question.why my JSON is appending null at beggining..update1
paul
@paul: In my experiments I had not such effect. Probably the entry come from the header where exist checkbox used for multiselect (select/deselect all rows). You can test whether the entry exist and if exist remove it: `if (!gridData[0].licensePlateNumber) { gridData.splice(0, 0); }`. The question about `customData` I suppose you solved yourself: you can send any data like `earliest` or `latesttime` which you need. You can either add a new properties to the `gridData` with `gridData.earliest = "my earliest";` or use `data: {jgGridData: postData, earliest: "my earliest", latesttime: 123}`
Oleg
@Oleg thanks! i tried with $("#orderPreview ").find("tr:eq(0)").remove(); and its working now....its a hack but working...
paul
@paul: So will be run also. You should be careful with the manual modification in jqGrid because of possible side effects. You can try to post existence of the row 0 as a bug on http://www.trirand.com/blog/?page_id=393/bugs/. Either Tony confirm that is a bug or he explain why the row is needed. In the last case you should better modify only the data which you need to post and not the jqGrid itself.
Oleg
@tOleg..Thanks! I am checking in my server side code for that as well ...so that don't have to hack it in my side...I have posted a question on which I am adding rows from my first grid to second grid like shopping cart(which you have answered before) ...and I am sending second grid data to server which will convert into persistent data and save in database...so the bug is from adding the data from grid 1 to 2 ...
paul