Page 1 of 1

Sum dollar values in al list top get a total.

Posted: Mon Mar 27, 2017 1:48 pm
by Andrew Clark

I wish to sum a list of dollars amounts and get a total amount of dollar. I realise this is very simple but I can't seem to get it right and its very frustrating.

What I would like is an example of js script to know how this is achieved in appery.

Below is screen shot of page of page. You can see list of items with dollar amounts and below that a total. Below that is a screen shot of the mapping of the page where data is.

Any assistance would be very warmly greeted.

thanks in advance

Andrew

Image

Image


Sum dollar values in al list top get a total.

Posted: Mon Mar 27, 2017 3:00 pm
by Bruce Stuart

Andrew,

In a success event - the 'data' comes back from the API / Server call - you will see it as a parameter.

I suggest never putting a lot of code into the success event on the UI - since mappings come and go - and you don't want to lose your code.

Therefore:

in a success event - a call to a function ('runJavascript' from the mapping editor that executes BEFORE YOUR MAPPING EVENT) :

fComputeTotal( data );

In a new or existing Javascript asset (for each project our coding standard is - one Javascript file called myProjectNamePageSupportFunctions - so there is my suggestion of the day ) enter this code:

function fComputeTotal( oInboundData ){
// Objective : Sum that data that came back from our API call -
// loop through the data that came back... that was sent in the parameter...
//
// Usage: fComputeTotal( oInboundData ) - where oInboundData is data coming
// back from our Appery service collection list_plan_service
//
// Returns: True
//////////////////////////////////////////////////////////////////////////

var nCounter ;
var nLength = oInboundData.length ;
var nTotal = 0
for ( nCounter = 0, nCounter < nLength , nCounter++ ) {
nTotal += oInboundData[ nCounter ].dollars ;
console.log('The running total is: ' , nTotal ) ;
}
localStorage.nTotal = nTotal
return true ;
}

Furthermore - you should:

  1. Create a local storage variable called nTotal

  2. In the mapping event AFTER that Javascript event - map the localStorage variable to the UI dollars.

    There are more likely 'elegant' solutions - but this is straightforward and the approach works every time. I vote for simplicity - and this is it. Apology in advance for any typos.

    Best,

    Bruce


Sum dollar values in al list top get a total.

Posted: Tue Mar 28, 2017 2:50 am
by Andrew Clark

Bruce

Thanks for this. Whats the best way for me to contact you re consultation advice. I tried your linked in account on site but it did not connect.

Thanks in advance

Andrew


Sum dollar values in al list top get a total.

Posted: Tue Mar 28, 2017 5:34 am
by Serhii Kulibaba

Hello Andrew,

Bruce is right, please use JS code above on the service "success" mapping


Sum dollar values in al list top get a total.

Posted: Tue Mar 28, 2017 5:37 am
by Andrew Clark

Thanks.


Sum dollar values in al list top get a total.

Posted: Tue Mar 28, 2017 1:24 pm
by Bruce Stuart

Hi Andrew,

Love to help ... a href="mailto:Bruce.stuart@the-software-studio.com" rel="nofollow"Bruce.stuart@the-software-studio.com/a

Bruce