6 Replies Latest reply on Aug 26, 2009 11:16 AM by webvalue

    Location in CollectionEvent

    webvalue Level 1



      I am using CollectionEvent to track which item in the ArrayCollection has been updated. However, except location, there is no other thing that can allow you to know which row is updated (a datagrid is changing the ArrayCollection.) However, location is only working when a row in the collection is "replaced", not "updated".


      Any idears on how to track which row in the collection has been updated?

        • 1. Re: Location in CollectionEvent
          babo_ya Level 3

          I had the same issue before. I ended up creating VO with index. And I make sure when adding an item.. I used addItem so I know the newly added item is always at the end.



          • 2. Re: Location in CollectionEvent
            Barna Biro Level 3

            I didn't play around with these things but why not simply compare the values from the old provider with the new one? Assuming your rows are not customly sorted then the comparing should reveal the correct index positions ( the changes ) that would also reflect the correct position in the grid. In case you are using some custom sorting that mixs up your rows then it might get tricky ( but you could also try sorting the new provider so the old and new are sorted the same way and compare them only after the sort ).

            • 3. Re: Location in CollectionEvent
              webvalue Level 1

              Will the comparison cost a lot? The collection could be big. Also, I assume you would use a loop to compare, right? Otherwise, how?

              • 4. Re: Location in CollectionEvent
                Barna Biro Level 3

                The comparision would definitely not be the most efficient solution, especially if you have large amounts of data. But if you don't load in your data in pieces then you don't really have too many options left. You either somehow compare the new data to the old one and update only the rows that have changed or you simply replace the old provider with the new data and that's it.

                • 5. Re: Location in CollectionEvent
                  babo_ya Level 3

                  Somehow use the indexing to update an item inside of the arraycollection.. for example, each item has an index value that matches the arraycollection index even after a sort or something else has been applied to the arrayCollection, you just need to make sure when updating item set the sort = null.


                  If you could create a sample app. I will be glad to help you with this issue.


                  Hope this help,

                  • 6. Re: Location in CollectionEvent
                    webvalue Level 1

                    Thank you for wanting to help. Here I made a small app. I 'd like to see what we can do in the function collectionChangeHandler, particularly, which record of the collection is changed.



                    <?xml version="1.0"?>

                    <!-- DataGrid control example. -->

                    <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()">





                    import mx.events.CollectionEvent;


                    import mx.collections.ArrayCollection;


                    private function init():void {

                    theAC.addEventListener(CollectionEvent.COLLECTION_CHANGE, collectionChangeHandler);



                    private function collectionChangeHandler(e:CollectionEvent):void {

                    trace("the collection has been changed.");








                    <mx:ArrayCollection id="theAC">

                            <mx:Array id="myArray">

                                <mx:Object firstName="John" lastName="Doe"/>

                                <mx:Object firstName="Matt" lastName="Anderson"/>





                        <mx:Panel title="DataGrid Control Example" height="100%" width="100%"

                            paddingTop="10" paddingLeft="10" paddingRight="10">


                            <mx:Label width="100%" color="blue"

                                text="Select a row in the DataGrid control."/>


                            <mx:DataGrid id="dg" width="100%" height="100%" rowCount="5" dataProvider="{theAC}" editable="true">


                                    <mx:DataGridColumn dataField="firstName" headerText="firstName" editable="true"/>

                                    <mx:DataGridColumn dataField="lastName" headerText="lastName" editable="true"/>