添加到集合时排序集合和排序保持不变吗?

发布于 2024-11-17 16:37:10 字数 1153 浏览 1 评论 0原文

当我从服务层获取集合时,我创建一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

书信已泛黄 2024-11-24 16:37:10

进行一次性排序的最简单方法:

var array:Array = yourArrayCollection.toArray():
array.sortOn("SomePropertyAvailableInEachItem", Array.DESCENDING | Array.NUMERIC);
yourArrayCollection.refresh();

因为您没有对底层数组进行排序,而不是对集合进行排序。

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.

卸妝后依然美 2024-11-24 16:37:10

不可以。每次更改时,您都应该调用 myCollection.refresh()。但您可以收听 collectionChange 事件 并从那里调用刷新:

    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();
        }
    }

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();
        }
    }
不再让梦枯萎 2024-11-24 16:37:10

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 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.

神回复 2024-11-24 16:37:10

ArrayCollection 上的 Sort 本身就是一个对象。当您将 Sort 对象分配给 ArrayCollection 上的排序属性时,它会保持分配状态,直到您手动将其删除。要删除它,只需在调用 refresh() 后将 ArrayCollection 上的 sort 属性设置为 null 即可。

myArrayCollection.sort = mySort;
myArrayCollection.refresh();
myArrayCollection.sort = null;

现在,每当您在 ArrayCollection 上调用刷新()时,sort 就不再存在。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文