price x qty in repeat group

Status
Not open for further replies.

Fred04

Member
hello,

I cannot figure out how to create for a Repeat Group a JS that perform the simple math

price = unit price x qty

here is what I have so far but it works only for the record [0]

Code:
$('join___12___order___price_0').value = 
parseFloat($('join___12___order___unitprice_0').value)
* parseFloat($('join___12___order___qty_0').value);

How can i make it work for any record? i suspect i have to play with some arrays but I'm no so familliar,
 
I am in the process of doing the same thing... Hugh pointed me to count the repeats in the array and then use that to do the calculation... it would be in the eval with PHP I believe...
I will post my attempt
 
I started out with 6 lines of code which Hugh whittled down to one line and then added a check to see if there were any repeats added...

here is the code he came up with...
Code:
$htp_cost = 15;
if (!array_key_exists(2, $formModel->_formData['join'])) {   
   return $htp_cost;
}
else {
   return $htp_cost * (1 + count($formModel->_formData['join'][2]['htp_team_registration_16_repeat___htp_team_member_email']));}

What I needed was to find the number of repeats that were added to the repeating group array then add 1 to that total and multiply the cost with the total sending that amount to PayPal. I used the email element as the field to count.

NOTE: Quantity to PayPal will only show as 1 with this plugin.

Here is the plugin with the code inserted into the Cost Eval box...
PaypalPluginwithRepeatingGroupCalc.png


Worked nicely!

He did add something to the latest github that Fabrik 2 had but hadn't been ported forward to f3, which is a checkOpts() method in the IPN handler. It now looks to see if your custom IPN handler has a checkOpts() method, and if so it will call it right before handler is redirected to PayPal, this lets you modify the $opts[] array, which are the CGI args (query string values) that is sent to PayPal... Paraphrased from Hugh... Chime in Hugh if I regurgitated it incorrectly...

You might add at least the paypal plugin update...
I Still have to work on some IPN handler checks will post those once I get them understood!
 
I'm a little bit lost with what you did.
It seems you set a price of 15 and then if you have a 'repeat group' you will multiply 15 by the number of repeat records....

I don't see how I can reuse that in my initial question

article: A qty=2 unit price = 5 --> price = 10
article: B qty=5 unit price = 9 --> price = 45
and so on.. the user can add as many record as he wants and each time I want to calculate automatically the total price

My question is really related to the 'repeat group' (not the paypal plugin)
 
Ahhhh.... I was using repeating group within the plugin... thought that might help you in working with arrays of a repeating group...

so you want to calculate qty x price at the element level within each repeat of the group and do what with the results? Are you looking for a way to action that calculation with the click of the green + button?
 
I want to trigger the calculation 'on change' of price or qty. That's the reason I would like to write some javascript

for now, I just want to save the result in my database

Any idea?
 
I think Fred needs this in JS to make it real time.....

Fred I can't help you as I'm useless with JS but for doing some Ajax the other day I discovered using this. And I actually mean the word 'this'.

Now I'm sure there a mootools ways of doing this but I had a repeat element I was trying to apply JS to but I couldn't because each element had a different reference, i.e

join___12___order___price_0
join___12___order___price_1
join___12___order___price_2

So in my element JS box I used the following code to get to what I wanted.


var identify = $(this).getElement('li').get('text');

getInfo(identify);



Then with a form js, i.e x.js I had a js function

function getInfo(identify){ js code here }


Not sure if that's the best way, but it worked for me.

The .getElement('li').get('text'); bit was to fire the JS when I clicked the element, so you probably don't want that but hopefully it will give you a start.
 
thanks for the tip, I'm getting closer

I included the variable i in my formula:

$('join___12___order___price_'+i).value =
parseFloat($('join___12___order___unitprice_'+i).value)
* parseFloat($('join___12___order___qty_'+i).value);

then if i = 0 I update the first record, if i = 1 I update the second record...

I tried to put
var i = $(this).getElement('li').get('text');
and some other variant but it does not work. I need to investigate a little bit more

or if someone as the magic code to retrieve the record id would be perfect
 
The following Wiki page might help you out if you haven't found it already: http://fabrikar.com/wiki/index.php/Form_and_element_javascript

Check out the section on Accessing All Elements in a Repeat Group. That will put all of your repeat element data into an array. It should be straight-up JS from there to perform your calculation.

Seeing as how this will take more than a couple lines of JS, I would also recommend putting your code into an X.js file (where X is the id of your form). More on this in the above link.

I did something similar (but easier). I just needed to sum up all instances of an element. BTW, I found Firebug necessary to work out the code and debug. Perhaps you could tweak the code for your needs. Here is the code:
Code:
function sumNetCash(){
    var qtys = document.getElements('input[name *= mps_bouts___cash]');
    var sum = 0;
	qtys.each(function (val) {
        sum += parseInt(form_6.formElements.get(val.id).getValue());
    });
	form_6.formElements.get('mps_events___net_cash').update(sum);
}
 
thank you for poitning me to this section of the wiki (still under Fabrik 2; i will try to add a link in the Fabrik 3 section)

However I'm still at the same place.

$(this).getElement('li').get('text');

what is 'text' for you? this is the part I don't understand. Do I have to replace it with something?
 
$(this).getElement('li').get('text');

returns the following error:
Uncaught TypeError: Cannot call method 'get' of null

I really would like it to work...
in the meantime I found a work-around but it is not well done in term of programming because I do a loop that is really unnecessary:(:

var j = $('group8').getElements('.fabrikSubGroup').length;

for (i=0;i<j;i++)
{
$('join___12___order___price_'+i).value =
parseFloat($('join___12___order___unitprice_'+i).v alue)
* parseFloat($('join___12___order___qty_'+i).value);
}

any idea to get the same result using a better way?
 
That code will work just fine. It does do some un-necessary work, in that it re-calculates the sums in all repeated groups, not just the one you changed something in, but the amount of extra work involved is sooo tiny, I wouldn't worry about it. And it also safe guards against potential corner cases where the event didn't get fired in a previous repeat.

If you really wanted to restrict it to just operating on the 'current' group, best approach (assuming you already have this code wrapped up in a function in a form_X.js of X.js file, as suggested by a previous poster) is to pass in the element which is firing the event, where you call the function form the element's JS event ...

Code:
myFunc(this);

... and define that as an input arg to the func (see code below).

Then instead of getting the repeat group count size, you can derive the specific repeat index being used by pattern matching it out of the element ID for the 'el' passed in to your func, which should give you the X on the end of the tablename___elementname_X ID. Then instead of a loop, you can use this appended to the ID's you want to use in one line ...

Code:
function myFunc(el) {
    var repeatCount = el.id.match(/_(\d+)$/)[1];
    $('join___12___order___price_'+repeatCount).value = 
parseFloat($('join___12___order___unitprice_'+repeatCount).value)
* parseFloat($('join___12___order___qty_'+repeatCount).value);
}

-- hugh
 
this work perfect! thank you Hugh

to summaryse, to get the group id in a repeat group we can use

Code:
i = this.id.match(/_(\d+)$/)[1];

I will try to update the wiki with this info
 
Yeah. It's a bit hacky, and at some point I'll probably add the repeat count as part of the actual element object, but for now ...

-- hugh
 
Status
Not open for further replies.
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top