8 Replies Latest reply on Jul 9, 2008 1:55 PM by bitwyse

    ArrayCollection vs. XML e4x

    bitwyse Level 1
      I have an application where most of my data comes from web services returning arrayColletctions of Objects. I use the arrayCollections as my data provider for various data grids. I have built a text field for filtering my data grids by using the filterFunction of the arrayCollection. Everything works great!

      Now I am populating another datagrid with XML directly thus I am using e4x for all my access to the data. I am having a lot of difficulty with the most basic functions in my datagrid sorting columns and don't have a clue how to implement the filterFunction. On my datagrid using e4x I implemented a labelFunction for each column to extract the data as well as a sortCompareFunction. For some reason my sortCompare function is not working. Some of the data is sorted but not all of it.

      Does this sound familiar to anyone? Any ideas where to look? Below is some sample code to see if that helps . . .

      Thanks for any help you can offer.

        • 1. Re: ArrayCollection vs. XML e4x
          ntsiii Level 3
          Dates can be tricky.

          Trace or step through the functions to see what is not working correctly.
          • 2. Re: ArrayCollection vs. XML e4x
            bitwyse Level 1
            I have a number of times. Each time the data is correct and it is sorting correctly for each two items compared. Yet they records are not sorted as a whole.

            I'm used to using array collections from the return of my web services. This however, is so much faster to use the XML and e4x to fetch the data but all other aspects of my data grid from label functions to sorting is very cumbersome and problematic. Is there a way to convert the XML data easily to an Array Collection without having to walk each item in a loop ?
            • 3. Re: ArrayCollection vs. XML e4x
              bitwyse Level 1
              Anyone have ideas how to convert xml into an arrayCollection?
              • 4. Re: ArrayCollection vs. XML e4x
                Gregory Lafrance Level 6
                I often handle xml data with HTTPService as follows:

                import mx.rpc.events.ResultEvent;
                import components.ButtonRenderer;

                private var resultObj:Object = new Object();

                private function resultHandler(event:ResultEvent):void{
                resultObj = Object(event.result);

                I wonder if this Object could more easily be converted to AC?
                • 5. Re: ArrayCollection vs. XML e4x
                  When you access a ListBase (like DataGrid) component's dataProvider property it will return an ICollectionView object that exposes the filterFunction property regardless of whether the dataProvider is an ArrayCollection or XMLListCollection.

                  Below is a simple example that illustrates the use of a filterFunction with an XMLListCollection. The idea is exactly the same as an ArrayCollection.

                  It would help us help you if you could provide some of the XML you're loading to see where things aren't working for you.

                  • 6. Re: ArrayCollection vs. XML e4x
                    bitwyse Level 1
                    @Greg I don't think I can just create an object like that. I will try it but I can't imagine that working here . . .
                    @Vario the code you can see in the previous post, so here is a snapshot of the XML. I must be doing something wrong here. I thought I could use the same filtering you are talking about like I do with all my datagrid's that have objects for dataproviders. . . .

                    • 7. ArrayCollection vs. XML e4x
                      VarioPegged Level 2
                      You have several problems with your code here, and it doesn't have anything to do with the data being an ArrayCollection or an XMLListCollection.

                      One of the problems is the fact that you have "On Behalf Of" with an extra space between Behalf and Of. Your sortCompareFunction could never even compare the right values. I probably don't have to say too much more on comparing strings that contain spaces, right? ;)
                      <mx:DataGridColumn headerText="On Behalf Of" dataField="text.(@columnnumber == '2')" width="80" labelFunction="getNotesName" sortCompareFunction="compareNonDateValues">
                      The second issue is, you cannot use an E4X expression for the dataField (it won't evaluate to anything), this is why you're using a labelFunction on that column.
                      The third is when you're trying to compare the strings within compareNonDateValues(), you're comparing the whole object (you used data1 and data2), when you should be comparing the strings data1Str and data2Str :

                      return ( data1Str.toLowerCase() < data2Str.toLowerCase() ) ? -1 : ( data1Str.toLowerCase() > data2Str.toLowerCase() ) ? 1 : 0;

                      Not to want to rag on you here, but I hope that you're the only one who has to look at that nested ternary. It's just very hard to read and even harder to catch a simple mistake.

                      The following works for me, see if it does for you. I've abbreviated the XML elements, but you get the idea.

                      • 8. Re: ArrayCollection vs. XML e4x
                        bitwyse Level 1
                        TS ,

                        Thank you so much for your finding my bugs hehe. The issue was all due to the data1.toString.toLower.......
                        Once I corrected everything worked great. The space issue was a typo here. Not acutually in my code.

                        No worries on critiquing the code. I appreciate everyones feedback good, bad or otherwise.

                        You rock!!!!!