arraycollection 的 AddItemAt() 在 Flex 中不起作用

发布于 2024-11-27 05:21:14 字数 107 浏览 1 评论 0原文

我正在尝试在 arraycollection 中添加一个项目,该项目使用 addItemAt() 进行排序和过滤。 但 addItemAt() 并未将项目添加到指定索引。 有谁知道上述问题的解决方案。

I am trying to add an item in arraycollection which is sorted and filtered using addItemAt().
But addItemAt() is not adding item to the specified index.
Do anyone knows the solution for the above problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

你穿错了嫁妆 2024-12-04 05:21:14

我正在尝试在 arraycollection 中添加一个已排序的项目,并且
过滤

如果集合已排序,则当您向其中添加新项目时,过滤器将自动刷新。因此,您添加项目的索引可能不是您的项目最终的索引。这完全取决于排序算法。

您可以删除排序以将新项目锁定在您指定的索引处。我突然想到,这样做:

arrayCollection.sort = null;
arrayCollection.refresh();

我很确定同样的概念也适用于过滤。如果您将过滤器应用于集合,则新项目需要匹配过滤条件,否则在删除过滤器之前它不会显示在集合中。

I am trying to add an item in arraycollection which is sorted and
filtered

If the collection is sorted, the filter will automatically be refreshed when you add a new item to it. So, the index you add your item may not the index were your item ends up. It depends entirely on the sorting algorithm.

You can remove the sort to lock your new item at the index you specify. Off the top of my head, do this:

arrayCollection.sort = null;
arrayCollection.refresh();

I'm pretty sure the same concept applies to filtering. If you have a filter applied to a collection, the new item needs to match the filter criteria or else it will not show up in the collection until the filter is removed.

咆哮 2024-12-04 05:21:14

我只在 Flex 3 和排序的 ArrayCollection 中遇到了与此类似的问题。如果你搜索,你会发现 addItemAt 不适用于排序的 ArrayCollection (并且可能没有过滤?不知道)。将根据排序标准添加该项目。

但是,我需要一个顶部有“全选”选项的已排序 ArrayCollection(alpha),所以我是这样进行的:

可以轻松对数组进行排序(array.sort),所以我首先创建了一个数组。然后,我循环 ArrayCollection 并将要排序的 ArrayCollection 中的项目添加到数组中。然后对这个新数组进行排序。

新排序的数组被循环,并且在这个循环中,ArrayCollection 被再次循环。如果在排序项上找到匹配项,我会将此对象添加到新的 ArrayCollection 中,同时还会为添加的对象创建一个名为“sortOrder”的新属性,该属性被设置为循环计数。

接下来,创建“全选”对象并将其 sortOrder 设置为 -1。

最后,在 ArrayCollection 的 sortOrder 字段上创建了数字排序,瞧,它成功了。

也许有人有一个更优雅的解决方案,但我很着急,而且它确实有效。

希望这对某人有帮助。

I had a problem similiar to this only in Flex 3 and with a sorted ArrayCollection. If you scour, you will find that addItemAt does not work with a sorted ArrayCollection (and prolly not filtered? don't know). The item will be added according to the sort criteria.

However, I needed a sorted ArrayCollection (alpha) with a "Select All" option at the top, so this is how I proceeded:

An array can be sorted easily (array.sort), so I first created an Array. Then I Looped the ArrayCollection and added the item from the ArrayCollection on which I wished to sort to the array. This new array was then sorted.

The newly sorted array was looped and within this loop, the ArrayCollection was looped again. If a match was found on the sorted item, I added this object to a new ArrayCollection but also created a new property of the object added called "sortOrder" which was set to the loop count.

Next the "Select All" Object was created and its sortOrder set to -1.

Finally, numeric sort was created on the sortOrder field of the ArrayCollection and Voila -- it worked.

Perhaps someone has a more elegant solution but I was in a hurry and it worked darn it.

Hope this helps someone.

在你怀里撒娇 2024-12-04 05:21:14
addItemAt() is adding item to the specified index.

*例如:
*

    <fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        [Bindable]
        private var myArray:ArrayCollection= new ArrayCollection([
            {student:'one',subject:'2'},
            {student:'two',subject:'4'},
            {student:'three',subject:'5'},
            {student:'four',subject:'6'}
        ]);

        protected function addArrayCollectioninRuntime(event:MouseEvent):void
        {
            myArray.addItemAt({student:nameTxtinput.text,subject:subjectTxtinput.text},3);


        }

    ]]>
</fx:Script>
<s:layout>
    <s:VerticalLayout/>
</s:layout>
<s:Form>
    <s:FormItem label="Student Name :">
        <s:TextInput id="nameTxtinput"/>
    </s:FormItem>
    <s:FormItem label="Student Subject :">
        <s:TextInput id="subjectTxtinput" />
    </s:FormItem>
    <s:Button label="Submit" click="addArrayCollectioninRuntime(event)"/>
</s:Form>
<mx:DataGrid dataProvider="{myArray}" id="dGrid" >
    <mx:columns>
        <mx:DataGridColumn dataField="student" id="stud"/>
        <mx:DataGridColumn dataField="subject" id="sub"/>
    </mx:columns>
</mx:DataGrid>
addItemAt() is adding item to the specified index.

*for example:
*

    <fx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        [Bindable]
        private var myArray:ArrayCollection= new ArrayCollection([
            {student:'one',subject:'2'},
            {student:'two',subject:'4'},
            {student:'three',subject:'5'},
            {student:'four',subject:'6'}
        ]);

        protected function addArrayCollectioninRuntime(event:MouseEvent):void
        {
            myArray.addItemAt({student:nameTxtinput.text,subject:subjectTxtinput.text},3);


        }

    ]]>
</fx:Script>
<s:layout>
    <s:VerticalLayout/>
</s:layout>
<s:Form>
    <s:FormItem label="Student Name :">
        <s:TextInput id="nameTxtinput"/>
    </s:FormItem>
    <s:FormItem label="Student Subject :">
        <s:TextInput id="subjectTxtinput" />
    </s:FormItem>
    <s:Button label="Submit" click="addArrayCollectioninRuntime(event)"/>
</s:Form>
<mx:DataGrid dataProvider="{myArray}" id="dGrid" >
    <mx:columns>
        <mx:DataGridColumn dataField="student" id="stud"/>
        <mx:DataGridColumn dataField="subject" id="sub"/>
    </mx:columns>
</mx:DataGrid>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文