bookmark_bordergform_post_calculation_events

Description

This filter can be used to register custom methods for triggering calculations.

Usage

gform.addAction( 'gform_post_calculation_events', function ( mergeTagArr, formulaField, formId, calcObj ) {
    // do stuff
} );

Parameters

  • mergeTagArr array

    The merge tag being processed.

    mergeTagArr[0] contains the full merge tag e.g. {Field label:ID:modifier}
    mergeTagArr[1] contains the field or input ID e.g. 2
    mergeTagArr[3] contains the modifier(s) including the colon e.g. :value
    mergeTagArr[4] contains the modifier(s) without the colon e.g. value

  • formulaField JavaScript Object

    The current calculation field object. e.g.

    {"field_id":3,"formula":"{:1}+{:2}","rounding":""}

  • formId integer

    The ID of the form in use.

  • calcObj JavaScript Object

    The calculation object.

Example

This example shows how you can trigger calculations by adding or removing List field rows or modifying the value of a List field input.

gform.addAction( 'gform_post_calculation_events', function ( mergeTagArr, formulaField, formId, calcObj ) {
    var fieldId = parseInt( mergeTagArr[1] ),
        fieldSelector = '#field_' + formId + '_' + fieldId;

    if ( jQuery( fieldSelector + ' table.gfield_list' ).length == 1 ) {
        jQuery( fieldSelector )
            .on( 'click', '.add_list_item', function () {
                jQuery( fieldSelector + ' .delete_list_item' ).removeProp( 'onclick' );
                calcObj.bindCalcEvent( fieldId, formulaField, formId, 0 );
            } )
            .on( 'click', '.delete_list_item', function () {
                gformDeleteListItem( this, 0 );
                calcObj.bindCalcEvent( fieldId, formulaField, formId, 0 );
            } )
            .on( 'change', ':input', function () {
                calcObj.bindCalcEvent( fieldId, formulaField, formId, 0 );
            } );
    }
} );

Placement

Your code snippet can be placed in a HTML field on your form or in a theme custom JavaScript file.

Source Code

This filter is located in js/gravityforms.js