Column properties 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
Display Hyperlink From Another Field of The Same Row https://phpgrid.com/example/display-hyperlink-from-another-field-of-the-same-row/ Sat, 17 Feb 2018 09:56:49 +0000 http://phpgrid.com/?p=9420 This demo is based on Hyperlink to Call JavaScript Function. It triggers a JavaScript event when hyperlink is clicked. To call JavaScript function on hyperlink click, we use “showlink” in set_col_format method. In the following example, the gotoUrl() function displays text from one field (productName) and hyperlink from another (productUrl) in the same row. Javascript: […]

The post Display Hyperlink From Another Field of The Same Row appeared first on phpGrid - PHP Datagrid.

]]>
This demo is based on Hyperlink to Call JavaScript Function. It triggers a JavaScript event when hyperlink is clicked.

To call JavaScript function on hyperlink click, we use “showlink” in set_col_format method.

In the following example, the gotoUrl() function displays text from one field (productName) and hyperlink from another (productUrl) in the same row.

Javascript:

1
2
3
4
5
6
7
8
 gotoUrl = function (grid,param) {
    var ar = param.split('=');
    if (grid.length > 0 && ar.length === 2 && ar[0] === '?id') {
        var rowid = ar[1];
        var url = grid.getCell(rowid, 'productUrl');
        window.location.href = url;
    }
};

PHP:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 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", "products");
$dg -> set_col_title("productCode", "Product Code");
$dg -> set_col_title("productName", "Product Name");
$dg -> set_col_title("productLine", "Product Line");

// form hyperlink from another cell that contains URL in product code
$dg->set_col_format("productName", "showlink", array("baseLinkUrl"=>"javascript:", "target"=>"_new",
    "showAction"=>"gotoUrl(jQuery('#products'),'",
    "addParam"=>"');"));

$dg -> display();

See live demo!

The post Display Hyperlink From Another Field of The Same Row appeared first on phpGrid - PHP Datagrid.

]]>
9420
Column Freeze https://phpgrid.com/example/column-freeze/ Sat, 20 Apr 2013 22:20:08 +0000 http://phpgrid.com/?p=2452 You can now use the set_col_frozen() method to set the column freeze method. It’s useful when working with a big table with many columns. The freezing column must start from the very left and then one by one to the right. 12345678// Always include namespace and conf.php on TOP of the script. use phpCtrl\C_DataGrid; require_once("/file/path/to/conf.php"); […]

The post Column Freeze appeared first on phpGrid - PHP Datagrid.

]]>
You can now use the set_col_frozen() method to set the column freeze method. It’s useful when working with a big table with many columns. The freezing column must start from the very left and then one by one to the right.

1
2
3
4
5
6
7
8
// 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 orders", "orderNumber", "orders");
$dg->set_dimension(600, 400, false);
$dg->set_col_frozen('orderNumber');
$dg -> display();

Note that it is recommended to set text not to wrap with CSS “nowrap”. See http://phpgrid.uservoice.com/knowledgebase/articles/154972

See Live Example!

The post Column Freeze appeared first on phpGrid - PHP Datagrid.

]]>
2452
Hyperlink to Call JavaScript Function https://phpgrid.com/example/call-javascript-function-on-hyperlink-click/ Sun, 15 Jul 2012 20:59:37 +0000 http://phpgrid.com/?p=2199 You can show hyperlink using set_col_link for static link or set_col_dynalink for dynamic link. What about if you want to do something even fancier, like open a new window, or trigger a JavaScript event when hyperlink is clicked? To call JavaScript function on hyperlink click, use “showlink” in set_col_format method. In the following example, the […]

The post Hyperlink to Call JavaScript Function appeared first on phpGrid - PHP Datagrid.

]]>
You can show hyperlink using set_col_link for static link or set_col_dynalink for dynamic link. What about if you want to do something even fancier, like open a new window, or trigger a JavaScript event when hyperlink is clicked?

To call JavaScript function on hyperlink click, use “showlink” in set_col_format method.

In the following example, the first set_col_format displays text in alert window using JavaScript function “myFunction” to retrieved text from a different cell in the same row. The second does a phpgrid.com site search using jQuery Ajax function “phpGridSiteAjaxSearch”, and displays the result in a alert message window.

