FLEX ArrayCollection删除过滤的数据

一、问题:

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;					
				}
			}
		}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值