4 Replies Latest reply on Nov 6, 2009 11:59 PM by Flex harUI

    sorting a DataGrid without changing the content

    NickPick1

      I have a datagrid which is shown in a chart. I'd like to have it sorted upside down the dataField "x" without changing the chart, which is bound to the same arrayCollection. How can I do this without changing the chart?

       

       

      <mx:DataGrid sortableColumns="false"  width="480" height="156" dataProvider="{myArrayCollection}" editable="false" y="581" enabled="true" x="10">

                <mx:columns>

                      <mx:DataGridColumn id="col1" dataField="x" headerText="Trade #" />

                      <mx:DataGridColumn id="col2" dataField="p" headerText="Price" />

                      <mx:DataGridColumn id="col3" dataField="n" headerText="Users" />

                  </mx:columns>

      </mx:DataGrid>

       

      thanks

      nicolas

        • 1. Re: sorting a DataGrid without changing the content
          Flex harUI Adobe Employee

          You need a second arraycollection that shares the same list as the first.

           

          Alex Harui

          Flex SDK Developer

          Adobe Systems Inc.

          Blog: http://blogs.adobe.com/aharui

          • 2. Re: sorting a DataGrid without changing the content
            NickPick1 Level 1

            Ok, I made a second arrayCollection which is also binding, but when I sort the second one it appears to effect the first one as well. Is that because both are binding?

             

            I made

            ArrayCollection2=ArrayCollection1;

             

            If ArrayCollection 2 is sorted, this has a direct effect on ArrayCollection1, which I find surprising. How can I avoid this? Is there any way to unbind those two from each other?

            • 3. Re: sorting a DataGrid without changing the content
              Subeesh Arakkan Level 4

              Hi,

               

              Both the collections hold the reference to the same objects. You need to use ObjectUtil class to copy the objects. You can use the belowing method to copy arraycollection

                              private function copyCollection(source:ArrayCollection):ArrayCollection
                            {
                                 var target:ArrayCollection = new ArrayCollection();
                                 for each ( var item:Object in source)
                                 {
                                      target.addItem(ObjectUtil.copy(item));
                                 }
                                 return target;
                            }
              
              • 4. Re: sorting a DataGrid without changing the content
                Flex harUI Adobe Employee

                ArrayCollection is a view of a source Array.  If you create a second view, it can have a different sort order because sort and filters are properties of a view.

                 

                Just assigning AC2 = AC1 simply has AC2 referencing the same view so sorting will have an effect on all consumers of that view.  However, if you do:

                 

                AC2 = new ArrayCollection();

                AC2.list = AC1.list

                 

                then you can sort AC2 and AC1 will not reflect the sort and vice-versa.  But, if you add an item to AC2 or AC1 it will be reflected in the other AC.  If you modify a property of an item in one AC, the other AC will see it.

                 

                If you do:

                 

                AC2 = new ArrayCollection()

                AC2.source = AC1.source

                 

                You can also sort AC2 and AC1 will not reflect the sort and vice-versa.  But, if you add an item to AC2 or AC1, the other will not reflect that change unless you force an update on the other.  Same is true if you modify a property on an item in the AC.  The other AC won't know about it until you force an update.

                 

                If you want to modify properties of an item in one AC and not every have it reflected in the other AC, then you should do a copy as the previous responder suggested.  I believe you want to use the pattern that assings AC2.list = AC1.list.

                 

                 

                Alex Harui

                Flex SDK Developer

                Adobe Systems Inc.

                Blog: http://blogs.adobe.com/aharui

                1 person found this helpful