Page 1 of 2

calculating field with JS

Posted: Wed Sep 03, 2014 8:05 pm
by Craig Rowland

Been reading up on JS. Whilst I get the basics for example the below code to sum two variables

http://jsfiddle.net/g7zz6/

I don't understand the relationship with the appery "bits".

For example. I have two fields. but I want to multiply them together and get a third field based on a rule.

I get the logic of the code snippet quoted above but

1)how do I get the variable names (is it just the class name)
2)where do I specify the JS.
3)Do i make server code?
4)Do I make a local storage variable and put code there.

Im confused, im just not quite sure where to start. I read the similar questions and responses but they are a step ahead of where I am so don't help me at this stage. No doubt I will refer back to them though


calculating field with JS

Posted: Wed Sep 03, 2014 8:41 pm
by Yurii Orishchuk

Hi Craig,

Here is main things you have to know:

1 user can see values in components(for example label)

2 each component in Appery.ioi has name. See details where you can find it this name: http://prntscr.com/4je1kq/direct

3 To get value from two labels, make calculation and set result to the third label you can use following code:

pre

//Note: you need to replace "firstLabelName" with your component name.
var firstValue = parseInt(Apperyio("firstLabelName").text());

//Note: you need to replace "secondLabelName" with your component name.
var secondValue = parseInt(Apperyio("secondLabelName").text());

//Calculation.
var result = firstValue * secondValue;

//Set result to the result label.
//Note: you need to replace "resultLabel" with your component name.
Apperyio("resultLabel").text(result);

/pre

That's all. On this code base you can create your own logic you need.

Regards.


calculating field with JS

Posted: Wed Sep 03, 2014 8:43 pm
by Craig Rowland

thanks for this. WIll give it a try :-)


calculating field with JS

Posted: Wed Sep 03, 2014 8:45 pm
by Craig Rowland

Last question. Where does the code go?


calculating field with JS

Posted: Wed Sep 03, 2014 9:08 pm
by Yurii Orishchuk

You can use this code in any JS event handler.

For example on button click:

See details: http://prntscr.com/4jeenb/direct

It's best way to understand how to work with this tool to pass this tutorial: http://devcenter.appery.io/tutorials/...

Regards.


calculating field with JS

Posted: Thu Sep 04, 2014 8:12 pm
by Craig Rowland

Thank you very much for your help


calculating field with JS

Posted: Thu Sep 25, 2014 9:45 pm
by Craig Rowland

Rather than start a new thread. This didn't work

I did this

On Load create a local variable called AnnualPrem
Used the JS above to multiply the labels PremiumFreq * InputPremium to creat a result called AnnalPrem

Am I missing something?

I couldn't find any relevance in the linked tutorial sorry but i looked at this one http://devcenter.appery.io/documentat...

following those instructions didn't yield any result either

To test this I'm trying to get the field

AnnualPrem to populate on clicking the SAVE button. It would be ideal.


calculating field with JS

Posted: Fri Sep 26, 2014 3:37 am
by Yurii Orishchuk

Hi Craig,

Please give us your app public link and describe steps to reproduce your problem.

We will take a look.

Thanks & regards.


calculating field with JS

Posted: Tue Mar 03, 2015 6:19 pm
by Benoit

Hello,

I have another problem regarding field calculation. I would like to make the sum of label fields. The total is only equal to one field. I wrote the following script where total_price is the label name of each cart lines. Total TTC should be 14.7 and not 8.5.

Script :

var sum =0;

Apperyio("total_price").each(function(){
var total_price = parseFloat($( this ).text());
sum += total_price;
Apperyio("gran_total_price").text(sum);
});

Thanks for your support.

Benoit

Image


calculating field with JS

Posted: Wed Mar 04, 2015 5:01 am
by Yurii Orishchuk

Hi Sauvage,

Please try following script instead of yours:

pre

var sum =0;

var items = jQuery('[name="total_price"]');

items.each(function(){
var total_price = parseFloat($( this ).text());
sum += total_price;
});
Apperyio("gran_total_price").text(sum);

/pre

Regards.