In the previous two tutorials you have created two Actionscript 2.0 classes, Product and Shopping. This tutorial covers how to use actionscript to display the items in your Shopping cart in a DataGrid component. Because we are using a DataGrid component, you will need Macromedia Flash MX 2004 Professional for this exercise. We will be creating our own "DataGrid" componenet later on in the tutorial. Click here to skip to the custom datagrid.
First create a new flash document. Drag a DataGrid Component instance onto the stage. Give it an instance name of dataGrid. Resize the grid to be 400 X 200. Here is the code we will be using.
import com.ahfx.Product;
import com.ahfx.Shopping;
var myProduct = new Product();
myProduct.setQuantity(3);
myProduct.setPrice(2.99);
myProduct.setDesc("Teddy Bear");
var myOtherProduct = new Product();
myOtherProduct.setQuantity(1);
myOtherProduct.setPrice(12.99);
myOtherProduct.setDesc("Cool Sunglasses");
var shop_cart = new Shopping();
shop_cart.addElement(myProduct);
shop_cart.addElement(myOtherProduct);
for(i=0;i<shop_cart.size();i++){
var newProd:Product = shop_cart.getElement(i);
dataGrid.addItem(newProd);
}
The only difference between this and the intermediate tutorial is that we have added the for loop. This loop gets a Product out of the Shopping cart and stores it in a temporary Product. Then the product is added to the dataGrid with the addItem() method. The datagrid identifies the instance variables and uses them as the headings for the grid. It displays the following:
You can see that it uses the variable names for the headings. This isn´t a very pretty output, so we add some code to make the output look better.
SUPPORT THIS SITE If this tutorial helped you, show your support and send me something from my wish list. Click on an item below and then choose Adam Hayes (Gift Registry Address) for the Ship to Address. It is that easy!