free jqGrid: wrapping POST data in beforeSubmit function

517 views Asked by At

I'm using Oleg's free jqGrid. After my form editing, I am trying to wrap my POST data, so that in PHP I get a nice array like:

Array(
oper => edit,
data => Array(
    foo => '123',
    bar => 'xyz',
    ...
    )
)

where the operator and the data on which it operates are nicely separated.

My intuitive approach would have been something like:

            beforeSubmit: function( postdata, formid ) {
                    var d = {};
                    d.data = postdata;
                    console.log( d );
                    postdata = d;
                    return( [true, ''] );
            }

Unfortuntely this doesn't work; console.log(d) nicely prints the desired result, but postdata is not changed. I am not well-versed in javascript, but I suspect the global postdata is not changed from within the function, and unfortunately the function doesn't allow me to return it.

Any suggestions on how to solve this. It is a minor problem, but I just thought that it looked more structured, and would be more maintainable, to have the clean separation between operator and its data.

Thank you.

1

There are 1 answers

0
Oleg On BEST ANSWER

The callback beforeSubmit isn't the best choice in your case. Instead of that I'd recommend you to use serializeEditData callback, which you can define either as the option of form editing (inside of formEditing parameter of jqGrid) or as the parameter of jqGrid. The callback get postdata as the only parameter and it should return the modified object of JSON string, which should be sent to the server. The code could be about the following

serializeEditData: function (postdata) {
    var d = {
            oper: postdata.oper,
            data: $.extend(true, {}, postdata) // make copy
        };
    delete d.data.oper; // remove unneeded oper property
    return d;
}