Virtual/Calculated Column

Starting version 5.5, you can now add virtual column, AKA calculated column, to your existing datagrid. Virtual, by definition, is that it doesn’t exist in the database table. It’s a calculated field created from other columns. The virtual columns are added to the END of the existing datagrid.

phpGrid only adds a “virtual” column and does NOT change the database table structure. Front-end user should not be able to add/change a column in your database table structure. It should be done only by a very smaller number of people such as DBA, and ultimately through a back-end database administration program.

It’s important that virtual column name is NOT an existing database column name used by PHP datagrid.
X
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$dg = new C_DataGrid("SELECT * FROM orders", "orderNumber", "orders");
$dg->enable_edit('INLINE');

// calculated value to be displayed in the virtual column
// n1 stores column 1 value, n2 stores column 7 value..and so on.
$col_formatter = <<<COLFORMATTER
function(cellvalue, options, rowObject){
    var n1 = parseInt(rowObject[0],10),      
        n2 = parseInt(rowObject[6],10);      
    return n1+n2;
}
COLFORMATTER;

$dg -> add_column(
        'total',
        array('name'=>'total',
            'index'=>'total',
            'width'=>'360',
            'align'=>'right',
            'sortable'=>false,
            'formatter'=>$col_formatter),
        'Total (Virtual)');
$dg->display();

See Live Example! (The last two columns “Total” and “Foo” are virtual.)