How to create total price while using Array? (SOLVED)


#1

Hi All.

First of all, you can check my codepen here : http://codepen.io/harked/pen/NqXowq

I want to create TOTAL PRICE which is total/ summary price of all food in the list. When User click delete button on each item, it also will change the TOTAL PRICE.

Anyone can help/give advice? it would be greatly appreciated.

Thanks.


#2

add a function, that calculates the totalprice

Keep in mind you have to format the output data like you want --> calculation only works with numbers.
If i need to format money and different currencies i am using http://openexchangerates.github.io/accounting.js/.

So i only format the output values and can work with numbers in my controller.


#3

Great Thanks, @bengtler!
It works with my app.


#4

Hi @bengtler, in this case,
what if i want 2 digits after decimal place?
i.e : I want it display $10.00 instead of $10.

is it possible? Could you give the advice?
Code : http://codepen.io/anon/pen/rVpRLK

Thanks :smiley:


#5

like i said… use thirdparty lib accounting.js
or write an own formatter.

ugly and fast solution use toFixed-method


#6

Alright @bengtler.
Many thanks for your kind help!


#7

Hi @bengtler.
I have another case in same topic : calculating price in Ionic.

You can take a look at this codepen : http://codepen.io/harked/pen/gpvdNK

There is initial price worth: $10.00 in the total price.
When user click on (+) button to add item, i.e : LAKSA (price $4.5), the price will be updated to : $14.50 (and the button will be change to (-) minus with red background).
and then When user add BURGER (price $2.5), the price will be updated to : $17.00.

then, when user click on the LAKSA again, the price will be decrease to $12.5.
and so on … that’s happen with the all item.

I’ve found another code here : http://codepen.io/harked/pen/KpQrMW
which is looks like my case. But i get stuck on how to implement this with my case.

Do you have any clue/advice, @bengtler?


#8

you have your ondelete function --> if clicked and our item is not added --> add the price you want, if it is added --> subtract the base price.

Store the baseprice of the product in a special key on the item.object.


#9

Thanks @bengtler!
for anyone who has the same case, here i share the code snippet :

$scope.onClickAdd = function(item){
    console.log(item.added);
    if (!item.added)
      $scope.initialPrice+=item.price
      else
        $scope.initialPrice-=item.price
    item.added = !item.added;
  };