如何访问dataProvider中的子对象
How to access subobjects in dataProvider
当我有一个带有数据提供者的数据网格时,我如何访问特定列的子对象?我正在使用 Adobe Flash Builder 4.6。
我的数据样本(为简单起见,在 JSON 中):
{
"Result":[
{
"Id":3644,
"Amount":50,
"SomeTimeFrom":null,
"SomeTimeTo":null,
"Interval":{
"DateTimeFrom":"0001-01-01T00:00:00Z",
"DateTimeTo":"0001-01-01T00:00:00Z"
}
},
{
"Id":3645,
"Amount":50,
"SomeTimeFrom":null,
"SomeTimeTo":null,
"Interval":{
"DateTimeFrom":"0001-01-01T00:00:00Z",
"DateTimeTo":"0001-01-01T00:00:00Z"
}
}
]
}
我的看法:
<mx:DataGrid id="dgvMain" width="100%" height="100%" editable="false" dataProvider="{_data}">
<mx:columns>
<mx:DataGridColumn headerText="L_ID" dataField="Id"/>
<mx:DataGridColumn headerText="L_TIME_FROM" textAlign="center" labelFunction="formatTimeFromUtc" dataField="SomeTimeFrom"/>
<mx:DataGridColumn headerText="L_TIME_TO" textAlign="center" labelFunction="formatTimeFromUtc" dataField="SomeTimeTo"/>
<mx:DataGridColumn headerText="L_DATETIME_FROM" textAlign="center" labelFunction="formatDateFromUtc" dataField="Interval.DateTimeFrom"/>
<mx:DataGridColumn headerText="L_DATETIME_TO" textAlign="center" labelFunction="formatDateFromUtc" dataField="Interval.DateTimeTo"/>
<mx:DataGridColumn headerText="L_AMOUNT" dataField="Amount"/>
</mx:columns>
</mx:DataGrid>
基本上,除了“DateTimeFrom”和“DateTimeTo”(在 Interval 下)之外,我的所有列都正确呈现。我是否错误地访问了它们?
我设法自己弄明白了。我创建了一个新的标签函数 a
protected function formatDateFromUtcObject(data:Object, column:Object):String //convert from utc to user date
{
if (data != null)
{
// Check if it's a complex object with properties
var columns:Array = column.dataField.split('.');
var date:String = "";
if (columns.length > 1)
{
date = data[columns[0]][columns[1]]; // I specifically know that my object has a subobject [0] with properties [1])
}
else
{
date = data[column.dataField]; // Else, access the direct property, since I am not trying to access a subobject
}
if (date != null && date != "")
{
var parsedDate:Date = DateTimeFromUtc(Util.invariantFormatter.parseDate(date)); // My case was date that I tried to parse, so that covers the format
if (parsedDate == null)
return "";
else
return Util.formatDate(parsedDate)
}
else
{
return "";
}
}
else
{
return "";
}
}
之后,我在模板代码中调用了label函数:
<mx:DataGridColumn headerText="L_DATETIME_FROM" textAlign="center" labelFunction="formatDateFromUtcObject" dataField="Interval.DateTimeFrom"/>
<mx:DataGridColumn headerText="L_DATETIME_TO" textAlign="center" labelFunction="formatDateFromUtcObject" dataField="Interval.DateTimeTo"/>
当我有一个带有数据提供者的数据网格时,我如何访问特定列的子对象?我正在使用 Adobe Flash Builder 4.6。
我的数据样本(为简单起见,在 JSON 中):
{
"Result":[
{
"Id":3644,
"Amount":50,
"SomeTimeFrom":null,
"SomeTimeTo":null,
"Interval":{
"DateTimeFrom":"0001-01-01T00:00:00Z",
"DateTimeTo":"0001-01-01T00:00:00Z"
}
},
{
"Id":3645,
"Amount":50,
"SomeTimeFrom":null,
"SomeTimeTo":null,
"Interval":{
"DateTimeFrom":"0001-01-01T00:00:00Z",
"DateTimeTo":"0001-01-01T00:00:00Z"
}
}
]
}
我的看法:
<mx:DataGrid id="dgvMain" width="100%" height="100%" editable="false" dataProvider="{_data}">
<mx:columns>
<mx:DataGridColumn headerText="L_ID" dataField="Id"/>
<mx:DataGridColumn headerText="L_TIME_FROM" textAlign="center" labelFunction="formatTimeFromUtc" dataField="SomeTimeFrom"/>
<mx:DataGridColumn headerText="L_TIME_TO" textAlign="center" labelFunction="formatTimeFromUtc" dataField="SomeTimeTo"/>
<mx:DataGridColumn headerText="L_DATETIME_FROM" textAlign="center" labelFunction="formatDateFromUtc" dataField="Interval.DateTimeFrom"/>
<mx:DataGridColumn headerText="L_DATETIME_TO" textAlign="center" labelFunction="formatDateFromUtc" dataField="Interval.DateTimeTo"/>
<mx:DataGridColumn headerText="L_AMOUNT" dataField="Amount"/>
</mx:columns>
</mx:DataGrid>
基本上,除了“DateTimeFrom”和“DateTimeTo”(在 Interval 下)之外,我的所有列都正确呈现。我是否错误地访问了它们?
我设法自己弄明白了。我创建了一个新的标签函数 a
protected function formatDateFromUtcObject(data:Object, column:Object):String //convert from utc to user date
{
if (data != null)
{
// Check if it's a complex object with properties
var columns:Array = column.dataField.split('.');
var date:String = "";
if (columns.length > 1)
{
date = data[columns[0]][columns[1]]; // I specifically know that my object has a subobject [0] with properties [1])
}
else
{
date = data[column.dataField]; // Else, access the direct property, since I am not trying to access a subobject
}
if (date != null && date != "")
{
var parsedDate:Date = DateTimeFromUtc(Util.invariantFormatter.parseDate(date)); // My case was date that I tried to parse, so that covers the format
if (parsedDate == null)
return "";
else
return Util.formatDate(parsedDate)
}
else
{
return "";
}
}
else
{
return "";
}
}
之后,我在模板代码中调用了label函数:
<mx:DataGridColumn headerText="L_DATETIME_FROM" textAlign="center" labelFunction="formatDateFromUtcObject" dataField="Interval.DateTimeFrom"/>
<mx:DataGridColumn headerText="L_DATETIME_TO" textAlign="center" labelFunction="formatDateFromUtcObject" dataField="Interval.DateTimeTo"/>