FLEX ArrayCollection删除过滤的数据问题解决
ArrayCollection添加过滤器后,部门数据不会被展现,当我删除未展现的数据时,调用removeItemAt()是无法删除的。
二、原因:
publicfunctionremoveItemAt(index:int):Object
{
if(index<0||index>=length)
{
varmessage:String=resourceManager.getString(
"collections","outOfBounds",[index]);
thrownewRangeError(message);
}
varlistIndex:int=index;
if(localIndex)
{
varoldItem:Object=localIndex[index];
listIndex=list.getItemIndex(oldItem);
}
returnlist.removeItemAt(listIndex);
}
因为varoldItem:Object=localIndex[index];中localIndex是一个未被过滤的数据。
三、解决
ArrayCollection中有list的属性:
publicfunctiongetlist():IList
{
return_list;
}
_list就是原始数据。
所以如果要在添加了过滤器的ArrayCollection上删除过滤的数据,需要list的帮助。实现代码如下:
publicfunctionfindEmployeeInSource(id:int):OrgEmployee{
varobj:OrgEmployee=null;
varlist:IList=employees.list;
varlen:int=list.length;
for(varindex:int=0;index<len;index++){
obj=list.getItemAt(index)asOrgEmployee;
if(obj.id==id){
returnobj;
}
}
returnnull;
}
publicfunctiondeleteEmployee(id:int):void{
varobj:OrgEmployee=findEmployeeInSource(id);
if(obj!=null){
varindex:int=employees.list.getItemIndex(obj);
employees.list.removeItemAt(index);
}
}
或者一个函数:
publicfunctiondeleteEmployee(id:int):void{
varobj:OrgEmployee=null;
varlist:IList=employees.list;
varlen:int=list.length;
for(varindex:int=0;index<len;index++){
obj=list.getItemAt(index)asOrgEmployee;
if(obj.id==id){
list.removeItemAt(index);
return;
}
}
}