开发者

AddItemAt() of arraycollection is not working in flex

I am trying to add an ite开发者_如何学Pythonm 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.


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.


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