tutorial Archives | phpGrid - PHP Datagrid Create PHP grids in minutes, not hours. Tue, 03 Dec 2024 18:30:53 +0000 en-US hourly 1 CodeProjecthttps://wordpress.org/?v=7.0.3 129966043 How to display data from other columns? https://phpgrid.com/example/how-to-display-data-from-other-columns/ Sun, 21 Nov 2021 06:48:28 +0000 https://phpgrid.com/?p=9845

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 […]

The post How to display data from other columns? appeared first on phpGrid - PHP Datagrid.

]]>

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
12
13
14
15
<?php
// Always include namespace and conf.php on TOP of the script.
use phpCtrl\C_DataGrid;
require_once("/file/path/to/conf.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>

The post How to display data from other columns? appeared first on phpGrid - PHP Datagrid.

]]>
9845