Apache flex 为什么在使用filter函数筛选arraycollection后未调用dataprovider的setter
我用的是:Apache flex 为什么在使用filter函数筛选arraycollection后未调用dataprovider的setter,apache-flex,actionscript-3,flex3,Apache Flex,Actionscript 3,Flex3,我用的是: private var _hrInfoView:ArrayCollection; [Bindable] public function get HRInfoView():ArrayCollection { return _hrInfoView; } public function set HRInfoView(value:ArrayCollection):void { _hrInfoView = value; } private functi
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?
}
当我把断点放在Hrnofiew的setter上时,它永远不会被击中(当我可以在watch表达式中清楚地看到Hrnofiew在过滤前后都发生了变化时)!为什么?
谢谢。编辑:再次阅读问题后,我想我看到了你的问题 将筛选函数应用于ArrayCollection时,实际上不会影响ArrayCollection。Flex创建ArrayCollection的副本并将其放入“包装器”中,只包含与筛选器匹配的记录。这就是为什么你的setter从未被调用 如果对过滤后的集合调用ArrayCollection.length,它将显示过滤后的记录数,而不是开始时的记录总数。如果删除filter函数并调用refresh()方法,则该“包装器”集合将被删除 您不需要做任何特殊的事情来获取ArrayCollection的“包装器”副本。无论何时使用原始ArrayCollection,Flex都会自动返回集合的筛选/包装副本 有一些额外的信息。并阅读源代码
[编辑]
<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"/>
它没有调用setter,因为您没有设置该属性。你要做的是调用getter,这样你就可以从中获取属性来设置它。
hrinfo.filterFunction
和hrinfo.refresh()
都在调用getter以获取\hrinfo视图
,然后从中调用函数或属性。没有人应答吗?谢谢,我试过了。还是同样的问题。更重要的问题应该是,过滤函数实际上做什么?它可能根本不会改变数据提供者!你能给我提供一些见解吗?谢谢。@user您可以在将ArrayCollection绑定到DataGrid的位置发布代码吗?@user filterFunction用于筛选数组集合,以仅显示符合特定条件的项。将筛选器函数应用于ArrayCollection并调用refresh()方法后,它将删除所有与筛选器不匹配的行。看看一个简单的例子。谢谢杰森,我已经知道很多了。。。我需要知道的是filterfunction实际上是如何工作的。。。当你说它消除了,它是如何做到的?因为我可以看到即使在过滤之后setter也没有被调用,我需要知道这种行为的原因…再次感谢。。。我将很快发布完整的代码…该链接仅适用于应用程序级别。。。我需要知道filterfunction是如何工作的。。。当你说它消除了,它是如何做到的?因为我可以看到即使在过滤之后也不会调用setter,我需要知道这种行为的原因……过滤函数不会通过检查arraycollection中的每个元素并返回true或false来调用或设置它们工作的数据。我将发布一个示例函数shortlyOk我为你添加了一个示例创建了一个新项目并进行了尝试我很抱歉Tom,但我猜你完全误解了我的问题。我不是问这个。我知道如何很好地使用它。我也知道它的功能。如果你再次关注我和Jason之间的问题和上述评论,你就会知道这里的问题是什么。无论如何,谢谢。有什么不明白的,我回答了你在我的样品中提出的所有问题。我还添加了一个过滤器删除功能。原始数据从未消失,只是经过过滤。只需删除过滤函数,就可以获得数据。