Craig Rowland
Posts: 0
Joined: Mon Sep 01, 2014 9:24 pm

calculating field with JS

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

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

calculating field with JS

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.

Craig Rowland
Posts: 0
Joined: Mon Sep 01, 2014 9:24 pm

calculating field with JS

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

Craig Rowland
Posts: 0
Joined: Mon Sep 01, 2014 9:24 pm

calculating field with JS

Last question. Where does the code go?

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

calculating field with JS

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.

Craig Rowland
Posts: 0
Joined: Mon Sep 01, 2014 9:24 pm

calculating field with JS

Thank you very much for your help

Craig Rowland
Posts: 0
Joined: Mon Sep 01, 2014 9:24 pm

calculating field with JS

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.

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

calculating field with JS

Hi Craig,

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

We will take a look.

Thanks & regards.

Benoit
Posts: 0
Joined: Fri Dec 19, 2014 3:26 pm

calculating field with JS

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

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

calculating field with JS

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.

Return to “Issues”