1. NOTICE: If you are using Fabrik and update to Joomla 3.10, you will need to update to Fabrik 3.10. And, if you are using Fabrik, do not upgrade to Joomla 4, we do not have a supported version ready for release. More information on a release date coming soon. Also, please note that Fabrik 3.10 will not install on any Joomla sites less than 3.8.
    Dismiss Notice

Does the Calc element calculate elements outside it's group?

Discussion in 'Community' started by Mustafa_s, Mar 18, 2015.

  1. Mustafa_s

    Mustafa_s Member

    Level: Community
    Hi,

    I have a calculation element set to calculate the sum of multiple dropdowns, this works great as long as the Calc is in the same group as the dropdowns (lets call this the FOOD group). Ajax calculations work great in this scenario.

    However, as soon as I move the Calc to a different group (lets call this the TOTAL group), the Calc element no longer calculates the sum of the elements inside the FOOD group via Ajax. If I submit the record and view the details of the record I get the sum in the TOTAL group.

    Is this per design? How can I Calc the sum of dropdown elements that are inside different groups via AJAX?

    [​IMG]
     
  2. Bauer

    Bauer Well-Known Member

    Level: Community
    You can't reference elements in a group (using placeholders) that is not in the same list/form.

    I remember reading your post yesterday regarding this menu calculation form - where you posted example attachments - but it looks like you removed it.

    If you stick with what you had yesterday...
    To keep the key values unique and still be able to 'pick out' the prices for your calculation, just add a unique identifier ahead of the 'value'/price

    I don't remember the exact value and label combinations that you were using in your dropdown element but it was something like this...
    2.75 | 32 oz. Coke
    2.75 | 32 oz. Pepsi
    2.75 | 32 oz. Dr. Pepper

    You needed a way of pulling the prices out of the option - while still having a unique value for each option.
    To do that, configure your dropdown like this instead...
    1_2.75 | 32 oz Coke
    2_2.75 | 32 ox. Pepsi
    3_2.75 | 32 oz. Dr. Pepper

    Then in your calc element - where you had ajax watching for changes to the dropdown selection and calculating a total price - use the php substr function to pull out the correct price for the selected item. For example...
    PHP:
    $e4 = substr('{allow_pizza_oder_4_repeat___drinks}', strpos( '{allow_pizza_oder_4_repeat___drinks}', '_')+1 );
    This way, if '32 oz. Pepsi' was selected this would return the '2.75' part of the '2_2.75' value that is assigned to the option. You have both your unique value and the ability to 'pull out' the correct price to use for your calc.

    As in your latest example, for the calc to work correctly, you may need to assure that $e4 is a float- I'm not sure. e.g. ...
    $e4 = (float) substr('{allow_pizza_oder_4_repeat___drinks}',strpos('{allow_pizza_oder_4_repeat___drinks}','_')+1);

    Also you may need to first assign '{allow_pizza_oder_4_repeat___drinks}' to a variable before applying the php function to it - I'm not so sure about that either. e.g. ...
    $e4full = '{allow_pizza_oder_4_repeat___drinks}';
    $e4price = (float) substr($e4full, strpos($e4full,'_')+1);

    I hope my interjection here at least gets you a little further than you were yesterday.:)
     
  3. Mustafa_s

    Mustafa_s Member

    Level: Community
    Bauer,

    Thanks for the detailed response, I think I have a good grasp of how the Calc element functions thanks to your examples.

    I already have about 100 elements or so ready to go, they're all different groups with validations. If I have to bring them all into one group I will lose the validations (during element copy to gather them all in 1 group), it will take a lot of time to redo those validations.

    Is there no way tell the Calc element to check additional elements in other groups? Can I reference them without using placeholder elements, if so how?
     
  4. Mustafa_s

    Mustafa_s Member

    Level: Community
    I have another related question if anyone has the answer, can I tell the Ajax Observe Fields to WATCH ALL except ELEMENT1 and ELEMENT2? I have so many elements, it would be easier to capture all instead of tell it to watch each element seperately.

    Is this even possible?
     
  5. troester

    troester Well-Known Member Staff Member

    Level: Community
    It's no problem to use an element from an other group (of the same form) in a calc.
    You don't need to observe elements you are using in the calc.
    Moving an element from one group to an other should keep any settings.

    If you want to access elements of a repeat group this may help
    http://fabrikar.com/forums/index.php?wiki/calculation-element/
    http://fabrikar.com/forums/index.php?wiki/php-form-plugin/#accessing-form-data

    BTW: I assume 100 ajax calc elements will blast the form.
    (and you can't use a calc element in a calc element)
     
  6. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Problems only arise when you try and reference elements in a different repeat group using placeholders. That's because we ("Fabrik") have no way of knowing which instance of the repeat group you want the values from. A placeholder can only hold one value, it can't hold multiple values for all repeats.

    In a calc element in a repeat group, when you use a placeholder for another element in the same group, we assume that you want the value from the same repeat instance. So when (say) a calc in the second repeat instance of a group refers to the placeholder for another element in the same group, we assume you want the value of that other element in the same (second) repeat instance. But if you try and reference a placeholder from a different repeat group, we have no way of knowing which repeat instance you want.

    This is just an inherent limitation of placeholders. They aren't designed to be a complete solution for getting any element value in a complex form, they are more of a short cut for simple usage. Once you start dealing with multiple repeat groups, you'll need to reference the actual for data, which is in the $data array, keyed by full element name.

    Also remember than "AJAX calc" fires off a call to the server. So if you have a lot of calc elements, as troester points out, you can overwhelm the server ... up to and including getting blacklisted by a firewall, if the firewall has restrictive limits on concurrent sessions for DDOS attack detection ... so if you had 50 calcs all firing within a second or so, a server may well assume it is under DDOS attack!

    -- hugh
     
    jimkeane likes this.
  7. Mustafa_s

    Mustafa_s Member

    Level: Community
    Troester and Hugh, thank you.

    That makes a lot of sense to me now that you have both explained how AJAX works and the drawbacks of using placeholders. I think AJAX will cause issues since I have a lot of elements, what I'll do is calculate on save instead - equally functional for my needs.

    I Appreciate the assistance guys.
     
  8. jimkeane

    jimkeane Member

    Level: Community
    This was very helpful to resolving an issue we had with our install. We have our server behind a load balancer that was patched and started blocking AJAX calls to the underlying server! All fixed now though :)
     

Share This Page