all groups > flash (macromedia) > august 2003 >
You're in the

flash (macromedia)

group:

Dynamically naming columns in a datagrid


Dynamically naming columns in a datagrid cbg3
8/6/2003 11:36:31 PM
flash (macromedia):
I want to populate the columns of a datagrid by grabbing the attribute names and values from an XML node. The node attribute names will be the datagrid column names, and the node attribute values will be the datagrid column values. The trouble is, it seems that you have to hard-code the column names into the data for a row. For example, I want to do something like:

// creates a row of table data for the given node
function setTableRowData(node)
{
var str = "{";
for (attr in node.attributes)
{
str += attr + ":" + "\"" + node.attributes[attr] + "\",";
}
str = str.substring(0, str.lastIndexOf(",")); // chop off trailing comma
str += "}";
my_grid.attItem(str);
}

What I'm getting is the value of the str variable passed to the addItem method in one column of the table, with "label" as the column heading.

If it's not possible to dynamically set the column names from XML data using this or another method, it would seem a pretty serious shortcoming of the DataProviderClass. But maybe I'm missing something?

--Ben
// creates a row of table data for the given node
function setTableRowData(node)
{
var str = "{";
for (attr in node.attributes)
{
str += attr + ":" + "\"" + node.attributes[attr] + "\",";
}
str = str.substring(0, str.lastIndexOf(",")); // chop off trailing comma
str += "}";
my_grid.attItem(str);
}

Re:Dynamically naming columns in a datagrid cbg3
8/10/2003 2:28:36 AM
Anyone? Anyone? Bueller?


Re:Dynamically naming columns in a datagrid blenz
8/10/2003 5:51:48 AM
manually create your columns one by one then you can set the name of the column (column header).


look into FGridColumn object. The help file of the FDataGrid have samples of how it is done



Re:Dynamically naming columns in a datagrid cbg3
8/10/2003 2:31:37 PM
Hello blenz, and thanks for the reply. I may not have made clear what I'm talking about. I'm not talking about setting the text that appears in the column header when using the setColumns method, but the the name part of the name-value pair that makes up the data in the column (for example: {firstName:"Ben", mood: "Pensive"}). I'm talking about "firstName" and "mood" in this example. Those are the items that must be hard-coded when writing out data from an XML file. But again, maybe I'm missing something?


Re:Dynamically naming columns in a datagrid blenz
8/10/2003 7:31:28 PM
you mean every row would have a different "name" in the name value pair?

i.e

{firstName:"Ben", mood: "Pensive"} //first row
{lastName:"Jones", status: "active"} //second row


if that is the case, not possible


Re:Dynamically naming columns in a datagrid cbg3
8/11/2003 12:39:34 AM
Noooo.... the names of the variables would be the same for each row, but you might not know what they are at compile time, only at run time, when your program reads the XML file. That's where the dynamic part comes in.

Re:Dynamically naming columns in a datagrid blenz
8/11/2003 1:23:35 AM
what I can think of is build first all your rows into one array and then assign the data to the datagrid using the setDataProvider method instead of adding them one by one through the addItem emthod. You are on the right track already with your setTableRowData : I assume you are looping through the xml calling setTableRowData over and over to build your grid :

say :
function buildGrid()
{
objArray = new Array()
for(i=0;i<dataNode.childNodes.length;i++)
{
rowData=setTableRowData(dataNode.childNodes);
objArray.push(rowData);
}
mygrid.setDataProvider(objArray);
}




function setTableRowData(node)
{

var str = "{";
for (attr in node.attributes)
{

str += attr + ":" + "\"" + node.attributes[attr] + "\",";
}
str = str.substring(0, str.lastIndexOf(",")); // chop off trailing comma
str += "}";

return str;

}


Re:Dynamically naming columns in a datagrid blenz
8/11/2003 5:38:26 PM
the problem with the above code is that addItem expects a dataItem object which is an array object (actually a multidimensional array);
the presence of curly braces "{ }" indicates it is an array object.

What you built is a String object with the curly braces part of the string.

I have not investigated how to properly fill the datagrid using the principle of what you are trying to do. I can understand what you are trying to accomplish though

AddThis Social Bookmark Button