11 Replies Latest reply on Mar 25, 2010 10:17 AM by LSPNwebmaster

    Datagrid rearranging columns?

    aharpro
      I'm having a problem. This code i have written will import the XML but it rearranges the fields when i do this:
      "for (var subKey:String in data[0])"
      All of the fields come through but the subKey order is not in the order of the actual XML. Is there any way to get the "$ColumnNames[$w]" var from php over to flex in the same order?

      I don't know if i just reinvented the wheel, but it was definitely a good learning experience.

      MXML is first then PHP is second. Some of the code is copied off other examples. I didn't see any copyrights but let me know if there are any problems. O and the error thing doesn't work on the PHP page.


        • 1. Re: Datagrid rearranging columns?
          rtalton Level 4
          I'm not able to test your code, but I also use PHP on the back-end returning XML from a MySQL db and never had an issue like this.
          Have you tried simply assigning your result directly to the data grid:
          dg.dataProvider="{event.lastResult.record}"
          If you do this, the data grid will set itself up correctly with the results in naturalorder.
          Can you tell us what you are trying to achieve by manipulating the data grid's columns in ActionScript? Not sure why you are not letting the data grid do this for you. Maybe I'm missing something?
          • 2. Re: Datagrid rearranging columns?
            aharpro Level 1
            dg.dataProvider = event.result.record;

            OK so i did reinvent the wheel, but the fields still do not come in in the same order as the XML with this method.
            That will sort them in alphabetical order.
            • 3. Re: Datagrid rearranging columns?
              rtalton Level 4
              Good morning!
              I made a mistake; this:
              dg.dataProvider="{event.lastResult.record}"

              should've been:
              dataProvider="{xmlRPC.lastResult.record}"
              ...as the dataProvider property of your datagrid. My aplologies.

              Anyway, I put together a sample for you to see:
              http://www.anaheimjobs.net/samples/dataservice/

              If you right-click and view source, you'll see the xml I used. Notice that the data always comes through in natural order into the datagrids. The only difference between the two datagrids is that I specified column oder in one; the other is done by Flex and you'll notice that Flex sorts the columns but NOT the data. Also note that I'm using the default result format for the HTTPService, which is "object". Had I used "e4x", I would always specify the columns, as Flex would not do it.

              If you retrieve your data this way, I think you'll find it much easier to work with the Data Grid component.
              • 4. Re: Datagrid rearranging columns?
                ntsiii Level 3
                I advise against usng the default resultFormat. Use e4x and either use the XML directly, or if you have the need, use the xml to build an ArrayCollection of strongly typed value objects.

                Flex shoud build the default columns using e4x also. I have not seen a case where it does not, but then I rarely let Flex build my columns.

                The enumeration order of for..in and for..each loops is NOT guaranteed.

                If your columns are dynamic, but you still want to specify an order, you can dynamically build the columns. I have a simple xample of this on www.cflex.net.

                Tracy
                • 5. Re: Datagrid rearranging columns?
                  rtalton Level 4
                  Hi Tracy,
                  I put up an example showing how Flex does not build columns automatically when specifying e4x as the result format of an HTTPService.
                  If you see something I've done incorrectly, please let me know. This is the first time I've tried this, and I was surprised by this behavior. I always specify the columns, and just did it this way while coming up with simple code fo the OP.
                  http://www.anaheimjobs.net/samples/e4xresult/

                  Of course, specifying e4x as the resultFormat makes it easier to work with the data.
                  • 6. Re: Datagrid rearranging columns?
                    ntsiii Level 3
                    I'd like to try that example, can you post the code? I think the url you posted is incomplete.

                    Tracy
                    • 7. Re: Datagrid rearranging columns?
                      rtalton Level 4
                      Sorry, I took it down this morning. It's at home and I'll put the files back up tonight and leave it for a couple of days.
                      • 8. Re: Datagrid rearranging columns?
                        ntsiii Level 3
                        Or post it here.
                        Tracy
                        • 9. Re: Datagrid rearranging columns?
                          rtalton Level 4
                          Here it is.
                          • 10. Re: Datagrid rearranging columns?
                            LSPNwebmaster

                            I am look for a solution to a similar problem.

                             

                            I've got this nice little .fla that displays a DataGrid component (Flash CS3 Professional) using a DataProvider created from XML loaded from URLRequest/URLLoader (.asp source, but could be PHP, etc.).

                             

                            It works great except for one little annoying item.  The order of the columns seems to be totally arbitrary.

                             

                            I was expecting the order of the column to be the same as they occur in the XML DATA element.

                             

                            Here is a sample DATA element (there are serveral such DATA elements in the XML:

                            <DATA>
                              <AreaID>TNV</AreaID>
                              <AreaName>TN, VA</AreaName>
                              <MaxDvrHT>4</MaxDvrHT>
                              <BoardName>TN, VA</BoardName>
                              <RegionName>SOUTHEAST</RegionName>
                              <salesGroup>David</salesGroup>
                            </DATA>

                            Here is the function that builds DataGrid:


                            function returnAJAXcallback(evtObj:Event):void{
                            // executed by URLLoader event listener "complete"
                            // load the XML returned from .asp into XML object
                            var myXML:XML = XML(evtObj.target.data);   
                            // create a data provider from the XML
                            var myDP = new DataProvider(myXML);  
                            // create a DataGrid
                            var myDataGrid:DataGrid = new DataGrid;  
                            // set the data provider

                            myDataGrid.dataProvider=myDP;   
                            myDataGrid.width = 900;
                            myDataGrid.height = 500;
                            // show DataGrid on stage
                            addChild(myDataGrid);     

                            for each (var child:XML in myXML.DATA) {
                              trace(child) ;  // list each DATA segment
                            }
                            }

                             

                            Here is the order that the columns appear in the DataGrid (left to right):

                             

                            AreaName
                            RegionName
                            salesGroup
                            AreaID
                            MaxDvrHT
                            BoardName

                             

                            So, how in the world does Flash come up with this order?

                            Why not default to the order in which the columns appear in the XML?

                             

                            Now, if some nice person knows the answer (I'm pretty new to Flash), what do I need to do (with as little code as possible) to get the columns ordered in a similar fashion to the order in which the XML data elements appear?

                             

                            Thanks to all.

                            • 11. Re: Datagrid rearranging columns?
                              LSPNwebmaster Level 1

                              I found the answer in an online video in chapter seven, section "Configuring the Data Grid".

                              Total Training for Adobe Flash CS3: Professional ActionScript 3 UI & Data

                              By: John Ulliman

                               

                              // the following is how we control the order of the columns.
                              myDataGrid.columns = ["AreaID", "AreaName", "MaxDvrHT", "BoardName", "RegionName", "salesGroup"];

                               

                              Yep, that simple and it really works!