How to post variable data with editData or onclickSubmit in jqgrid
Asked Answered
R

2

1

I can't get editData or onclickSubmit to do what I need.

I want the grid to follow the added or edited row after update. So, I need to post some additional info so the server can return the id and correct page of the added/edited record.

I was able to do this using the addfunc and editfunc and a custom form, but I'd like to do it with the jqgrid generated forms.

I have a global declared before the DocumentReady function. Then, I tried using editData in the edit parameters and setting the variables in beforeSubmit or beforeInitData. The variables are posted to the server, but only as they are initially declared. It seems like the editData is created at initialization and can't be updated. I also tried using onclickSubmit, but couldn't get that to work either.

Here's an edited example:

var data2pass = {};
    data2pass['sortColumnName'] = '';
    data2pass['sortOrder'] = '';
    data2pass['rowNum'] = '';

$(document).ready(function(){

  $("#ProdGrid").jqGrid({
    url:'products_DAT.php?thespot=server_ProdGrid',
    datatype: 'json',
    mtype: 'POST',
    colNames:['ID','Product Name:','Category:','Unit Price:'],
    colModel :[ 
      {name:'ProductID', editable:true},
      {name:'ProductName', editable:true},
      {name:'CategoryID', editable:true, edittype:"select", editoptions: { dataUrl:  "products_DAT.php?thespot=select4_CategoryID" }},
      {name:'UnitPrice', align:'right', editable:true, formatter:'currency'}
    ],
    pager: '#ProdGrid_pager',
    rowNum: 15,
    sortname: 'ProductName',
    sortorder: 'asc',
    gridview: true,
    editurl: 'products_DAT.php?thespot=update_ProdGrid',
    height: 'auto'
  });

$("#ProdGrid").jqGrid('navGrid','#ProdGrid_pager', {},
{closeAfterEdit:true, reloadAfterSubmit: false, editData: data2pass,
    beforeInitData: function(formid) { 
    data2pass['sortColumnName'] = 'ProductName';
    data2pass['sortOrder'] = 'asc';
    data2pass['rowNum'] = '15';
    }
}, // Edit parameters
{}, // Add Parameters
{reloadAfterSubmit:true}, // Delete parameters
{}, // Search params
{} // View params
 );

However the data2pass variables are initially declared is what gets posted to the server. What event should I use to update the values of data2pass to post to the server? Or is there another better way to do it?

Any advice is greatly appreciated.

Thanks

Ravelin answered 21/4, 2013 at 18:42 Comment(0)
E
4

You can either define properties of editData using functions

editData: {
    sortColumnName: function () { return "ProductName"; },
    sortOrder: function () { return "asc"; },
    rowNum: function () { return 15; }
}

or use callback onclickSubmit to extend the data posted to the server

onclickSubmit: function (options, postData) {
    return {
        sortColumnName: "ProductName",
        sortOrder: "asc",
        rowNum: 15
    };
}

or use serializeEditData callback

serializeEditData: function (postData) {
    return $.extend(true, {}, postData, {
        sortColumnName: "ProductName",
        sortOrder: "asc",
        rowNum: 15
    });
}

Every from above ways do the same. You can choose one way which you find the mostly convenient for your requirements.

Endaendall answered 21/4, 2013 at 19:15 Comment(1)
I"ll give those a try! Thank you! And thanks also for the other help you provide on this site. I've learned alot reading your replies to others.Ravelin
D
0

I think this should do exactly what you want, it changes the postData variable that is sent to the controller, and then triggers a reload.

$('#gridName').jqGrid('setGridParam', { postData: { KeyName: KeyValue } }).trigger('reloadGrid', [{ page: 1}]);

if you need it to go as part of your edit you can do almost the same thing

$(#gridName).jqGrid('editGridRow', rowid, { editData: { KeyName: KeyValue
Dejong answered 21/4, 2013 at 18:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.