How to display data from other columns?

multiple columns display

Sometimes, you want to have more control on how data should be displayed, such as showing full name instead of displaying first and last name in separate columns. Similar use cases includes manipulating data, modifying, or even adding additional information in a single column.

Custom formatter is your friend.

A Custom Formatter is a powerful and versatile function. You will fall in love with set_col_property()  It not part of phpGrid, but a jqGrid Javascript function with the following parameters:

  • – cellvalue – the cell value to be formatted
  • – options
  • – rowObject – the data of the current row in an array with column name indexed

So a custom formatter function can be declared like so:

1
2
3
function nameFormatter(cellvalue, options, rowObject){
    return rowObject["firstName"] + ' ' + rowObject["lastName"];
}

then use it with phpGrid set_col_property() function:

1
$dg -> set_col_property('firstName', array('formatter' => '###nameFormatter###')); // must have ###

The example demonstrates concatenating first and last name as full name displayed inside firstName column .

Full example code

1
2
3
4
5
6
7
8
9
10
11
<?php
$dg = new C_DataGrid("SELECT firstName, lastName, age FROM table1", "id", "table1");
$dg->set_col_property('firstName', array('formatter'=>'###nameFormatter###')); // must have ###
$dg->display();
?>
 
<script type="text/javascript">
function nameFormatter(cellvalue, options, rowObject){
    return rowObject["firstName"] + ' ' + rowObject["lastName"];
}
</script>