When I have a data grid with a dataprovider, how do I access a subobject for a specific column? I am using Adobe Flash Builder 4.6.
My data sample (in JSON for simplicity purposes):
{
"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"
}
}
]
}
My view:
<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>
Essentially, all my columns render properly except the "DateTimeFrom" and "DateTimeTo" (under Interval). Am I incorrectly accessing them or?
I managed to figure it out myself. I created a new label function 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 "";
}
}
Afterwards, I called the label function in my template code:
<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"/>