flex(flash builder 4)に高度なデータグリッドがあります。dataProviderはArrayCollection(this._encounters)を指しています。
その配列コレクション内には、オブジェクト(クライアントオブジェクト)であるプロパティがあります。
this._encounters配列コレクションのclientObjプロパティ内の名プロパティを参照するために、dataFieldを「clientObj.firstName」に設定してみました。何も表示されませんでした。
そこで、その列(以下のコード)にlabelFunctionを追加して、セルにテキストを設定しました。これは正常に機能し、グリッドに値が表示されます。
問題は、列のタイトルをクリックして並べ替えるときです。プロパティclientObj.firstNameが私の配列コレクションに見つからないというエラーがスローされます!
それで、列のdataField / sourceを設定し、サブオブジェクトのプロパティを指すより良い方法、または並べ替えを修正する方法はありますか?
最初の列の下
<mx:AdvancedDataGrid x="0" y="25" id="adgEncounters" designViewDataType="flat" width="100%" height="100%" dataProvider="{this._encounters}">
<mx:columns>
<mx:AdvancedDataGridColumn headerText="first" dataField="clientObj.firstName" labelFunction="encounterGridLabelFunct"/>
<mx:AdvancedDataGridColumn headerText="first" dataField="thisWorksField"/>
</mx:columns>
</mx:AdvancedDataGrid>
protected function encounterGridLabelFunct(item:Object, column:AdvancedDataGridColumn):String //put just the HH:MM in to the grid, not the whole date string
{
if(column.headerText=="first") result=item.clientObj.firstName;
return result;
}
更新:これが私が使用した最終的な動作コードです。3つのソート関数の例。1つは数値ソート用、1つは文字列ソート用、もう1つは日付ソート(データベースからの文字列日付)用です。
// sort adg column numerically
private function numericSortByField(subObjectName:String, fieldName:String):Function
{
return function(obj1:Object, obj2:Object):int
{
var value1:Number = (obj1[subObjectName][fieldName] == '' || obj1[subObjectName][fieldName] == null) ? null : new Number(obj1[subObjectName][fieldName]);
var value2:Number = (obj2[subObjectName][fieldName] == '' || obj2[subObjectName][fieldName] == null) ? null : new Number(obj2[subObjectName][fieldName]);
return ObjectUtil.numericCompare(value1, value2);
}
}
//sort adg column string
private function stringSortByField(subObjectName:String, fieldName:String):Function
{
return function(obj1:Object, obj2:Object):int
{
var value1:String = (obj1[subObjectName][fieldName] == '' || obj1[subObjectName][fieldName] == null) ? null : new String(obj1[subObjectName][fieldName]);
var value2:String = (obj2[subObjectName][fieldName] == '' || obj2[subObjectName][fieldName] == null) ? null : new String(obj2[subObjectName][fieldName]);
return ObjectUtil.stringCompare(value1, value2);
}
}
//sort adg date diff (takes date strings for example from a db)
private function dateSortByField(subObjectName:String, fieldName:String):Function
{
return function(obj1:Object, obj2:Object):int
{
var value1:String = (obj1[subObjectName][fieldName] == '' || obj1[subObjectName][fieldName] == null) ? null : new String(obj1[subObjectName][fieldName]);
var value2:String = (obj2[subObjectName][fieldName] == '' || obj2[subObjectName][fieldName] == null) ? null : new String(obj2[subObjectName][fieldName]);
var value1Date:Date = new Date();
var value1Time:int = value1Date.setTime(Date.parse(value1));
var value2Date:Date = new Date();
var value2Time:int = value2Date.setTime(Date.parse(value2));
return ObjectUtil.numericCompare(value1Time, value2Time);
}
}
上記のmxmlでは、これは変更された行です-stringSortByFieldが追加されていることに注意してください。
<mx:AdvancedDataGridColumn headerText="first" dataField="clientObj.firstName" sortCompareFunction="{stringSortByField('clientObj','firstName')}" labelFunction="encounterGridLabelFunct"/>
数値フィールドの場合は、numericSortByFieldを使用します。データベースの日付文字列の場合は、代わりにdateSortByField関数を使用してください。