1

我试图计算重力窗体列表栏的总和, 不幸的是这不是包含在主要的插件功能,所以我不得不寻找不同的源来完成这一点。我能找到一个应该做到这一点的脚本,可在这里, 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

您是否试过List Field Calculations插件?

它扩展了数字字段计算选项以包含列表字段列。

你可以得到总的列和行的某个字段中的计数,然后配置自己的计算,例如'第3列的总和'。计算将自动填充数字字段。

1

有点晚了这里,但我偶然发现了这个剧本,并决定以创造价格字段的用户定义的量来使用它。客户希望能够进入多个发票数额上显示总,我能够使用下面的方法来得到这个工作:

  1. 与脚本中的神奇之处在于最后一行:

    listFieldColumnTotal(140, 1, 3, 3, true); 
    

元素在功能被描述,但如下中顺序描述:

  • 的表格ID(来自gform_wrapper_ )
  • 列表容器的ID(前。 #field_6_ )
  • ,我们希望作为生活在列表容器(如下面的动态值使用的列项。.gfield_list_7_cell )
  • 结果输入集装箱的ID(前。field_6_ )
  • 定义羯羊或不是我们使用一种货币(我没有这个值设置为“假”的实验,但我想它的工作原理相同

所以,这将导致在我们的最终宣言中将如下所示:

listFieldColumnTotal(6, 7, 2, 18, true); 
  • 我用gform_pre_render滤波器到脚本附加到表格。为了做到这一点,我们将上面的脚本集成到我们的函数中。PHP文件:
  • add_filter('gform_pre_render', 'realtimePriceListUpdate'); 
     
    \t function realtimePriceListUpdate($form) { 
     
    \t ?> 
     
    
     
    \t \t <script type="text/javascript"> 
     
    
     
    \t \t \t jQuery(document).ready(function(){ 
     
    
     
    \t \t \t \t function calculateLFColumnTotal(formId, columnClass, totalFieldId, currency) { 
     
    \t \t \t \t  var columnTotal = 0, 
     
    \t \t \t \t   preField = '#field_' + formId + '_' + totalFieldId, 
     
    \t \t \t \t   totalField = jQuery('#input_' + formId + '_' + totalFieldId), 
     
    \t \t \t \t   cellValue; 
     
    \t \t \t \t  currency = (currency && typeof gf_global !== 'undefined'); 
     
    \t \t \t \t  jQuery(columnClass).each(function() { 
     
    \t \t \t \t   cellValue = jQuery(this).val(); 
     
    \t \t \t \t   cellValue = (currency) ? gformToNumber(cellValue) : cellValue; 
     
    \t \t \t \t   columnTotal += parseFloat(cellValue) || 0; 
     
    \t \t \t \t  }); 
     
    \t \t \t \t  if (jQuery(preField).hasClass('gfield_price')) { 
     
    \t \t \t \t   columnTotal = gformFormatMoney(columnTotal); 
     
    \t \t \t \t   if (jQuery(preField + ' input').length > 1) { 
     
    \t \t \t \t    totalField.html(columnTotal); 
     
    \t \t \t \t    totalField = jQuery('input[name="input_' + totalFieldId + '.2"]'); 
     
    
     
    \t \t \t \t   } 
     
    \t \t \t \t  } else { 
     
    \t \t \t \t   columnTotal = (currency) ? gformFormatMoney(columnTotal) : columnTotal; 
     
    \t \t \t \t  } 
     
    
     
    \t \t \t \t \t totalField.val(columnTotal); 
     
    \t \t  \t \t gformCalculateTotalPrice(formId); 
     
    
     
    \t \t \t \t } 
     
    
     
    \t \t \t \t function listFieldColumnTotal(formId, fieldId, column, totalFieldId, currency) { 
     
    \t \t \t \t  var listField = '#field_' + formId + '_' + fieldId, 
     
    \t \t \t \t   columnClass = '.gfield_list_' + fieldId + '_cell' + column + ' input'; 
     
    \t \t \t \t  
     
    \t \t \t \t  jQuery(listField).on('focusout', columnClass, function() { 
     
    
     
    \t \t \t \t   if (currency && typeof gf_global !== 'undefined') { 
     
    \t \t \t \t    gformFormatPricingField(this); 
     
    \t \t \t \t   } 
     
    \t \t \t \t   calculateLFColumnTotal(formId, columnClass, totalFieldId, currency); 
     
    
     
    \t \t \t \t  }); 
     
    
     
    \t \t \t \t  jQuery(listField).on('click', '.add_list_item', function() { 
     
    \t \t \t \t   jQuery(listField + ' .delete_list_item').removeProp('onclick'); 
     
    \t \t \t \t  }); 
     
    
     
    \t \t \t \t  jQuery(listField).on('click', '.delete_list_item', function() { 
     
    \t \t \t \t   gformDeleteListItem(this, 0); 
     
    \t \t \t \t   calculateLFColumnTotal(formId, columnClass, totalFieldId, currency); 
     
    \t \t \t \t  }); 
     
    
     
    \t \t \t \t } 
     
    
     
    \t \t \t \t listFieldColumnTotal(6, 7, 2, 18, true); 
     
    
     
    \t \t \t }); \t 
     
    
     
    \t \t </script> 
     
    
     
    \t \t <?php 
     
    
     
    \t \t return $form; 
     
    
     
    \t }

  • 最后,我们不能使用通过重力供给形式 “总” 字段。我们必须创建一个“产品”字段,这是我们在步骤1中获取id的字段(结果输入容器的id)。另外,我们必须在“产品”字段的“高级”选项卡中启用“允许字段动态填充”选项。或者,我们可以将产品的字段类型更改为“隐藏”,因为我们不想同时显示总计和产品字段。 Here's a screenshot of the fields required for this list calculation script