ListCollectionView中filterFunction只对集合过滤一次的实现

本文介绍如何在Flex中使用ListCollectionView类实现一次性过滤集合的功能,并确保之后的元素插入不再受过滤影响。通过监听刷新事件来移除过滤函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Flex中,集合类ListCollectionView及其子类是很常用的。这个类提供了两个很有用的功能,一个是过滤,一个是排序。这两个功能的实现是设置ListCollectionViewr的filterFunction和sort属性,然后调用refresh方法。然后refresh方法会在系统认为合适的时候,更新集合内容。

不过,在集合内容被更新之后,ListCollectionViewr的filterFunction和sort属性并不会被清除,而且这时向集合中插入新的元素,都要经过过滤和排序。

尤其是对过滤操作来说,我们很可能会遇到希望只对集合现有的元素一次过滤,然后在后续的插入操作中,不再进行过滤。这时,就需要在集合更新操作完成之后,将ListCollectionViewr的filterFunction属性置空。而另一方面,Flex系统并不一定会在refresh方法被调用时,就会立刻去更新集合内容,所以我们在调用refresh方法后,立刻去将filterFunction置空,可能会出现问题。根据Flex 的language reference,当集合更新完成后,集合会触发CollectionEvent事件,并且这个事件的kind属性是CollectionEventKind.REFRESH,那么我们就有以下这种解决方法。

  1. package mithrilon.flex {
  2.     import mx.collections.ListCollectionView;
  3.     import mx.events.CollectionEvent;
  4.     import mx.events.CollectionEventKind;
  5.     public class CollectionUtils {
  6.         /**
  7.         * Filter <code>ListCollectionView</code> parameter collection with <code>Function</code>
  8.         * parameter filterFunction, then refresh the collection. When refreshing has been finished,
  9.         * remove the filterFunction from the collection.
  10.         * 
  11.         * NOTE:The definition of filterFunction must follow the requirement of 
  12.         * <code>ListCollectionView</code>. 
  13.         */
  14.         public static function filterCollectionOnceAndRefresh(collection:ListCollectionView,
  15.                 filterFunction:Function):void
  16.         {
  17.             filterCollectionOnce(collection, filterFunction);
  18.             collection.refresh();
  19.         }
  20.         /**
  21.         * Filter <code>ListCollectionView</code> parameter collection with <code>Function</code>
  22.         * parameter filterFunction. When refreshing has been finished, filterFunction will be
  23.         * removed from the collection.
  24.         * 
  25.         * NOTE:The definition of filterFunction must follow the requirement of 
  26.         * <code>ListCollectionView</code>.
  27.         * NOTE: You should refresh the collection. 
  28.         */
  29.         public static function filterCollectionOnce(collection:ListCollectionView,
  30.                 filterFunction:Function):void
  31.         {
  32.             collection.filterFunction = filterFunction;
  33.             collection.addEventListener(CollectionEvent.COLLECTION_CHANGE, removeCollectionFilter);
  34.         }
  35.         /**
  36.         * This is a event handler for <code>CollectionEvent</code>. When the event parameter's kind
  37.         * property is <code>CollectionEventKind.REFRESH</code>, it will remove filterFunction from 
  38.         * collection dipatching event.
  39.         * 
  40.         * When a <code>ListCollectionView</code> object dispatches a CollectionEvent object whose
  41.         * kind property is <code>CollectionEventKind.REFRESH</code>, it means the refreshing
  42.         * operation on that collection has been done successfully.
  43.         */
  44.         public static function removeCollectionFilter(event:CollectionEvent):void {
  45.             if (!event || event.kind != CollectionEventKind.REFRESH) {
  46.                 return;
  47.             }
  48.             var collection:ListCollectionView = event.target as ListCollectionView;
  49.             collection.filterFunction = null;
  50.             collection.removeEventListener(CollectionEvent.COLLECTION_CHANGE, removeCollectionFilter);
  51.             //NOTE: It is NOT enough to set filterFunction to be null to remove filter from
  52.             //collection. Calling refresh method is necessary, or some internal data containing
  53.             //filtering info will cause some error.
  54.             collection.refresh();
  55.         }
  56.     }
  57. }

可以如下使用此工具类。

 

  1. ……
  2. var collection:ArrayCollection = new ArrayCollection();
  3. ……
  4. CollectionUtils.filterCollectionOnceAndRefresh(collection, someFilter);
  5. ……

 

注意:

在CollectionUtils的62行,此处调用refresh方法的作用是将集合内部保存的一些过滤和排序的视图数据清空,因为只是将filterFunction置空,集合是不会同时清除视图数据的,这时如果对集合进行操作,就会产生一些运行时异常。而且也必须在collection.removeEventListener(CollectionEvent.COLLECTION_CHANGE, removeCollectionFilter)这段代码的后面调用refresh,否则

removeCollectionFilter可能会被重复调用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值