Skip to content

Currency conversion

Problem:


We need to be able to convert USD to EUR when viewing the grid. Given the currency conversion rate changes, we don't want to hard-code the EUR value. Is there a way to display a EUR "column" next to the USD value so we know the value?

Solution:


phpGrid supports adding a virtual column to the end of the grid. Please check out https://phpgrid.com/example/virtual-column-aka-calculated-column/

Code sample:

<?$dg = new C_DataGrid('SELECT customerName, city, state, creditLimit FROM customers', 'customerName', 'customers'); 

// calculated value to be displayed in the virtual column
$col_formatter = <<<COLFORMATTER
function(cellvalue, options, rowObject){
    var n1 = parseInt(rowObject[3],10);
    return '&euro; ' + (n1 * 0.94);
}
COLFORMATTER;

$dg -> add_column(
    'EuroPrice',
    array('name'=>'EuroPrice',
        'index'=>'EuroPrice',
        'width'=>'360',
        'align'=>'left',
        'sortable'=>false,
        'formatter'=>$col_formatter),
    'EUR');
$dg->display();
?>

creditLimit here is in USD, the $col_formatter converts that into Euro and display on the page. Note virtual column can be only at the end of the grid.

Feedback and Knowledge Base