菊花链 ListCollectionView 未反映源集合中过滤器的更改
ListCollectionView 通常被宣传为以菊花链方式连接同一数据的不同视图的好方法。您有一些源集合,然后使用 ListCollectionView + filterFunction 来包装源数据并呈现该源集合的子集。当源集合发生变化(添加或删除)时,链接的 LCV 会自动更新。但是,如果源集合的filterFunction发生变化,它似乎不会传播到链接的LCV:
ArrayCollection source = new ArrayCollection(new Array({name: 'Matt'}, {name: 'John'}, {name: 'Zach'}));
ListCollectionView justMatts = new ListCollectionView(source);
justMatts.filterFunction(function(obj: Object):Boolean { obj.name == 'Matt' });
justMatts.refresh();
如果我向source
添加一个新的Matt对象,justMatts
将接受该更改并按预期重新运行其过滤器。
但是,如果我向 source
添加过滤器以排除 Matt 对象,justMatts
不会拾取对源过滤器的更改:
source.filterFunction(function(obj: Object):Boolean { obj.name != 'Matt' });
source.refresh();
此时,我希望 justMatts接收 CollectionChangeEvent
并重新运行其过滤器。由于所有 Matt 都已从 source
对象中过滤掉,因此 justMatts
也将为空。但是,如果您查看ListCollectionView
代码,它们不会处理CollectionEventKind.REFRESH
,这是当source
应用新过滤器时触发的。我在这里做错了什么(使用错误的类等)还是在 Flex 中没有做任何事情支持对 source
过滤器的更改。
我可以通过在 source
上手动分派 CollectionEventKind.RESET
来按照我的预期实现此功能,但我希望不必对任何内容进行子类化。
ListCollectionView is often promoted as a great way to daisy-chain different views of the same data. You have some source collection and then use ListCollectionView + filterFunction to wrap the source data and present a subset of that source collection. When the source collection changes (either adds or removals), the chained LCV gets updated automatically. However, if the filterFunction for the source collection changes, it does not appear to propagate to the chained LCV:
ArrayCollection source = new ArrayCollection(new Array({name: 'Matt'}, {name: 'John'}, {name: 'Zach'}));
ListCollectionView justMatts = new ListCollectionView(source);
justMatts.filterFunction(function(obj: Object):Boolean { obj.name == 'Matt' });
justMatts.refresh();
If I add a new Matt object to source
, justMatts
will pick up that change and rerun its filter as expected.
However, if I add a filter to source
to exclude Matt objects, justMatts
doesn't pick up that change to the source's filter:
source.filterFunction(function(obj: Object):Boolean { obj.name != 'Matt' });
source.refresh();
At this point, I would expect justMatts to receive a CollectionChangeEvent
and rerun its filter. Since all Matts have been filtered out of the source
object, justMatts
would be empty as well. However if you look in ListCollectionView
code, they don't handle a CollectionEventKind.REFRESH
which is what gets fired when source
gets a new filter applied. Am I doing something wrong here (using wrong class, etc) or does nothing in Flex support the changes to source
filter.
I can get this working as I expect it to by manually dispatching a CollectionEventKind.RESET
on source
, but I was hoping to not have to subclass anything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我根本没想到它会这样工作。我希望 ArrayCollection(这是一个 LisCollectionView)和显式 ListCollectionView 的源是相同的,它们的过滤器独立运行。
但是,如果您仔细研究这两个类的源代码,您很可能会发现过滤 AC 不会分派 CollectionChange 事件(值得怀疑,因为它需要刷新),或者 LCV 不会侦听该事件并且更新。
如果您首先将过滤器应用于列表,然后创建 ListCollectionView,您会看到您期望的结果吗?如果没有,那么我认为您误解了它的工作原理。如果你这样做了,那么我就有了;-)
I wouldn't expect it to work like that at all. I would expect that the source of both the ArrayCollection (which is a LisCollectionView) and the explict ListCollectionView to be identical, with their filters operating independently.
However, if you poke around in the source code for these two Classes, you may well find that either filtering the AC doesn't dispatch a CollectionChange event (doubtful, since it requires refresh), or LCV doesn't listen for the event and update.
If you apply the filter to the list first and THEN make the ListCollectionView, do you see the results you expect? If not, then I think you've misread how this is supposed to work. If you do, then I have ;-)