I am attempting to calculate the total sum of a list column in Gravity Forms,
unfortunately this is not a feature that is included in the main plugin, so I have had to find a different source to accomplish this. I was able to find a script that is supposed to accomplish this, available here,
jsFiddle
function calculateLFColumnTotal(formId, columnClass, totalFieldId, currency) {
var columnTotal = 0,
preField = '#field_' + formId + '_' + totalFieldId,
totalField = jQuery('#input_' + formId + '_' + totalFieldId),
cellValue;
currency = (currency && typeof gf_global !== 'undefined');
jQuery(columnClass).each(function () {
cellValue = jQuery(this).val();
cellValue = (currency) ? gformToNumber(cellValue) : cellValue;
columnTotal += parseFloat(cellValue) || 0;
});x
if (jQuery(preField).hasClass('gfield_price')) {
columnTotal = gformFormatMoney(columnTotal);
if (jQuery(preField + ' input').length > 1) {
totalField.html(columnTotal);
totalField = jQuery('input[name="input_' + totalFieldId + '.2"]');
}
} else {
columnTotal = (currency) ? gformFormatMoney(columnTotal) : columnTotal;
}
totalField.val(columnTotal).change();
gformCalculateTotalPrice(formId);
}
function listFieldColumnTotal(formId, fieldId, column, totalFieldId, currency) {
var listField = '#field_' + formId + '_' + fieldId,
columnClass = '.gfield_list_' + fieldId + '_cell' + column + ' input';
jQuery(listField).on('blur', columnClass, function () {
if (currency && typeof gf_global !== 'undefined') {
gformFormatPricingField(this);
}
calculateLFColumnTotal(formId, columnClass, totalFieldId, currency);
});
jQuery(listField).on('click', '.add_list_item', function () {
jQuery(listField + ' .delete_list_item').removeProp('onclick');
});
jQuery(listField).on('click', '.delete_list_item', function () {
gformDeleteListItem(this, 0);
calculateLFColumnTotal(formId, columnClass, totalFieldId, currency);
});
}
listFieldColumnTotal( 140, 1, 3, 3, true );
However, I am unsure of which elements I should be changing and which should be left as is. If anyone has experience with this, and can guide me in the right direction, that would be great.
A little late here, but I stumbled across this script and decided to use it in order to create a user defined amount of price fields. The client wished to be able to enter multiple invoice dollar amounts and display the total and I was able to get this working using the following method:
The magic with the script lies in the final line:
The elements are described in the function, but are described in order as follows:
So, this will result in our final declaration will look like this:
Here’s a screenshot of the fields required for this list calculation script
Have you tried the List Field Calculations plugin?
It extends the number field calculation options to include list field columns.
You can get the total of a column and the count of rows in a field and then configure your own calculations, e.g. ‘sum of column 3’. The calculation will automatically populate the number field.