首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >为什么使用筛选函数筛选数组集合后不调用dataprovider的setter

为什么使用筛选函数筛选数组集合后不调用dataprovider的setter
EN

Stack Overflow用户
提问于 2011-06-10 08:48:28
回答 3查看 976关注 0票数 1

我使用的是:

代码语言:javascript
运行
复制
private var _hrInfoView:ArrayCollection;
 [Bindable]
 public function get HRInfoView():ArrayCollection
  {
   return _hrInfoView;
   }
  public function set HRInfoView(value:ArrayCollection):void
  {
   _hrInfoView = value;
  }

 private function onFilterByContent(event:ContextMenuEvent):void
 {              
    HRInfoView.filterFunction = processFilter;  
    HRInfoView.refresh();
  //Break point here shows HRInfoView as different what is in line above. Why is setter not called?
}

当我将断点放在HRInfoView的setter上时,它永远不会命中(当我可以在监视表达式中清楚地看到过滤前后HRInfoView发生了变化时)!为什么?谢谢。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-06-10 23:37:37

编辑:再读一遍问题后,我想我看到你的问题了。

当您将过滤函数应用于ArrayCollection时,您实际上并没有影响ArrayCollection。Flex创建ArrayCollection的副本并将其放入“包装器”中,并且只包含与您的过滤器匹配的记录。这就是为什么你的setter从不被调用的原因。

如果您在过滤集合上调用ArrayCollection.length,它将显示过滤记录的数量,而不是您开始时的记录总数。如果删除filter函数并调用refresh()方法,则该“包装器”集合也会被删除。

您不需要做任何特殊的事情就可以获得ArrayCollection的“包装器”副本。每当您使用原始ArrayCollection时,Flex都会自动返回集合的过滤/包装副本。

This link有一些额外的信息。

票数 1
EN

Stack Overflow用户

发布于 2011-06-11 01:07:50

go here并阅读源代码

编辑

代码语言:javascript
运行
复制
<mx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;

        [Bindable]
        public var myAC:ArrayCollection = new ArrayCollection([{label:"One"}, {label:"Two"}, {label:"Three"}, {label:"Four"}]);

        [Bindable]
        public var myAC2:ArrayCollection = new ArrayCollection();

        public function removeFilter(e:Event):void{
            trace(this.myAC.source.length )
            this.myAC.filterFunction = null;
            this.myAC.refresh()
            trace(this.myAC.source.length )
        }
        public function addFilter(e:Event):void{
            trace(this.myAC.source.length )
            this.myAC.filterFunction = filterFunc;
            this.myAC.refresh()
            trace(this.myAC.source.length )
        }
        public function filterFunc( item:Object ):Boolean{
            if( item.label == "One" )
                return true;

            if( item.label == "Two" )
                return true;

            return false;
        }
        public function copyData(e:Event):void{
            myAC2 =   new ArrayCollection( myAC.toArray() );  
            myAC2.refresh()
        }
    ]]> 
</mx:Script>
<mx:Label text="original data with filter option"  y="0"/>
<mx:DataGrid y="26" id="nameGrid" dataProvider="{myAC}" width="200" height="200"/>
<mx:Button id="button2" label="Add Filter" click="addFilter(event)" x="0" y="234"/>
<mx:Button id="button3" y="264" label="Remove Filter" click="removeFilter(event)"/>


<mx:Button id="button4" x="289" y="234" label="copy filtered data" click="copyData(event)"/>
<mx:Label text="Copied filtered data"  x="300" y="0"/>
<mx:DataGrid  y="26" x="287" id="nameGrid2" dataProvider="{myAC2}" width="200" height="200"/>
票数 1
EN

Stack Overflow用户

发布于 2012-06-28 02:58:52

它不会调用setter,因为您没有设置该属性。您要做的就是调用getter,这样您就可以从它获取属性来设置它。HRInfoView.filterFunctionHRInfoView.refresh()都调用getter来获取_hrInfoView,然后从中调用函数或属性。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6300866

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档