Query database and dynamically create repeat groups

Hello I am trying to dynamically set the number of repeat groups when a form loads.
But I want query the database to find out how many repeats are needed.
I am using this code which works great, I feel I need to dynamically change the value of howManyRepeats.

JavaScript:
requirejs(['fab/fabrik'], function() {
   Fabrik.addEvent('fabrik.form.group.duplicate.min', function(form) {
    var name = Fabrik.getBlock('form_17').formElements.get('tbl_inspections___dbj_vehicle');
    var howManyRepeats = 12;
    var group = 90;
    form.options.minRepeat[group] = form.options.maxRepeat[group] = howManyRepeats;
});

I am using this code to get the information from database which retrieves the data I want, but I can't seem to marry the two together.

JavaScript:
function getPreviousFaultCount(){
   var url = "index.php?option=com_fabrik&format=raw&task=plugin.userAjax&method=countPreviousFaults";
var vehicleId = 8;

                 new Request(
                {
                  type: 'get',
                  data: {vehicleId: vehicleId},
                  async: true,
                  url:url,

                  onSuccess: function(response){
                    console.log(response);
              
                  }
                }).send();}
}

Any suggestions are most welcome?
 
I never used it but I think dynamically repeat group numbers are built in the repeat groups setting with the "Repeat Num Element".
 
Thanks troester
I have looked at the method "Repeat Num Element" but I need to change several groups. So I was hoping to use form_X.js with a for loop and just one request to the database to get the repeat number requirements.
 
Have you tried just calling the AJAX method from within the fabrik.form.group.duplicate.min event handler, and setting the form.options.minRepeat from within the onSuccess of that? Although probably recode it to use jQuery ajax. Something like ...

Code:
requirejs(['fab/fabrik'], function() {
    Fabrik.addEvent('fabrik.form.group.duplicate.min', function (form) {
        var vehicleEl = Fabrik.getBlock('form_17').formElements.get('tbl_inspections___dbj_vehicle');
        var vehicleId = vehicleEl.getValue();
        var group = 90;
        var url = "index.php?option=com_fabrik&format=raw&task=plugin.userAjax&method=countPreviousFaults";
        jQuery.ajax({
            url: url,
            method: 'get',
            dataType: 'json',
            data: {
                'vehicleId': vehicleId
            }
        }).fail(function (jqXHR, textStatus, errorThrown) {
            window.alert(textStatus);
        }).done(function (json) {
            form.options.minRepeat[group] = form.options.maxRepeat[group] = json.howManyRepeats;
        });
    });
});

I've made the assumption that your tbl_inspections___dbj_vehicle element has the vehicle ID as the value.

The problem I think you may find trying to do it this way is the asyncronous nature of JS. I think your main event handler is going to return (so the Fabrik form processing will resume) before your AJAX call has completed and set the group repeats. So you may need to force the AJAX call to be syncronous by adding ...

async: false

... to the call. But that would block everything until the call returns.

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top