一、問(wèn)題:
ArrayCollection添加過(guò)濾器后,部門數(shù)據(jù)不會(huì)被展現(xiàn),當(dāng)我刪除未展現(xiàn)的數(shù)據(jù)時(shí),調(diào)用removeItemAt()是無(wú)法刪除的。
二、原因:
復(fù)制代碼 代碼如下:
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);
}
因?yàn)関ar oldItem:Object = localIndex[index];中l(wèi)ocalIndex是一個(gè)未被過(guò)濾的數(shù)據(jù)。
三、解決
ArrayCollection中有l(wèi)ist的屬性:
復(fù)制代碼 代碼如下:
public function get list():IList
{
return _list;
}
_list就是原始數(shù)據(jù)。
所以如果要在添加了過(guò)濾器的ArrayCollection上刪除過(guò)濾的數(shù)據(jù),需要list的幫助。實(shí)現(xiàn)代碼如下:
復(fù)制代碼 代碼如下:
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);
}
}
或者一個(gè)函數(shù):
復(fù)制代碼 代碼如下:
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;
}
}
}