I have modified the app using Alex Harui's solution and it works when i add a
product to the cart and also updates the total when i delete an item from the
cart. But when i edit the quantity in the text input, the total does not get
updated. Below is the modified version. Anything am missing?
?xml version=1.0 encoding=utf-8?
mx:Application xmlns:mx=http://www.adobe.com/2006/mxml;
layout=horizontal
xmlns:ns1=*
xmlns:ns2=as_logic.*
mx:states
mx:State name=dark
mx:SetProperty target={product}
name=text
value=Dark Chocolate/
mx:SetProperty target={price}
name=text
value=50/
/mx:State
mx:State name=spread
mx:SetProperty target={product}
name=text
value=Drinking
Chocolate/
mx:SetProperty target={price}
name=text
value=100/
/mx:State
/mx:states
mx:Script
![CDATA[
import mx.events.FlexEvent;
import mx.controls.listClasses.ListData;
import mx.collections.ArrayCollection;
import mx.events.CollectionEvent;
[Bindable]
private var orderColl:ArrayCollection=new
ArrayCollection();
private function addProduct():void
{
/*** Create an object to hold the data ***/
var obj:Object=new Object();
/*** Assign the variables to it ***/
obj.Product=product.text;
obj.Price=price.text;
obj.Qty=1;
/*** Add the object to the list ***/
orderColl.addItemAt(obj, 0);
orderColl.addEventListener(CollectionEvent.COLLECTION_CHANGE, calculateSum);
}
public function deleteOrder():void
{
/*** Remove the item from the array collection
***/
orderColl.removeItemAt(products.selectedIndex);
orderColl.addEventListener(CollectionEvent.COLLECTION_CHANGE, calculateSum);
}
public function changeQty(event:Event):void
{
orderColl.addEventListener(CollectionEvent.COLLECTION_CHANGE, calculateSum);
}
public function calculateSum(event:CollectionEvent):void
{
var amt:Number=0;
var n:int=orderColl.length;
for (var i:int=0; i n; i++)
{
var
cartEntry:Object=orderColl.getItemAt(i);
amt+=cartEntry.Qty * cartEntry.Price;
}
sum.text=usdFormatter.format(amt.toString());
}
]]
/mx:Script
mx:DefaultTileListEffect id=dtle0
fadeOutDuration=300
fadeInDuration=300
moveDuration=650
color=0xff/
mx:CurrencyFormatter id=usdFormatter
precision=0
currencySymbol=$
alignSymbol=left/
mx:Canvas width=500
height=300
mx:Label x=10
y=10
text=Milk Chocolate
id=product/
mx:Label x=10
y=36
text=10
id=price/
mx:Button x=10
y=62
label=submit