0

I want to send row data to be updated but when the jqgrid was created the datatype set to "Local". I'm trying to set the dataType : "json" in onSubmiClick and want to send the row data (data to be posted to REST method). Here is my JqGrid code block. Any help is appreciated.

var editSettings = {
recreateForm:true,
jqModal:false,
reloadAfterSubmit:true,
closeOnEscape:true,
closeAfterEdit:true,
addCaption: "Edit Scripts",
onclickSubmit: function(params,postdata) {
    $.ajax({ 
    type : "POST",
    url : "/fnol-maintenance/reports/update",
    contentType : "application/json; charset=utf-8",
    data : {
         jqGridData : postdata    //I'm trying to figure out how to pass the row data being edited using jQgrid Form Edit.
       },
    dataType : "json",
    asynch : false
    });
    return{};
}};

myGrid = $("#mygrid").jqGrid({
caption: "FNOL Maintenance Report",
datatype: "local",
colNames: myColNames,
colModel: myColModel,
pager: '#mypager',
//rowNum: 10000,
rowList: [10, 20, 50, 100],
viewrecords: true,
autowidth: true,
gridview: true,
ignorecase: true,
altRows: true,
altclass: 'myAltRowClass',
height: gridHeight, //commented to use browser vertical scrollbar
//height: "100%",   //un-commented to use browser vertical scrollbar
loadtext: "Loading data...",
//forceFit: true
//headertitles: true,
footerrow: true,
userDataOnFooter: true,
editUrl: '/fnol-maintenance/reports/update'
});
myGrid.jqGrid('navGrid','#mypager',{edit:true,add:false,del:false,search:false}, editSettings );
jQuery("#mygrid").jqGrid('setCaption', (myjsongrid.reportTitle==""?'Maintenance Report':myjsongrid.reportTitle));
myGrid.setGridParam({datatype: "local"});
myGrid.setGridParam({data: mydata}).trigger("reloadGrid");
//jQuery("#mygrid").jqGrid('navGrid','#mypager',{del:false,add:false,edit:true,search:false});
//alert("after ajax");
Arvind
  • 53
  • 1
  • 9
  • How about before the ajax call in `onclickSubmit()` you call the line `myGrid.setGridParam({datatype: "json"});` and then use your ajax call. – Ajo Koshy Mar 28 '13 at 05:12

1 Answers1

2

The code which you posted is very strange. First of all you use undefined variable editSettings. The value will be assigned to editSettings later. One should not wonder that callback onclickSubmit will be not called.

In the same way I don't see where you defined myGrid, mydata etc. Is it real code which you use?

By the way I see no sense to create jqGrid without specifying of data parameter and then to use setGridParam to set datatype to "local" (it was already "local"), change data parameter and reload grid. One can just use data: mydata parameter directly during creating the grid.

I don't see any sense to use $.ajax inside of onclickSubmit to post the data manually to the server because jqGrid do the same internally. If you need some custom serialization of data which will be sent to the server you can use serializeEditData callback. If you need just set some additional options of $.ajax (like contentType) you can use ajaxEditOptions option.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • EditSettings is defined above the grid but when I posted the question I have put them in wrong order. My mistake. – Arvind Mar 28 '13 at 14:00
  • I have set the datatype as local when I define the grid as well as just before reloading the grid using – Arvind Mar 28 '13 at 14:02
  • @Arvind: Could you correct the code which you posted? Do you understand my suggestion about usage `ajaxEditOptions` and optionally `serializeEditData`? – Oleg Mar 28 '13 at 14:12
  • I have set the datatype as local when I define the grid as well as just before reloading the grid using `code`myGrid.setGridParam({datatype: "local"}); My understanding is this datatype setting is for the grid data. when I open a form to edit a row I get the error "Url is not Set" if I don't define the $.ajax within editSettings. I don't want to pass any custom data but I want to pass the actual row data I'm editing to server as JSON string. Unfortunately I could not get this work. How do I set datatype: "json" for only form data and the server URL. – Arvind Mar 28 '13 at 14:36
  • That worked, Thanks!!! But now I get an error "error Status: 'Unsupported Media Type'. Error code: 415". How do I define the data I'm sending to the server is "json"? I want to define only the form values being sent to server as "json" and not the entire grid because in the grid the datatype is "local" already. – Arvind Mar 28 '13 at 15:40
  • My server controller consumes JSON so I need to send JSON but in this case by defining datatype as "local" for grid, the firebug console show the error "415 Unsupported Media Type" becuase parameters are passed as "application/x-www-form-urlencoded". How to resolve this. Any help is appreciated. – Arvind Mar 28 '13 at 16:14
  • 1
    @Arvind: Do you used `ajaxEditOptions` options like I suggested? You should set it as part of `editSettings`. For example `var editSettings = {recreateForm:true, ajaxEditOptions: { contentType: "application/json" }, serializeEditData: function (postdata) { return JSON.stringify(postdata); }, ....};`. You can modify code of `serializeEditData` to send `jqGridData: JSON.stringify(postdata)` or `JSON.stringify(jqGridData: postdata)` or some other. You can set default options by modifying `$.jgrid.edit` see [here](http://stackoverflow.com/a/3918615/315935). – Oleg Mar 28 '13 at 16:41