AS3: XML Based DataProvider for DataGrid puts columns out of order
I'm creating a DataP开发者_C百科rovider called 'dataProvider' in AS3 by passing it the following XML:
<GameInfo>
<item>
<attribute>name</attribute>
<info>default0</info>
</item>
<item>
<attribute>type</attribute>
<info>Abe</info>
</item>
<item>
<attribute>health</attribute>
<info>100</info>
</item>
<item>
<attribute>frame</attribute>
<info>1</info>
</item>
<item>
<attribute>through</attribute>
<info>true</info>
</item>
<item>
<attribute>time</attribute>
<info>2</info>
</item>
</GameInfo>
I then assign the DataProvider to an object that extends DataGrid with the following code:
this.dataProvider = dataProvider;
My problem is that the resulting DataGrid puts the info field in the first column, and the attribute field in the second column. I assumed the xml element at index 0 would be placed in column 0, but they are switched.
I know I can simply go in and switch the columns but that solution seems like a bit of a hack and I'd like to figure out what my underlying mistake is.
Please let me know if the question is unclear.
There might be another way to do this but this is how I set the columns
dataGrid has a columns attribute.
Bind an Array to this attribute.
This is an Array of DataGridColumn
Push the headers as you need in order
Make sure the columns are created before the dataprovider Data changes
[Bindable]
public var aColumns:Array;
var myAttribute:DataGridColumn = new DataGridColumn( );
myAttribute.dataField = 'attribute';
myAttribute.visible = true;
// measure the width of the text to make it pretty
var label:TextField = new TextField();
label.text = 'attribute';
var metrics:TextLineMetrics = label.getLineMetrics(0);
myAttribute.width = metrics.width + 30;
var myInfo:DataGridColumn = new DataGridColumn( );
myInfo.dataField = 'info';
myInfo.visible = true;
// measure the width of the text to make it pretty
var label:TextField = new TextField();
label.text = 'info';
var metrics:TextLineMetrics = label.getLineMetrics(0);
myInfo.width = metrics.width + 30;
aColumns.push( myAttribute);
aColumns.push( myInfo);
this.columns = aColumns;
精彩评论