PHP:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 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", "products");
$dg -> set_col_title("productCode", "Product Code");
$dg -> set_col_title("productName", "Product Name");
$dg -> set_col_title("productLine", "Product Line");

$dg->set_col_format("productLine", "showlink", array("baseLinkUrl"=>"javascript:", "target"=>"_self",
"showAction"=>"myFunction(jQuery('#products'),'",
"addParam"=>"');"));

$dg->set_col_format("productName", "showlink", array("baseLinkUrl"=>"javascript:", "target"=>"_self",
"showAction"=>"phpGridSiteAjaxSearch(jQuery('#products'),'",
"addParam"=>"');"));
$dg -> display();

JavaScript:

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
31
32
33
myFunction = function(grid, param) {
    var ar = param.split('=');
    if (grid.length > 0 && ar.length === 2 && ar[0] === '?id') {
        var rowid = ar[1];
        var prodDesc = grid.getCell(rowid, 'productDescription');

        alert(prodDesc);
    }
};

phpGridSiteAjaxSearch = function(grid, param) {
    var ar = param.split('=');
    if (grid.length > 0 && ar.length === 2 && ar[0] === '?id') {
        var rowid = ar[1];
        var productName = grid.getCell(rowid, 'productName');

        jQuery.ajax({
            url: 'https://phpgrid.com',
            data: {
                s: productName
            },
            type: 'GET',
            dataType: 'text',
            success: function(data, status) {
                // your code goes here for a successul Ajax call .e.g open a new window
                alert(data);
            },
            error: function(data, status, err) {
                alert(data + status);
            }
        });
    }
};

See Live Example

The post Hyperlink to Call JavaScript Function appeared first on phpGrid - PHP Datagrid.

]]>
2199
Required fields https://phpgrid.com/example/required-fields/ Fri, 17 Sep 2010 00:02:29 +0000 http://64.38.236.7/?p=543 To set fields as required in an editable grid, use set_col_required() method. If required fields are left blank, an “* required” message will be displayed. One more more columns can be specified as required fields in this method. 12345678910111213141516171819202122232425// Always include namespace and conf.php on TOP of the script. use phpCtrl\C_DataGrid; require_once("/file/path/to/conf.php");   $dg = […]

The post Required fields appeared first on phpGrid - PHP Datagrid.

]]>
To set fields as required in an editable grid, use set_col_required() method. If required fields are left blank, an “* required” message will be displayed. One more more columns can be specified as required fields in this method.

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
// 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 Orders", "orderNumber", "Orders");

// change column titles
$dg -> set_col_title("orderNumber", "Order No.");
$dg -> set_col_title("orderDate", "Order Date");
$dg -> set_col_title("shippedDate", "Shipped Date");
$dg -> set_col_title("customerNumber", "Customer No.");
 
// enable edit
$dg -> enable_edit("INLINE", "CRUD");

// hide a column
$dg -> set_col_hidden("requiredDate");

// read only columns, one or more columns delimited by comma
$dg -> set_col_readonly("orderDate, customerNumber");

// required fields
$dg -> set_col_required("orderNumber, customerNumber");
 
$dg -> display();

See Live Example!

To test, click on any row to toggle inline edit, leave a required fields blank, then hit Enter.

The post Required fields appeared first on phpGrid - PHP Datagrid.

]]>
543
Read only fields https://phpgrid.com/example/read-only-fields/ Thu, 16 Sep 2010 23:59:45 +0000 http://64.38.236.7/?p=540 Sometimes we don’t want certain fields to be editable, then we can use set_col_readonly() method. Note that you can set more than one columns to be read only using this method. 123456789101112131415161718192021use phpCtrl\C_DataGrid; require_once("/file/path/to/conf.php");   $dg = new C_DataGrid("SELECT * FROM Orders", "orderNumber", "Orders"); // change column titles $dg -> set_col_title("orderNumber", "Order No."); $dg -> […]

The post Read only fields appeared first on phpGrid - PHP Datagrid.

]]>
Sometimes we don’t want certain fields to be editable, then we can use set_col_readonly() method.

Note that you can set more than one columns to be read only using this method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
use phpCtrl\C_DataGrid;
require_once("/file/path/to/conf.php");  

$dg = new C_DataGrid("SELECT * FROM Orders", "orderNumber", "Orders");

