使用重力表单列表进行数学运算

时间:2020-06-27 作者:sacredfaith

在使用Gravity表单的客户端站点上工作时,我发现自己想要使用Gravity提供的高级列表的美观,但需要更多的算术火力(请阅读:any,算术火力;Gravity在没有插件的情况下无法在其高级列表字段上进行计算)。

我发现了这个:https://stackoverflow.com/questions/35872452/gravity-forms-find-sum-of-list-column这让我接近了我想要的,但我还需要在每一行中进行一些预先计算。也就是说,假设我有单元格A、B、C和D。我需要对A和B进行数学运算,然后将其存储在C中。然后,我需要找到所有行中所有C单元格的总和。

首先是起始代码:

// ************      Gravity Forms Dynamic List Summation ************** //
//EX:  listFieldColumnTotal( 140, 1, 3, 3, true );
//The elements are described in the function, but are described in order as follows:
//
//The Form ID (ex. gform_wrapper_6)
//The list container\'s ID (ex. #field_6_7)
//The column item which we want to use as the dynamic value which lives beneath the list container 
// (ex. .gfield_list_7_cell2)
//The id of the result input\'s container (ex. field_6_18)
//defines whether or not we\'re using a currency (I didn\'t experiment with this value set to "false", 
// but I imagine it works the same
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, 120, 3, 47, true );

});

1 个回复
SO网友:sacredfaith

