AS3:DataGrid的基于XML的DataProvider将列无序排列

时间:2011-06-13 20:48:44

标签: xml actionscript-3 datagrid components dataprovider

我正在通过传递以下XML在AS3中创建一个名为'dataProvider'的DataProvider:

<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>

然后我将DataProvider分配给一个使用以下代码扩展DataGrid的对象:

this.dataProvider = dataProvider;

我的问题是生成的DataGrid将信息字段放在第一列中,将属性字段放在第二列中。我假设索引0处的xml元素将放在第0列中,但它们会被切换。

我知道我可以简单地进入并切换列,但这个解决方案看起来有点像黑客,我想知道我的潜在错误是什么。

如果问题不清楚,请告诉我。

1 个答案:

答案 0 :(得分:0)

可能有另一种方法可以做到这一点,但这就是我设置列的方式

dataGrid具有columns属性 将数组绑定到此属性。
这是一个DataGridColumn数组 根据需要按下标题 确保在数据提供者数据更改

之前创建列
[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;