PDA

View Full Version : Datagrid, Event Listener, external XML, and Images


feight
07-14-2008, 02:08 AM
I have no clue what i'm doing, but i'm having a blast!!

Basicly, here is what i'm trying to do: Take one xml file that will be updated weekly, populate that in flash to datagrid1 , Add an event listener to get the selected row, pull data out of the same xml file and populate datagrid2 with additional info about said row/selecteddata.

The only thing I don't have working is the show image in one of the cells. I understand I need to use a cell renderer? Currently i'm doing this in as2.0, but could use 3, or even flex if anyone wants to hold my hand like a baby.

var mXML:XML = new XML();

mXML.ignoreWhite = true;

mXML.onLoad = function(){
if(mXML.loaded){
//create array that will hold the movies info
var arrMovies:Array = this.firstChild.childNodes;
var i:Number = 0;

for(i; i < arrMovies.length; i++){
var movieName:String = arrMovies[i].attributes.name;
var movieYear:Number = arrMovies[i].attributes.year;
var movieGenre:String = arrMovies[i].attributes.genre;

//Hidden columns
var movieJake:String = arrMovies[i].attributes.jake;

//Let's create an associative array using an Object constructor
//this array will be used to populate our DataGrid
var item:Object = {Name:movieName, Year:movieYear, Genre:movieGenre, Jake:movieJake};

//Let's set out columns widths
dgMovies.getColumnAt(0).width = 50;
dgMovies.getColumnAt(1).width = 50;
dgMovies.getColumnAt(2).width = 50;

dgMovies.getColumnAt(0).resizable = false;
dgMovies.getColumnAt(1).resizable = false;
dgMovies.getColumnAt(2).resizable = false;
dgMovies.removeColumnAt(3);

dgMovies.addItem(item);



}

//let's create the event listener to our DataGrid object
var dgListener:Object = new Object();

dgListener.cellPress = function(eObj:Object) {

//let's retrieve column name
var colName:Object = eObj.target.columns[eObj.columnIndex].columnName;

//to retrieve cell value
var cellVal:Object = {Name:eObj.target.selectedItem.Jake, Year:eObj.target.selectedItem.Genre};
//var cellVal:Object = {data:"http://www.helpexamples.com/flash/images/image1.jpg", title:"image1.jpg"};


dgMovies2.removeAll(cellVal);


dgMovies2.addItem(cellVal);


};

//let's add the event listener to our DataGrid object
dgMovies.addEventListener("cellPress", dgListener);

}
}

mXML.load("movies.xml");


And here's the movies.xml file
<?xml version="1.0" encoding="UTF-8"?>
<movies>
<movie name="Transformers" year="2007" genre="Action" jake="http://www.helpexamples.com/flash/images/image1.jpg" />
<movie name="Pan's Labyrinth" year="2006" genre="Fantasy" jake="test2" />
<movie name="The Commitments" year="1991" genre="Comedy" jake="test3" />
<movie name="1409" year="2007" genre="Horror" jake="test4" />
<movie name="Hero" year="2004" genre="Action" jake="test5" />
<movie name="Braveheart" year="1995" genre="Epic" jake="test7"/>

</movies>


Feel free to email me! thejkfarr@yahoo.com

These are a hodge podge of examples I found on the web btw. It's amazing I got them to work at all. I'm a complete novice!