Hi Flexers!!!
I was wondering if anyone can help me with my code. I'm
trying to
create a shopping cart, but I'm having trouble on the addToCart()
and
addItem() functions on my actionscript. I'm new to flex and not
the
best actionscriptist so any advice would be greatly appreciated!
I
want to add the product details (name, quantity, and price) from my
xml
document ("catalog.xml") to the the datagrid in the shopping cart
when
the user clicks the cart button "(cartBtn)." This has been
driving me nuts
for the past week, PLEASE HELP! :)
Thanks
Sal
<?xml
version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
xmlns:local="*"
creationComplete="srv.send()">
<mx:Script>
<![CDATA[
public
function addToCart(product:Object):void{
cartView.addItem({name:
product.name, qty: 1, price: product.price});
}
]]>
</mx:Script>
<mx:HTTPService id="srv"
url=""
useProxy="false"
result="(srv.lastResult.catalog.product)"/>
<mx:Canvas
x="0" y="0" width="100%" height="100%">
<mx:HBox x="0" y="0"
width="100%" height="100%"
horizontalAlign="center"
verticalAlign="middle">
<mx:Panel id="catalogPanel"
layout="absolute" width="780"
height="100%" shadowDirection="left"
shadowDistance="6"
title=".:Browse:." fontSize="11"
fontWeight="bold" moveEffect="Move">
<mx:Canvas
id="boxesCanvas" width="100%" height="100%">
<!--Tile
View-->
<mx:VBox width="100%" height="100%"
horizontalAlign="right">
<mx:TileList
dataProvider="{srv.lastResult.catalog.product}"
height="100%"
width="100%" itemRenderer="thumbnail"
id="productTileList"
rowCount="3" columnCount="4"
dragEnabled="true"/>
</mx:VBox>
</mx:Canvas>
</mx:Panel>
<local:ShoppingCart
id="cartView" width="200"
height="50%"
purchase="addToCart(event)"/>
</mx:HBox>
</mx:Canvas>
</mx:Application>
<!--Shopping
Cart Component-->
<?xml version="1.0"
encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Metadata>
[Event("purchase")]
</mx:Metadata>
<mx:Script>
<![CDATA[
public
function
addItem(item:Object):void{
dg.addChild(item.data);
}
]]>
</mx:Script>
<mx:Panel
width="100%" height="100%" layout="absolute"
title="Shopping
Cart">
<mx:DataGrid x="0" y="0" id="dg" width="100%"
height="100%"
dropEnabled="true">
<mx:columns>
<mx:DataGridColumn
headerText="Product" dataField="name"/>
<mx:DataGridColumn
headerText="Quantity" dataField="qty"/>
<mx:DataGridColumn
headerText="Price"
dataField="price"/>
</mx:columns>
</mx:DataGrid>
</mx:Panel>
</mx:Canvas>
<!--Thumbnail
Component-->
<?xml version="1.0"
encoding="utf-8"?>
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml"
width="100%"
height="100%">
<mx:Metadata>
[Event("purchase")]
</mx:Metadata>
<mx:VBox
xmlns:mx="http://www.adobe.com/2006/mxml"
backgroundAlpha="0"
borderStyle="none" width="75%"
height="100%"
verticalAlign="middle" verticalGap="0"
verticalScrollPolicy="off"
horizontalAlign="center"
>
<mx:Image id="img" height="115" width="130"
source="../{data.image}"
verticalAlign="middle"
horizontalAlign="center"/>
<mx:Label
text="{data.part}" fontWeight="bold"
fontSize="10"/>
<mx:Label text="{data.manufacturer}"
fontWeight="bold"
fontSize="8"/>
</mx:VBox>
<mx:VBox width="25%"
height="100%"
horizontalAlign="center"
verticalAlign="middle">
<mx:Button
id="cartBtn" width="40" label="Add" toolTip="Add To
Cart"
click="dispatchEvent(new
Event('purchase'))"/>
</mx:VBox>
</mx:HBox>