Joomla 3.3 - Javascript "Fabrik.blocks[formRef] is undefined" error

Hi,
just wrote this code in a X.js form:
Code:
function setRepeatElementsTotals() {
    var elementName = 'orders_7_repeat___line_quantity';
    var totalElement = 'orders___product_totals';
    var total = 0;
 
    if (typeof(Fabrik.blocks.form_4) == 'undefined') {
        console.log('Details!!!');
        var formRef = 'details_4';           
       
    } else {
        console.log('Form!!!');
        var formRef = 'form_4';           
}
 
    var elements = Fabrik.blocks[formRef].formElements;
 
    elements.each(function (element, key) {
        if (key.contains(elementName)) {
            total += element.get('value').toFloat();
        }
    });
 
 
    elements.get(totalElement).update(total);
};

It calculates a grand total for repeat group field and it works fine when it's run in a form but when I run it in a detail view (however, I think I'm in a detail view since I'm modifying a record and .form is undefined) I get this error on the console:

TypeError: Fabrik.blocks[formRef] is undefined

It seems 'details_4' is wrong, but what do I have to use?

Hope someone has an idea...
 
Diiging deeper, I found the issue:

When we are in a "new item" form, it's referenced as form_X (that's correct).
When we are updating an existing item, form is referenced as form_X_Y, where Y is the record number.
 
Just did a workaround, but it will work until Fabrik.blocks will have a single property...
Code:
function setRepeatElementsTotals() {
    var elementName = 'orders_7_repeat___line_quantity';
    var totalElement = 'orders___product_totals';
    var total = 0;
    for(var key in Fabrik.blocks) {
        formRef = key;
    }     
 
    var elements = Fabrik.blocks[formRef].formElements;
 
    elements.each(function (element, key) {
        if (key.contains(elementName)) {
            total += element.get('value').toFloat();
        }
    });
 
    elements.get(totalElement).update(total);
};
 
Hi mauro
I tell you my problem:
I have two groups to calculate expenses of a company

  • Group 1 = gastos table (id, total -> element to calculate the sum of the subtotal field)
  • Group 2 = gasto_3_repeat table (id, parent_id, id_servicio-> Join servicios___id_servicio, quantity -> field, price -> field, subtotal-> calc php -> quantity * price)
  • List servicios: table servicios (id_servicio, description)

I want to do is calculate the total of the subtotals of repeated groups.
I have a month trying to do this and I can not. PLEASE HELP ME :'(
Attached is a picture...

My javascript is form_2.js:

Code:
requirejs(['fab/fabrik'], function () {
function total() {
  // replace 1 with the forms id
  var formRef = 'form_2';
  // Replace with the element name (note it is prefixed with join___JOINID___)
  var elementName = ('join___1___gastos_3_repeat___total_part');
  // The element containing the line items total
  var totalElement = 'gastos___total';
  var total = 0;
  // Get the forms elements
  var elements = Fabrik.blocks[formRef].formElements;
  // Loop over the elements and if their key contains the elementName update the total
  elements.each(function (element, key) {
    if (key.contains(elementName)) {
     
      var v = (Number(element.get('value'))).toFloat(2);
 
    }
  }
);
  // update the total element
  elements.get(totalElement).update(total);
}
// 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();
});
});

PHP plugin in form "gasto":
PHP:
$rowid = $formModel->_formData[id];
$total = $formModel->_formData[total];
if (!empty($total)) {
  $db = JFactory::getDBO();
    $query = $db->getQuery(true);
  $query->update('gastos')->set("total = '$total'")->where("id = '$rowid'");
  $db->setQuery($query);
  $db->query();
}


(I speak Spanish too)
My version Joomla! is 3.1 and Fabrik 3.1
 

Attachments

  • problem1.png
    problem1.png
    15.2 KB · Views: 296
  • problem2.png
    problem2.png
    22.6 KB · Views: 304
  • problem3.png
    problem3.png
    49.4 KB · Views: 288
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top