custom formatter 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
Data Format Display https://phpgrid.com/example/data-format-display/ Thu, 16 Sep 2010 23:45:28 +0000 http://64.38.236.7/?p=525 phpGrid comes with several frequently used data formatter through set_col_format() method. Different data format has different format options. In most cases, users only need to set column formatting for “integer”, “number”, and “mail” using this method. For “currency’,”link”, and “showlink” formats, phpGrid provides a number of helper functions to make formatting simpler and easier. Refer […]

The post Data Format Display appeared first on phpGrid - PHP Datagrid.

]]>
phpGrid comes with several frequently used data formatter through set_col_format() method. Different data format has different format options. In most cases, users only need to set column formatting for “integer”, “number”, and “mail” using this method. For “currency’,”link”, and “showlink” formats, phpGrid provides a number of helper functions to make formatting simpler and easier. Refer to documentations on column formatter helper methods.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 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 * from products", "productCode", "productCode");
$dg -> set_col_title("productCode", "Product Code");
$dg -> set_col_title("productName", "Product Name");
$dg -> set_col_title("productLine", "Product Line");

// display static Url
$dg -> set_col_link("productUrl");                                            

// dynamic url. e.g.http://www.example.com/?productCode=101&foo=bar
$dg -> set_col_dynalink("productCode", "http://www.example.com/", "productCode", '&foo=bar');

// column format
$dg -> set_col_currency("buyPrice");
$dg -> set_col_format("quantityInStock", "integer", array("thousandsSeparator" => ",",
                                                          "defaultValue" => "0"));  
$dg -> set_col_format("MSRP", 'currency', array("prefix" => "$",
                                                "suffix" => '',
                                                "thousandsSeparator" => ",",
                                                "decimalSeparator" => ".",
                                                "decimalPlaces" => '2',
                                                "defaultValue" => '0.00'));

// the above line is equivalent to the following helper function                        
$dg -> set_col_currency("MSRP", "$", '', ",",".", "2", "0.00");    
                                                                                   
$dg -> display();

See Live Example!

Didn’t see a formatter you need? You can also implement your own custom formatter such as phone number and SSN. Here’s a custom formatter sample code.

The post Data Format Display appeared first on phpGrid - PHP Datagrid.

]]>
525