Raph
Raph

Reputation: 150

How to reaload (refill) jqGrid data from an ajax reponse (from a Struts 2 action)?

I have this code just to send data from already loaded jqGrid:

jQuery("#bedata").click(function(){ //Function for button "bedata"

 var postData = "SOME DATA TO SEND"

//Sending data:
$.ajax({
    type: "POST";
    url: "GuardaFila.action", //Action called to data treatament (Struts 2)
    data : {
        jgGridData: postData, //PARAMETER jgGrdData with variable "postData" value
        customData: "someinfo" //Just another parameter called "customData" with more data,
    },

    dataType:"json",
    contentType: "application/x-www-form-urlencoded; charset=utf-8",

//Success function has the key that I am looking for:
    success: function(response, textStatus, xhr) {
        //SOME CODE HERE TO REFILL jqGrid.
        alert("success");
     },
    error: function(xhr, textStatus, errorThrown) {
        alert("error");
    }

});
});

jqGrid has been created before this way:

  jQuery("#rowed3").jqGrid({
                url:'CargaTabla.action',
                datatype: "json",
                colNames:['id', 'Direccion', 'Nombre'],
                colModel:[
                    {name:'id',index:'id', width:55},
                    {name:'direccion',index:'direccion', width:90, editable:true},
                    {name:'nombre',index:'nombre', width:100,editable:true}
                ],
                jsonReader: {
                    root: 'gridModel',
                    id: '0',
                    cell :"",
                    repeatitems: false
                },


(....... etc)

So, id for jqGrid table is #rowed3. I know that in the ajax function:

        success: function(response, textStatus, xhr) {
        //SOME CODE HERE TO REFILL jqGrid.
        alert("success");
     },

response parameter has a the new content in JSON for the grid. I tried some ways to refill the grid with its data as setting its "datastr" parameter with its content and others. Has someone faced before this problem?

Thank you.

Upvotes: 1

Views: 2389

Answers (2)

Kat
Kat

Reputation: 36

I think you may be looking for grid.addJSONData method.

For example:

success: function (data, textStatus) {
    if (textStatus == "success") {
       var grid = $("#rowed3")[0];
       grid.addJSONData(JSON.parse(data.d));
   }
},

Keep in mind that you need to make sure your JSON data format matches what is specified in the JSON reader.

Upvotes: 1

Craig Stuntz
Craig Stuntz

Reputation: 126577

Don't reinvent the wheel here. jqGrid already knows how to fetch data, so you don't need to call $.ajax. Just change the grid params and tell it to do its thing (from memory; correct function names if need be, but this will give you the idea):

var opts = { url: "GuardaFila.action", postData: postData };
grid.setGridParam(opts);
grid.trigger("reloadGrid");

Upvotes: 1

Related Questions