现在是最后一段基本上未经测试的代码。最后,我不确定我需要做哪些操作,因此想扩展功能,以应对我的客户将来可能对我提出的任何问题。经过两天的努力,我很兴奋[\'*\',\'+\']这个案子成功了,我想分享它,希望它能帮助其他人。

// ************      Gravity Forms Dynamic List Calculations ************** //
//EX:  listFieldColumnTotal( 6, 120, 2, 3, 4,\'*\', \'+\', 47, true );
//EX:  listFieldColumnTotal( FormID, listContainerID, FirstRowOperand, SecondRowOperand, RowResult, RowOperator, ColumnOperator, ColumnOperatorFieldID, isCurrency );;
//The elements are described in the function, but are described in order as follows:
//
//Form ID (ex. gform_wrapper_6)
//ListContainerID (ex. #field_6_120)
//FirstRowOperand: The first column we want to do math with
//SecondRowOperand: The first column we want to do math with
//RowResult: The column we want to store the result of the: FirstRowOperand [RowOperator] SecondRowOperand
//RowOperator: \'*\',\'+\',\'-\',\'/\'
//ColumnOperator: \'*\',\'+\',\'-\',\'/\'
//ColumnOperatorFieldID (ex. .gfield_list_7_cell2): "Take all of the results stored in the RowResul fields and [ColumnOperator] them together and put the result here"
//defines whether or not we\'re using a currency (I didn\'t experiment with this value set to "false", but I imagine it works the same
jQuery(document).ready(function(){
          function calculateLFColumnTotal(formId, columnClassA,columnClassB, columnClassResult, operatorAB,operatorR, totalFieldId, currency) {
            var subTotal = 0,
                columnTotal = 0,
                preField = \'#field_\' + formId + \'_\' + totalFieldId,             
                totalField = jQuery(\'#input_\' + formId + \'_\' + totalFieldId),               
                cellValue;                
                currency = (currency && typeof gf_global !== \'undefined\');

            //Go through and get all of the values from the first column
            var listA = [];            
            jQuery(columnClassA).each(function () {
                cellValueA = jQuery(this).val();
                cellValueA = (currency) ? gformToNumber(cellValueA) : cellValueA;
                listA.push(cellValueA);                
            });

            //Go through and get all of the values from the second column
            var listB = [];            
            jQuery(columnClassB).each(function () {
                cellValueB = jQuery(this).val();
                cellValueB = (currency) ? gformToNumber(cellValueB) : cellValueB;
                listB.push(cellValueB);               
            });

            
            //Do the appropriate arithmetic on first column and second column values
            var i = 0;
            jQuery(columnClassResult).each(function () {
                                   
                    switch(operatorAB){
                    case "*":
                        subTotal = parseFloat(listA[i]) * parseFloat(listB[i]);
                        break;
                    case "+":
                        subTotal = parseFloat(listA[i]) + parseFloat(listB[i]);
                        break;
                    case "-":
                        subTotal = parseFloat(listA[i]) - parseFloat(listB[i]);
                        break;                      
                    case "/":
                        subTotal = parseFloat(listA[i]) / parseFloat(listB[i]);
                        break;
                    default:
                        subTotal = parseFloat(listA[i]) + parseFloat(listB[i]);
                        break;
                    }

                jQuery(this).val(subTotal);
                i += 1;             
            });


             //Do the appropriate arithmetic on all of the resulting values
             jQuery(columnClassResult).each(function () {
                    cellValue = jQuery(this).val();
                    cellValue = (currency) ? gformToNumber(cellValue) : cellValue;
                           
                    switch(operatorR){
                    case "*":
                        columnTotal *= parseFloat(cellValue) || 0;
                        break;
                    case "+":
                        columnTotal += parseFloat(cellValue) || 0;
                        break;
                    case "-":
                        columnTotal -= parseFloat(cellValue) || 0;
                        break;                      
                    case "/":
                        columnTotal /= parseFloat(cellValue) || 0;
                        break;
                    default:
                        columnTotal += parseFloat(cellValue) || 0;
                        break;
                    }       
            });

        
            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, columnOperandA, columnOperandB, columnResult, operatorAB, operatorR, totalFieldId, currency) {
            var listField = \'#field_\' + formId + \'_\' + fieldId,
                columnClassA = \'.gfield_list_\' + fieldId + \'_cell\' + columnOperandA + \' input\';
                columnClassB = \'.gfield_list_\' + fieldId + \'_cell\' + columnOperandB + \' input\';
                columnClassResult = \'.gfield_list_\' + fieldId + \'_cell\' + columnResult + \' input\';
                
            jQuery(listField).on(\'focusout\', columnClassA, function () {

                if (currency && typeof gf_global !== \'undefined\') {
                    gformFormatPricingField(this);
                }
                calculateLFColumnTotal(formId, columnClassA,columnClassB, columnClassResult, operatorAB,operatorR, totalFieldId, currency);

            });

            jQuery(listField).on(\'focusout\',columnClassB, function () {

                if (currency && typeof gf_global !== \'undefined\') {
                    gformFormatPricingField(this);
                }
                calculateLFColumnTotal(formId, columnClassA,columnClassB, columnClassResult, operatorAB,operatorR, totalFieldId, currency);

            });

            jQuery(listField).on(\'focusout\',columnClassResult, function () {

                if (currency && typeof gf_global !== \'undefined\') {
                    gformFormatPricingField(this);
                }
                calculateLFColumnTotal(formId, columnClassA,columnClassB, columnClassResult, operatorAB,operatorR, 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,columnClassA,columnClassB, columnClassResult, operatorAB, operatorR, totalFieldId, currency);
            });
          }

 // **Edit this line to reflect your situation!
 listFieldColumnTotal( 1, 22, 2, 3, 4,\'*\', \'+\', 50, true );


});

相关推荐

嵌入CodePen Calorie脚本-jQuery错误

我正在尝试嵌入此代码:https://codepen.io/jme11/pen/zMVJVX我的Wordpress关于健身,但我不知道为什么它不能正常工作-我已经下载了代码的导出,它在独立的HTML(+js和css)文件中工作得很好(特别是完成的表单将显示在results div中)。然后,我将HTML嵌入到我的Wordpress模板页面中,它显示在前端,很好,我已经按照HTML中相同的顺序正确链接了css和js,并将它们检入(查看源代码显示它们已连接)填写表格并点击calculate确实可以做一些事情(