Skip to content

Save Data in Calculated/Virtual Column to Database

To save data in virtual column or calculated column back to database, you can use jqGridAddEditAfterSubmit event (FORM edit only) to post data to another script through a separate Ajax call. The catch is you need to supply your own save data script. Just iterate the $_POST variables and call appropriate functions to save posted data to database. Should be fairly simple to do.

See code snippet below. Replace "orders" with your own table name please. You need to implement save_virtual_column.php to save data.

// post virtual column data to another page after submit through another Ajax call
$afterSubmit = <<<AFTERSUBMIT
function (event, status, postData)
{
    selRowId = $("#orders").jqGrid ('getGridParam', 'selrow');
    virtual_data1 = $("#orders").jqGrid("getCell", selRowId, 'total');   // data in virtual column
    virtual_data2 = $("#orders").jqGrid("getCell", selRowId, 'foo');
    console.log('going to post virtual column data ' + virtual_data1 + ', ' + virtual_data2 + ' to another page through a separate AJAX call');
    $.ajax({ url: 'save_virtual_column.php',
        data: {v_data1: virtual_data1, v_data2: virtual_data2}, // replace customerNumber with your own field name
        type: 'post',
        success: function(output) {
                    alert(output);
                }
        });

}
AFTERSUBMIT;
$dg->add_event("jqGridAddEditAfterSubmit", $afterSubmit);

Read more about virtual column: 
http://phpgrid.com/example/virtual-column-aka-calculated-column/

Feedback and Knowledge Base