import flash.display.MovieClip;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.MouseEvent;
import flash.events.Event;
import fl.controls.DataGrid;
import fl.controls.ComboBox;
import fl.controls.Button;
var dg:DataGrid;
var cb:ComboBox;
var urlLoader:URLLoader = new URLLoader();
var loadButton:Button;
var bookXML:XML;
setupGrid();
setupComboBox();
setupButton();
function setupGrid():void {
dg=new DataGrid();
dg.addColumn("Title");
dg.addColumn("InStock");
dg.addColumn("Price");
//This sets the size of the datagrid
dg.setSize(600,100);
//This is how many rows you want the datagrid to show
dg.rowCount=5;
//When we add colums they are put into an array
//Here we set the first column "Title" width to 450
dg.columns[0].width=450;
//This set the x and y position of the datagrid
dg.move(0,100);
addChild(dg);
}
function setupComboBox():void {
cb = new ComboBox();
//This adds item to the comboBox
cb.addItem({label: "Flash" });
cb.addItem({label: "Ajax" });
cb.addItem({label: "Php" });
//This sets the x and y positions
cb.move(200,50);
addChild(cb);
}
function setupButton():void {
loadButton = new Button();
loadButton.label = "Load Books";
loadButton.addEventListener(MouseEvent.CLICK, loadBooks);
loadButton.x = 200;
loadButton.y = 325;
addChild(loadButton);
}
function loadBooks(e:Event):void {
//Here the cb.selectedLabel returns a string so we call toLowerCase() on it
//and append .xml to it i.e. if 'Flash' is selected we load 'flash.xml'
urlLoader.load(new URLRequest(cb.selectedLabel.toLowerCase()+".xml"));
urlLoader.addEventListener(Event.COMPLETE, populateGrid);
}
function populateGrid(e:Event):void {
var booksXML:XML = new XML( e.target.data);
//How many items are in the xml file
var booksLength:int = booksXML.book.length();
//This removes all the previously added data in the datagrid.
dg.removeAll();
//Here we loop through the nodes in the xml file, and add each as a row to the datagrid
for (var i:int =0; i < booksLength; i++) {
dg.addItem({Title: booksXML.book[i].title,InStock: booksXML.book[i].instock,
Price: booksXML.book[i].price});
}
}
// Create 3 XML files in the same directory as the Fla
// with the names :
//php.xml , flash.xml , ajax.xml
// example of the xml file shema
Learning ActionScript 3.0: A Beginner's Guide
yes
26.39
Essential ActionScript 3.0
yes
34.64
Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/KsNj8EqbwzA/12217