问题
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 ) );
}
回答1:
Easiest way to do a one time sort:
var array:Array = yourArrayCollection.toArray():
array.sortOn("SomePropertyAvailableInEachItem", Array.DESCENDING | Array.NUMERIC);
yourArrayCollection.refresh();
Since you're not sorting the underlying Array, and not the collection.
回答2:
No. You should call myCollection.refresh()
every time it is changed. But you can listen for collectionChange event and call refresh from there:
private function collectionChangeHandler(event:CollectionEvent):void
{
if (event.kind == CollectionEventKind.ADD || event.kind == CollectionEventKind.REMOVE ||
event.kind == CollectionEventKind.REPLACE || event.kind == CollectionEventKind.MOVE || event.kind == CollectionEventKind.UPDATE)
{
ArrayCollection(event.currentTarget).refresh();
}
}
回答3:
The sort of an ArrayCollection
will remain linked to it. You can try to set the sort
attribute to null after the initial sort and refresh, but I'm not 100% sure that a following refresh()
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 new ArrayCollection
passing that array as its source in the constructor. Note that ArrayCollection
is really just a wrapper on array, so the toArray()
and the construction of a new ArrayCollection
for an existing array should not be heavy operations.
回答4:
A Sort
on an ArrayCollection
is an object itself. When you assign the Sort
object to the sort property on the ArrayCollection
, it remains assigned until you remove it manually. To remove it, just set the sort
property on your ArrayCollection
to null
after you call refresh()
.
myArrayCollection.sort = mySort;
myArrayCollection.refresh();
myArrayCollection.sort = null;
Now any time you call refresh() on the ArrayCollection, the sort
is no longer in place.
来源:https://stackoverflow.com/questions/6536605/sort-collection-and-sort-remains-in-place-when-adding-to-collection