sfRegisterGroupCallback
Binds a function to an event on a group control. It is used to execute a code block when a user adds or removes an item from a group control.
Syntax
sfRegisterGroupCallback(id, event, callback)
Parameters
Name | Type | Mandatory | Description |
---|---|---|---|
id | string | yes | Control id of the group control. |
event | string | yes | Event (user action on the form control) that should trigger the callback. See supported events. |
callback | function | yes | Code block to be executed when the event occurs. The callback function contains one parameter:
The parameter is available only for add event. See callback function. |
event
An event is a user action on a group control like click of add button to add a new row to the group or click of cross icon to remove a row from the group.
The supported events are:
Name | Description | Applies to |
---|---|---|
add | Occurs when a new row is added through one of these actions:
| Applies to Group. |
remove | Occurs when the cross icon in the context menu of one of the existing rows is clicked | Applies to Group. |
Usage examples
Example 1
Let us consider a group control Products that contains a selectbox UnitOfMeasurement. When a new product row is added, we can select MT (Metric Ton) by default as the unit of measurement in the new row as,
sfRegisterGroupCallback('control_products', 'add', function(position){
var index = position - 1;
sfSetValue('control_unitofmeasurement', 'MT', index);
});
Example 2
Let us consider a group control Products and a readonly numeric field NumberOfProducts outside the group control that displays the total number of products in the group control. When a product row is removed, the total displayed can be updated as,
sfRegisterGroupCallback('control_products', 'remove', function(){
var count = sfGetValue('control_numberofproducts');
sfSetValue('control_numberofproducts', count - 1);
});