PDA

View Full Version : DataGrid Component loading external file? PLEASE HELP



Beany
June 4th, 2007, 03:27 AM
This is the 3rd forum i've posted this on now lol. No one can or will help me with this.. and it dont even seem that hard!

So PLEASE help,

I've created a list of rankings for top scores using the datagrid component, which you can see here, (http://error1.com/stats/ranks.html) and at the moment the data for the rows/columns listed within the dataGrid Component are all in the AS3 code (below), but i dont want it in the AS3 code, i want that code in a external file (dont know what kinda file to use for this?) which the DataGrid Component would load from and display.

Heres the AS3 code:

import fl.controls.DataGrid;
import fl.events.DataGridEvent;
import fl.controls.ScrollPolicy;
import fl.data.DataProvider;
import flash.net.navigateToURL;

var i:uint;
var totalRows:uint = 5;
var dp:DataProvider = new DataProvider();
dp.addItem({Rank:"1", Name:"lightspeed", Time:"13.219", CPU_Type:"C2D E6600", CPU_MHz:"3887 MHz", RAM:"4096MB DDR2", RAM_MHz: "864 MHz"}),
dp.addItem({Rank:"2", Name:"Beany", Time:"13.766", CPU_Type:"C2D E6600", CPU_MHz:"3700 MHz", RAM:"2048MB DDR2", RAM_MHz: "1235 MHz"}),
dp.addItem({Rank:"3", Name:"Timmaah", Time:"13.984", CPU_Type:"C2D E6600", CPU_MHz:"3627 MHz", RAM:"2048MB DDR2", RAM_MHz: "1005 MHz"}),
dp.addItem({Rank:"4", Name:"Darth Vader", Time:"14.000", CPU_Type:"C2D E6600", CPU_MHz:"3600 MHz", RAM:"1024MB DDR2", RAM_MHz: "1000 MHz"}),
dp.addItem({Rank:"5", Name:"ik0n", Time:"14.181", CPU_Type:"C2D E6600", CPU_MHz:"3600 MHz", RAM:"2048MB DDR2", RAM_MHz: "800 MHz"}),


var dg:DataGrid = new DataGrid();
dg.setSize(576, 425);
dg.move(20, 112);
dg.sortableColumns = false;
dg.columns = ["Rank", "Name", "Time", "CPU_Type", "CPU_MHz", "RAM", "RAM_MHz"];
dg.columns[0].width = 45;
dg.columns[1].width = 136;
dg.columns[2].width = 65;
dg.columns[3].width = 110;
dg.columns[4].width = 80;
dg.columns[5].width = 100;
dg.columns[6].width = 80;

dg.dataProvider = dp;

addChild(dg);
dg.addEventListener(Event.CHANGE, topTen);


function topTen(event:Event) {


if (event.currentTarget.selectedItem.Rank == 1) {
gotoAndStop(11);
}
if (event.currentTarget.selectedItem.Rank == 2) {
gotoAndStop(12);
}
if (event.currentTarget.selectedItem.Rank == 3) {
gotoAndStop(13);
}
if (event.currentTarget.selectedItem.Rank == 4) {
gotoAndStop(14);
}
if (event.currentTarget.selectedItem.Rank == 5) {
gotoAndStop(15);
}
if (event.currentTarget.selectedItem.Rank == 6) {
gotoAndStop(16);
}
if (event.currentTarget.selectedItem.Rank == 7) {
gotoAndStop(17);
}
if (event.currentTarget.selectedItem.Rank == 8) {
gotoAndStop(18);
}
if (event.currentTarget.selectedItem.Rank == 9) {
gotoAndStop(19);
}
if (event.currentTarget.selectedItem.Rank == 10) {
gotoAndStop(20);
}
if (event.currentTarget.selectedItem.Rank == 11) {
gotoAndPlay(21);
}
if (event.currentTarget.selectedItem.Rank == 12) {
gotoAndStop(22);
}
if (event.currentTarget.selectedItem.Rank == 13) {
gotoAndStop(23);
}
if (event.currentTarget.selectedItem.Rank == 14) {
gotoAndStop(24);
}
if (event.currentTarget.selectedItem.Rank == 15) {
gotoAndStop(25);
}
if (event.currentTarget.selectedItem.Rank == 16) {
gotoAndStop(26);
}
if (event.currentTarget.selectedItem.Rank == 17) {
gotoAndStop(27);
}
if (event.currentTarget.selectedItem.Rank == 18) {
gotoAndStop(28);
}
if (event.currentTarget.selectedItem.Rank == 19) {
gotoAndStop(29);
}
if (event.currentTarget.selectedItem.Rank == 20) {
gotoAndStop(30);
}
}

The reason i want this is because i'm trying to create a submit form so people can post there scores themselfs into the rankings, rather than me always having to update it manually, and i want the scores to be posted to an external file (like .txt .as, .xml, no idea which to use??) Then the DataGrid Component would load the submitted scores from the external file.

I'm rubbish with AS3 so could someone please edit the above code to what is needed? Or explain in detail what i need to do? Because most of time people reply saying do this and that, and i've no idea what there on about because i'm not good with AS in general...
And i can put up the .FLA for download if needed...

Thanks!

mathew.er
June 6th, 2007, 05:34 AM
The simplest way is to use a XML file. It can have a structure like this

<items>
<item Rank="1" Name="lightspeed" Time="13.219" CPU_Type="C2D E6600" CPU_MHz="3887 MHz" RAM="4096MB DDR2" RAM_MHz="864 MHz">
<item Rank="2" Name="lightspeed" Time="13.219" CPU_Type="C2D E6600" CPU_MHz="3887 MHz" RAM="4096MB DDR2" RAM_MHz="864 MHz">
<item Rank="3" Name="lightspeed" Time="13.219" CPU_Type="C2D E6600" CPU_MHz="3887 MHz" RAM="4096MB DDR2" RAM_MHz="864 MHz">
</items>
and so on...

You can change the code to load the XML file and then use its content as the data provider.

var xmlLoader : URLLoader = new URLLoader ();
xmlLoader.addEventListener ( Event.COMPLETE, xmlLoaded );
xmlLoader.load ( new URLRequest ( 'path/to/the.xml' ) );

function xmlLoaded ( e : Event ) : void
{
var items : XMLList = XML ( e.target.data ).item
dg.dataProvider = items;
}
Now I'm not sure, if you won't need to have the xml structure like
<items>
<item>
<Rank>1</Rank>
<Name>...</Name>
etc.
</item>
</itmes>But I didn't work with this so much so there might be other folks that will give you an exact answer.

Charleh
June 6th, 2007, 07:27 AM
Well I don't know about loading into a grid as I did it a while ago in AS2.0 via a database connection using an ASP page to get the new scores but you could cut down a lot of your code for the topTen function...



function topTen(event:Event) {

if (event.currentTarget.selectedItem.Rank <= 20) {
gotoAndStop(event.currentTarget.selectedItem.Rank + 10);
}

}


Basically my old code would send Flash to an URL after a player had got a top score and posted their score into the database via an ASP page, then the flash would load the new scores by calling an ASP page which created a load of POST variables I think, thought I can't quite remember

Anyway you are probably better off writing an XML file instead like mathew.er said!