0

When submitting the jqGrid form, I want to append '/theRecordId' to the URL, REST style, for the edit use case. This is not working:

jQuery("#noteList").jqGrid('navGrid','#pager',
    {addtitle:"Add New Note", clearAfterAdd: true},
    {addCaption:"Edit Note", mtype:"PUT"
        ,onclickSubmit: function(rp_ge, postdata) {
            rp_ge.url = editurl + '/' + postdata.id;  }
     },

    {addCaption:"Add New Note", mtype:"POST"}, 
    {}, 
    {}, 
    {}
  );

This results in "/[myurl]/undefined" being sent to the server (where [myurl] is the correct initial editurl). So the dynamic url manipulation seems to work, but 'id' is undefined. What should be the name of the 'id' property? I can see that what is being passed as a form parameter is 'id'. I also tried noteId, which is the name it has in the incoming data when displaying the grid, and also what I used for jsonReader.id. Didn't work either. Where should this attribute name come from?

Pablo
  • 167
  • 1
  • 2
  • 13

1 Answers1

2

Probably you used an example created for old version of jqGrid. The current version of jqGrid uses {gridid}_id as the property of id value. For example if the id of the grid which you use is noteList then noteList_id is the name of id property of postdata parameter of onclickSubmit. It's important to understand that later, after calling of onclickSubmit and beforeSubmit, the name of property will be changed from noteList_id to id (see the part of code).

You can fix the code of onclickSubmit to the following

onclickSubmit: function (options, postdata) {
    options.url = editurl + '/' + encodeURIComponent(postdata[this.id + "_id"]);
}

I included the call of JavaScript function encodeURIComponent to be sure that URL will be correctly encoded in case of any characters used in id.

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thank you so much Oleg, it worked like a charm! jqGrid is really an amazingly powerful and versatile framework but also it's hard to learn! I hope you're coming up soon with a book :) – Pablo Jun 23 '13 at 12:54
  • Unfortunately I still have the same problem with the DELETE use case. This returns /undefined: {mtype:"DELETE", onclickSubmit: function (options, postdata) { options.url = editurl + '/' + encodeURIComponent(postdata[this.id + "_id"]); }} – Pablo Jun 23 '13 at 13:22
  • @user2309409: format of `postdata` of `onclickSubmit` of Delete form is very simple: it's just `id` value in case of single selection and is comma separated ids of selected rows if you use `multiselect: true`. So if you don't use `multiselect: true` option of jqGrid then you can use `options.url = editurl + '/' + encodeURIComponent(postdata);`. I recommend additionally use `serializeDelData: function () { return ""; }`. See [the answer](http://stackoverflow.com/a/7365228/315935) – Oleg Jun 24 '13 at 05:14