添加到集合时排序集合和排序保持不变吗?
当我从服务层获取集合时,我创建一个 ArrayCollection 并应用排序。当我稍后将项目添加到集合中时,排序仍然有效吗?看来确实如此。我以为我只对它进行一次排序,而不是应用一种会粘住的排序???
这是添加项目的方法:
private function onAddNewClick():void
{
var fileTemplate:FileTemplateDetailDTO = new FileTemplateDetailDTO();
fileTemplate.fileTemplateId = 0;
fileTemplate.fileTmpltDtlId = 0;
fileTemplate.createdBy = appModel.userName;
newFieldsDp.addItem( fileTemplate );
this.fieldsGridEmpty.rowCount = newFieldsDp.length + 1;
totalCount = newFieldsDp.length;
this.fieldsGridEmpty.scrollToIndex( totalCount );
}
这是当数据从服务返回时我正在尝试的方法:
for each( var dto:FileTemplateCompositeDTO in coll ){
dto.templateHistory = createHistoryColl( dto.details );
var sortedArray:ArrayCollection = sortDetails( dto.details );
sortedArray.sort = null;
var freshArray:Array = sortedArray.toArray();
dto.details.source = freshArray;
model.fileTemplateComposites.addItem( FileTemplateCompositeDTO( dto ) );
}
When I get a collection back from the service tier, I create an ArrayCollection and apply a sort. When I add an item to the collection later on, the sort is still in place? It seems to be the case. I thought I was only sorting it once, not applying a sort that will stick???
Here is the method for adding an item:
private function onAddNewClick():void
{
var fileTemplate:FileTemplateDetailDTO = new FileTemplateDetailDTO();
fileTemplate.fileTemplateId = 0;
fileTemplate.fileTmpltDtlId = 0;
fileTemplate.createdBy = appModel.userName;
newFieldsDp.addItem( fileTemplate );
this.fieldsGridEmpty.rowCount = newFieldsDp.length + 1;
totalCount = newFieldsDp.length;
this.fieldsGridEmpty.scrollToIndex( totalCount );
}
And here is what I'm trying now for when the data comes back from the service:
for each( var dto:FileTemplateCompositeDTO in coll ){
dto.templateHistory = createHistoryColl( dto.details );
var sortedArray:ArrayCollection = sortDetails( dto.details );
sortedArray.sort = null;
var freshArray:Array = sortedArray.toArray();
dto.details.source = freshArray;
model.fileTemplateComposites.addItem( FileTemplateCompositeDTO( dto ) );
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
进行一次性排序的最简单方法:
因为您没有对底层数组进行排序,而不是对集合进行排序。
Easiest way to do a one time sort:
Since you're not sorting the underlying Array, and not the collection.
不可以。每次更改时,您都应该调用
myCollection.refresh()
。但您可以收听collectionChange
事件 并从那里调用刷新:No. You should call
myCollection.refresh()
every time it is changed. But you can listen forcollectionChange
event and call refresh from there:ArrayCollection
的排序将保持与其链接。您可以尝试在初始排序和刷新后将sort
属性设置为 null,但我不能 100% 确定以下refresh()
会保持元素顺序(尽管应该如此)。另一种方法:将初始排序应用于集合后,您可以调用其 toArray() 方法来获取(已排序的)数组,然后只需创建一个新的 ArrayCollection 并将其传递给数组作为构造函数中的源。请注意,
ArrayCollection
实际上只是数组的包装器,因此toArray()
以及为现有数组构造新的ArrayCollection
不应该进行繁重的操作。The sort of an
ArrayCollection
will remain linked to it. You can try to set thesort
attribute to null after the initial sort and refresh, but I'm not 100% sure that a followingrefresh()
will keep the elements order (although it should).Another approach: after applying the initial sort to your collection, you can call its
toArray()
method to obtain a (sorted) array, then just create a newArrayCollection
passing that array as its source in the constructor. Note thatArrayCollection
is really just a wrapper on array, so thetoArray()
and the construction of a newArrayCollection
for an existing array should not be heavy operations.ArrayCollection
上的Sort
本身就是一个对象。当您将Sort
对象分配给ArrayCollection
上的排序属性时,它会保持分配状态,直到您手动将其删除。要删除它,只需在调用refresh()
后将ArrayCollection
上的sort
属性设置为null
即可。现在,每当您在 ArrayCollection 上调用刷新()时,
sort
就不再存在。A
Sort
on anArrayCollection
is an object itself. When you assign theSort
object to the sort property on theArrayCollection
, it remains assigned until you remove it manually. To remove it, just set thesort
property on yourArrayCollection
tonull
after you callrefresh()
.Now any time you call refresh() on the ArrayCollection, the
sort
is no longer in place.