JQgrid placing user data at boot

Surprisingly, someone used jQgrid to publish dynamic data from another form on the same page. The dynamic one is that I don’t know the input names for the publication, but would rather like to publish the whole serialized form when rendering the grid.

I got to setting extra data in postData, but it doesn’t get into the URL correctly, since it looks like double url-encoded. Cm:

$(document).ready(function() { $("#rpt").jqGrid( { url:'/get.json', postData: {filter: $('form').serialize()}, datatype: "json", gridview: true, colModel:[id:'col1'] }); }); 

Through various threads here and on other sites, I tried the suggested JSON.stringify and serializearray () in the form, as well as user-defined functions to no avail. Form data looks encoded and inaccessible on the other hand through _GET.

Any suggestions would be great - thanks!

+2
source share
1 answer

I am not sure what form you want to receive the data from the form on the server side. However, I suggest you use postData in the following form

 postData: { filter: function () { var result = {}, i, item, formInfo = $('form#myForm').serializeArray(), l = formInfo.length; for (i = 0; i < l; i++) { item = formInfo[i]; result[item.name] = item.value; } return JSON.stringify(result); } } 

In the case of the following test form

 <form id="myForm"> <div><input type="text" name="a" value="1 from a" id="a" /></div> <div><input type="text" name="b" value="2 from b" id="b" /></div> <div><input type="hidden" name="c" value="3 from c" id="c" /></div> <div> <textarea name="d" rows="8" cols="40">4</textarea> </div> <div><select name="e"> <option value="5" selected="selected">5</option> <option value="6">6</option> <option value="7">7</option> </select></div> <div> <input type="checkbox" name="f" value="8" id="f" /> </div> </form> 

The result variable will be

 var result = { a: "1 from a", b: "2 from b", c: "3 from c", d: "4", e: "5" } 

Thus, data conversion will not be performed. Then I suggest converting the result object to a JSON string using JSON.stringify . (Depending on the server code, it may not be needed.) Thus, the filters parameter will be sent as

 {"a":"1 from a","b":"2 from b","c":"3 from c","d":"4","e":"5"} 

You can use Fiddler or Firebug to view the HTTP traffic for the corresponding small demo .

+1
source

Source: https://habr.com/ru/post/918029/


All Articles