Solved : Total of fields in repeat groups shown on another group

louis037

Member
Hi, I have a problem to show in a field the total of other fields which are in a repetable group. here's my code and the error that I get on change numbers in repetable groups : "el is not defined"
My code :
JavaScript:
// JavaScript Document
requirejs(['fab/fabrik'], function() {
    function total(el) {
// use form_X where X is your numeric form ID
var form = Fabrik.getBlock('form_48');

// get the object we want to summarize in the 2nd group
var rnum = el.getRepeatNum();
   var elLignName = 'data_reac_trainings_80_repeat___hour_' + rnum;
   var elLignVal = this.formElements.get(elLignName).getValue();

// replace 17 with your numeric group ID for the repat group
var repeats = form.repeatGroupMarkers[80]
var hourTot = 0;

for (c = 0; c < repeats; c++) {
   // replace foo_repeat___whatever_ with the relevant element names in the repeat group, but keep the appended _
    //get repetable object
   var elObjectName = 'data_reac_trainings_80_repeat___hour_' + c;
    //get repetable object value
   var elObjectVal = form.formElements.get(elObjectName).getValue();
   
   
   if (elObjectVal != '') {     
         var elHourVal = elObjectVal;
         hourTot += elHourVal.toFloat();   
   }
}

// replace foo___total_price with the element name
form.formElements.get('data_reac_trainings___hours_total').update(hourTot.toString());
}

    // Add events when adding/deleting groups
    Fabrik.addEvent('fabrik.form.group.duplicate.end', function(form, event) {
        total(el);
    });

    Fabrik.addEvent('fabrik.form.group.delete.end', function(form, event) {
        total(el);
    });

    document.addEvent('blur:relay(input[name^=data_reac_trainings_80_repeat___hour])', function() {
        total(el);
    })
})
The image of my form is joined too.ERROR_Screenshot-2017-9-30 REAC Formations.png
Can anyone help me please. Thanks!!!
 
Well, you aren't actually using the only value you use el for, so try ...

Code:
requirejs(['fab/fabrik'], function() {
    function total() {
// use form_X where X is your numeric form ID
        var form = Fabrik.getBlock('form_48');

// replace 17 with your numeric group ID for the repat group
        var repeats = form.repeatGroupMarkers[80]
        var hourTot = 0;

        for (c = 0; c < repeats; c++) {
            // replace foo_repeat___whatever_ with the relevant element names in the repeat group, but keep the appended _
            //get repetable object
            var elObjectName = 'data_reac_trainings_80_repeat___hour_' + c;
            //get repetable object value
            var elObjectVal = form.formElements.get(elObjectName).getValue();


            if (elObjectVal != '') {
                var elHourVal = elObjectVal;
                hourTot += elHourVal.toFloat();
            }
        }

// replace foo___total_price with the element name
        form.formElements.get('data_reac_trainings___hours_total').update(hourTot.toString());
    }

    // Add events when adding/deleting groups
    Fabrik.addEvent('fabrik.form.group.duplicate.end', function(form, event) {
        total();
    });

    Fabrik.addEvent('fabrik.form.group.delete.end', function(form, event) {
        total();
    });

    document.addEvent('blur:relay(input[name^=data_reac_trainings_80_repeat___hour])', function() {
        total();
    })
})
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top