PDA

View Full Version : [AS2] datagrid dynamic column names/values


zealouse
03-05-2009, 02:15 AM
The code works but.... only if INDEX 0 has all of the column labels :o
what do I do if I dont know the column names in advance?

Example:

Index 0

Column: A B
Value: 1 1

Index 1 !!! this will not work since C wasnt in Index 0 :(

Column: A B C
Value: 2 2 2


//declare vars
///data dpovider array
var dp:Array = new Array();
var return_format:Array = new Array();
///split array
var formated:Array;
///onject to store values
var Obj:Object = new Object();

//value string
var myInfo:String = 'A1 B1'+('\r\n')+'A2 B2 C2';

//split at ' ' new array
return_format = myInfo.split('\r\n');

///format between nextline
for (var i = 0; i<return_format.length; i++) {

//split at ' ' new array
formated = return_format[i].split(' ');

//clear object
Obj = {};

///Pull out each letter for 'Type' and set value 'types:Value'
for (var j = 0; j<formated.length; j++) {

///Type
Types = formated[j].slice(0, 1);
//trace(Types)

//value
Value = formated[j].slice(1, formated[j].length);

//Type:value to objects
Obj[Types] = Value;
}

///push object
dp.push(Obj);

}
//!!!!have to have the column!!!
dp.unshift({A:'', B:'', C:''});

///send to dataprovider
Data_Grid.dataProvider = dp;

eddiewireless
03-05-2009, 03:22 AM
Don't you just love the DataGrid...:p

I am not sure i understand what you are trying to do...

you can get the header names and cell values using a recursive function:

for (var r:Number = 0; r < Data_Grid.length; r++) {
var c:Number = 0;
for (var j in Data_Grid.getItemAt(r)) {
trace("Columnc: " + j);
trace("row: " + r + " column:" + c + " data: '" + Data_Grid.getItemAt(r)[j] + "'");
c++;
}
}


now if you want to get a cell item using the gird values, like (row,colum), you can do something like this:
function getItem(row:Number, column:Number):String {
//counter to keep up with the columns
var c:Number = 0;
//j contains the column name!
for (var j in Data_Grid.getItemAt(row)) {
if (c == column) {
return String(Data_Grid.getItemAt(row)[j]);
}
c++;
}
}
//usage
var my_value:String = getItem(2, 2);

zealouse
03-05-2009, 03:40 AM
eddiewireless thank you for the replay :) yeah the dataGrid is being a pain...the list box has not issue with it :o

Actually I am trying to populate the data grid rather then retrieve values...sorry about that I had a difficult time describing it.

It seems if I don’t declare the datagrids column names at the start I can’t create them later.

If you have a sec to check the file that would be great!


http://alphagraphicdesigns.com/help/Column_names.zip

eddiewireless
03-05-2009, 04:13 AM
i can't open that file for some reason...

as far as changing header text it can be done at any time during your program:

DataGridInstance.getColumnAt(index).headerText = "new header name";


if you use the datagrid a lot, you should take a look at this extension, it gives you a lot of additional options for datagrid control, best 5 bucks i ever spent:
Advanced Datagrid (http://www.tufat.com/s_macromedia_flash_datagrid_extensions.htm)

zealouse
03-05-2009, 04:35 AM
Ooops I resaved and posted the file as CS3 format....going to the Advanced Datagrid site now.

Jennysaur
07-07-2009, 06:56 PM
Can't seem to use anything but straight text in the dataGrid array. Any variables or alternate wording returns an error. Is there a solution to this?

my_datagrid.addItem({someVariable:"item data"});