// change column titles
$dg -> set_col_title("orderNumber", "Order No.");
$dg -> set_col_title("orderDate", "Order Date");
$dg -> set_col_title("shippedDate", "Shipped Date");
$dg -> set_col_title("customerNumber", "Customer No.");

// enable edit
$dg ->enable_edit("INLINE", "CRUD");
 
// hide a column
$dg -> set_col_hidden("requiredDate");

// read only columns, one or more columns delimited by comma
$dg -> set_col_readonly("orderDate, customerNumber");
 
$dg -> display();

To allow add for read-only field when adding new records, use “jqGridAddEditAfterShowForm” event, and conditionally remove the “disabled” attribute based on operand, which is either “edit” or “add”.

1
2
3
4
5
6
$dg -> add_event('jqGridAddEditAfterShowForm',
  'function(e, form, oper){
    if (oper == "add") {
      $("#COLUMN_NAME").removeAttr("disabled");
    }
  }'
);

Click on row to see the fields that are set to read-only.

See Live Example!

The post Read only fields appeared first on phpGrid - PHP Datagrid.

]]>
540
Image Display https://phpgrid.com/example/image-display/ Thu, 16 Sep 2010 23:49:56 +0000 http://64.38.236.7/?p=529 Display image is easy in phpGrid. Simply set the column name in set_col_img(), phpGrid will take care of the rest.   1234567891011121314151617181920212223242526272829303132use 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 -> […]

The post Image Display appeared first on phpGrid - PHP Datagrid.

]]>
Display image is easy in phpGrid. Simply set the column name in set_col_img(), phpGrid will take care of the rest.

 

phpGrid Image Display

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
31
32
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");                                      

// display image
$dg -> set_col_img("Image");
                                                 
$dg -> display();


You can also further adjust image display options such as width, height and border with CSS using set_col_property method. The CSS will only apply the specific column.

1
2
3
<style>
    .image_css img{width: 40pt;border:1px solid black;}
</style>
1
$dg->set_col_property('Image', array("classes"=>'image_css'));

Alternatively, you can also resize images for the entire grid with CSS.

 
See Live Example!

The post Image Display appeared first on phpGrid - PHP Datagrid.

]]>
529
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
Display Dynamic URL https://phpgrid.com/example/display-dynamic-url/ Thu, 16 Sep 2010 23:42:51 +0000 http://64.38.236.7/?p=522 From example above, we learn that phpGrid can display simple, static URL using set_col_link() method. However, it is often for database driven webpage that URL is dynamically formed based on parameters value. Method set_col_dynalink() is designed specially for this purpose. The following demonstrates passing the column orderNumber as the dynamic value used as part of […]

The post Display Dynamic URL appeared first on phpGrid - PHP Datagrid.

]]>
From example above, we learn that phpGrid can display simple, static URL using set_col_link() method. However, it is often for database driven webpage that URL is dynamically formed based on parameters value. Method set_col_dynalink() is designed specially for this purpose.

The following demonstrates passing the column orderNumber as the dynamic value used as part of the URL.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 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");
                 
// display dynamic url. e.g.http://www.example.com/?productCode=101&foo=bar
$dg -> set_col_dynalink("productCode", "http://www.example.com/", "productCode", '&foo=bar');                                                                  

$dg -> display();

See Live Example!

The post Display Dynamic URL appeared first on phpGrid - PHP Datagrid.

]]>
522
Hyperlink Display https://phpgrid.com/example/display-hyperlink/ Thu, 16 Sep 2010 23:36:07 +0000 http://64.38.236.7/?p=520 You can display the text in column as static hyperlink by calling set_col_link() and passing the column name. Note this method only display static URL stored in database field. To display dynamic URL links based in variables, see the next example on set_col_dynalink() method. Please note we are using Products table in this example. 12345678910111213// […]

The post Hyperlink Display appeared first on phpGrid - PHP Datagrid.

]]>
You can display the text in column as static hyperlink by calling set_col_link() and passing the column name. Note this method only display static URL stored in database field. To display dynamic URL links based in variables, see the next example on set_col_dynalink() method.

Please note we are using Products table in this example.

1
2
3
4
5
6
7
8
9
10
11
12
13
// 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");                                            
                                                                                     
$dg -> display();

See Live Example!

The post Hyperlink Display appeared first on phpGrid - PHP Datagrid.

]]>
520