Andrew Clark
Posts: 0
Joined: Sun Jan 05, 2014 1:26 pm

Sum dollar values in al list top get a total.

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

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

Sum dollar values in al list top get a total.

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

Andrew Clark
Posts: 0
Joined: Sun Jan 05, 2014 1:26 pm

Sum dollar values in al list top get a total.

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

Serhii Kulibaba
Posts: 150
Joined: Tue Aug 27, 2013 1:47 pm

Sum dollar values in al list top get a total.

Hello Andrew,

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

Bruce Stuart
Posts: 0
Joined: Fri Oct 24, 2014 4:42 am

Sum dollar values in al list top get a total.

Hi Andrew,

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

Bruce

Return to “Issues”