flex ArrayCollection 存储的对象中包含另一个对象。这是如果想要访问最内层对象的属性可以直接通过两次“.”进行,但是这是如果最内层对象如果是空的程序会报错退出。举个例子说明:
public dynamic class CustomerInfor
{
private var _customerId:String;// 客户ID(关键字)
private var _customerNam:String;// 客户(唯一)
private var _phoneNum:String;// 手机号码
private var _e_mail:String;// 客户电子邮箱地址
private var _address:String;// 客户地址
private var _customerType:String;// 客户类型。批发客户,零售客户,
}
public dynamic class CustomerRecords
{
private var _customerRecordsId:String;// 客户消费记录ID(关键字)
private var _customer:CustomerInfor;// 客户ID(关键字)
private var _consumeAmount:Number;// 交易金额
private var _consumeTimes:int;// 交易次数
private var _consumeDate:Date;// 交易日期
private var _goodsTypeNam:String;// 商品所属种类(农作物、疫苗等)
}
var record1: CustomerRecords = new CustomerRecords ();
var record2: CustomerRecords = new CustomerRecords ();
var record3: CustomerRecords = new CustomerRecords ();
var records:ArrayCollection = new ArrayCollection(
[record1, record2, record3
]);
这里我们看到一个CustomerRecords 对象里面有一个CustomerInfor 类型的变量作为属性,在新定义的records:ArrayCollection 中添加了三个CustomerRecords 类型的变量。现在我们对records进行操作。
首先是进行过滤:把customer 中“批发客户”的销售信息筛选出来。
protected function id_test_clickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
records.filterFunction = filterFuc;
records .refresh();
}
public function filterFuc( item:Object ):Boolean
{
// Alert.show(item.customer.toString(),'fail',4,this);
var tt:CustomerInfor = item.customer;
if( “批发客户” == tt. customerType )
return true;
return false;
}
这里我们需要注意的是如果一个CustomerRecords对象中customer属性为空,那么上述函数会报错中止程序执行。
接要说用records作为数据源来对组件提供数据:
1.作为DataGrid组件的数据源
这个很简单。例如:
<mx:DataGrid y="83" width="491" height="434" variableRowHeight="true" dataProvider="{customerRecord}" id="IDD_RECORDS" x="6">
<mx:columns>
<mx:DataGridColumn headerText="消费记录ID" dataField="customerRecordsId" visible="false"/>
<mx:DataGridColumn headerText="客户名" dataField="customer.customerNam" width="80" fontSize="11" textAlign="center" />
<mx:DataGridColumn headerText="交易金额" dataField="consumeAmount" width="70" fontSize="11" textAlign="center"/>
<mx:DataGridColumn headerText="交易次数" dataField="consumeTimes" width="70" fontSize="11" textAlign="center"/>
<mx:DataGridColumn headerText="交易日期" dataField="consumeDate" width="90" fontSize="11" textAlign="center" labelFunction="getformatDate"/>
<mx:DataGridColumn headerText="交易商品类型" dataField="goodsTypeNam" width="100" fontSize="11" textAlign="center" />
</mx:columns>
</mx:DataGrid>
2.作为DropDownList(以及相似的组件)的数据源
这里有些人可能想当然的以为和DataGrid类似即可,如下述代码:
<s:DropDownList x="57" y="291" id="idd_test" dataProvider="{customerRecord}" labelField="customer.customerNam" />
在这里很遗憾的告诉你,你的想法错了,错的很厉害。
这个地方是一个很纠结的,下面讲一下我的解决办法,代码如下:
<s:DropDownList x="57" y="291" id="idd_test" dataProvider="{customerRecord}" labelField="customer" labelFunction="droptest" />
也就是添加一个labelFunction函数。
private function droptest( item:Object ):String
{
return item.customer.customerNam;
}
实验一下,哈哈可以正常显示目标数据了。
但是这里还是有一点小问题的。知道labelFunction含义的或许已经想到了。没错就是labelFunction执行时已经把绑定的数据改变成String类型了。这时再选中的数据就是一个String类型。所以我们无法得到想要的CustomerRecords对象啊。
这里说一下我的解决办法。
虽然labelFunction改变了改变了组件中得数据类型,但是对于数据源的数据并没有变化,而且对应的数据位置也没有变化。所以这里我们可以先获取选中项的INDEX再根据这个从数据源中获取目标数据。
恩这次就总结这么多吧。