我正在尝试计算Gravity Forms中列表列的总和, 不幸的是,这不是主插件中包含的功能,所以我必须找到一个不同的源来完成这个。我能够找到一个应该完成此任务的脚本,可以在这里找到, 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 );
但是,我不确定哪些元素应该更改,哪些应该保留原样。如果有人有这方面的经验,并能指导我朝着正确的方向发展,那就太棒了。
答案 0 :(得分:1)
这里有点晚了,但我偶然发现了这个脚本并决定使用它来创建用户定义的价格字段数量。客户希望能够输入多个发票金额并显示总金额,并且我能够使用以下方法实现此工作:
剧本的神奇之处在于最后一行:
listFieldColumnTotal( 140, 1, 3, 3, true );
元素在函数中描述,但按如下顺序描述:
因此,这将导致我们的最终声明将如下所示:
listFieldColumnTotal( 6, 7, 2, 18, true );
add_filter( 'gform_pre_render', 'realtimePriceListUpdate' );
function realtimePriceListUpdate( $form ) {
?>
<script type="text/javascript">
jQuery(document).ready(function(){
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;
});
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);
gformCalculateTotalPrice(formId);
}
function listFieldColumnTotal(formId, fieldId, column, totalFieldId, currency) {
var listField = '#field_' + formId + '_' + fieldId,
columnClass = '.gfield_list_' + fieldId + '_cell' + column + ' input';
jQuery(listField).on('focusout', 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( 6, 7, 2, 18, true );
});
</script>
<?php
return $form;
}
&#13;
答案 1 :(得分:0)
您是否尝试过List Field Calculations插件?
它扩展了数字字段计算选项以包括列表字段列。
您可以获取字段的总数和字段中的行数,然后配置您自己的计算,例如'第3列的总和'。计算将自动填充数字字段。