正文

FLEX ArrayCollection删除过滤的数据问题解决(flexible什么意思)全程干货

文章摘要

文章讨论了在使用ArrayCollection添加过滤器后遇到的问题:过滤器添加后,部门数据无法显示,且删除未显示数据时,调用`removeAtIndex()`方法无法成功。文章详细解释了原因:在过滤器添加时,`removeAtIndex()`方法中的`localIndex`仍指向未过滤的数据,因此无法正确删除过滤后的数据。文章提供了解决方案,即通过访问原始数据列表来实现过滤后的数据删除。提供了具体的代码示例,展示了如何在添加过滤器后,通过`employees.list`获取原始数据列表,并通过遍历该列表找到目标数据后删除。

 一、问题:

ArrayCollection添加过滤器后,部门数据不会被展现,当我删除未展现的数据时,调用removeItemAt()是无法删除的。

二、原因:

复制代码 代码如下:

public function removeItemAt(index:int):Object

{

if (index < 0 || index >=length)

{

var message:String=resourceManager.getString(

“collections”, “outOfBounds”, [ index ]);

throw new RangeError(message);

}

var listIndex:int=index;

if (localIndex)

{

var oldItem:Object=localIndex[index];

listIndex=list.getItemIndex(oldItem);

}

return list.removeItemAt(listIndex);

}

因为var oldItem:Object=localIndex[index];中localIndex是一个未被过滤的数据。

三、解决

ArrayCollection中有list的属性:

复制代码 代码如下:

public function get list():IList

{

return _list;

}

_list就是原始数据。

所以如果要在添加了过滤器的ArrayCollection上删除过滤的数据,需要list的帮助。实现代码如下:

复制代码 代码如下:

public function findEmployeeInSource(id:int):OrgEmployee {

var obj:OrgEmployee=null;

var list:IList=employees.list;

var len:int=list.length;

for (var index:int=0; index < len; index++) {

obj=list.getItemAt(index) as OrgEmployee;

if (obj.id==id) {

return obj;

}

}

return null;

}

public function deleteEmployee(id:int):void {

var obj:OrgEmployee=findEmployeeInSource(id);

if (obj !=null) {

var index:int=employees.list.getItemIndex(obj);

employees.list.removeItemAt(index);

}

}

或者一个函数:

复制代码 代码如下:

public function deleteEmployee(id:int):void {

var obj:OrgEmployee=null;

var list:IList=employees.list;

var len:int=list.length;

for (var index:int=0; index < len; index++) {

obj=list.getItemAt(index) as OrgEmployee;

if (obj.id==id) {

list.removeItemAt(index);

return;

}

}

}