加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

FLEX ArrayCollection删除过滤的数据

发布时间:2020-12-15 04:20:55 所属栏目:百科 来源:网络整理
导读:一、问题: ArrayCollection添加过滤器后,部门数据不会被展现,当我删除未展现的数据时,调用removeItemAt()是无法删除的。 二、原因: public function removeItemAt(index:int):Object { if (index 0 || index = length) { var message:String = resour

一、问题:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读