13 Replies Latest reply on Jan 23, 2009 1:24 PM by Developer504

    DataGrid headers from XML file

    Developer504 Level 1
      I have a datagrid with some headers that are supposed to be determined dynamically from an XML file. I thought this was working but I just noticed they aren't.

      This is my datagrid column entry:

      <mx:DataGridColumn headerText="{bkDs..banks[0].@bank1}" dataField="bank1" width="{this.width * 0.11}"
      textAlign="right" labelFunction="Bank1BalForm"/>

      This is the structure of my XML file:

      <bankCodes>
      <banks bank1="ABC Bank" bank2="National Bank" bank3="Regional Bank" bank4="City Bank" bank5="Energy Bank" bank6="Credit Bank" bank7="Giant Bank"/>
      </bankCodes>

      This is put into an XMLList:
      [Bindable] public var bkDs:XMLList;

      private function handleXmlDataBK(event:ResultEvent):void
      {bkDs=XMLList(event.result.banks);}

      <mx:HTTPService id="bkWs" url="{bkPath}" result="handleXmlDataBK(event)" resultFormat="e4x"/>


      I notice I'm getting two warnings:

      Data binding will not be able to detect changes to XMLList "banks", need an XML instance
      Data binding will not be able to detect changes when using square bracket operator. For Array, please use ArrayCollection.GetItemAt() instead.

      I'm not sure if I should put this XML file into an XMLListCollection or what? What I'm getting is just "Bank1","Bank2" etc. instead of "ABC Bank", "National Bank" etc.

      G.
        • 1. Re: DataGrid headers from XML file
          Level 7

          "Developer504" <webforumsuser@macromedia.com> wrote in message
          news:gladvd$kke$1@forums.macromedia.com...
          >I have a datagrid with some headers that are supposed to be determined
          > dynamically from an XML file. I thought this was working but I just
          > noticed
          > they aren't.
          >
          > This is my datagrid column entry:
          >
          > <mx:DataGridColumn headerText="{bkDs..banks[0].@bank1}" dataField="bank1"
          > width="{this.width * 0.11}"
          > textAlign="right" labelFunction="Bank1BalForm"/>

          Try
          XML(bkDs..banks[0]).attribute('bank1')

          HTH;

          Amy


          • 2. Re: DataGrid headers from XML file
            Developer504 Level 1
            Just tried that, I get the same result, the only change was that the second warning went away though the first one is still there.

            G.
            • 3. Re: DataGrid headers from XML file
              rtalton Level 4
              The data binding warning are a result of using e4x expressions. One way to avoid this is to filter your data before assigning it to the Data Grid. Unfortunately, your xml file does not lend itself to that since data from several banks are grouped together as attributes of the 'banks' element.
              Consider re-structuring your xml file and filtering it before assigning it to the bkDs variable.
              Also, be aware that the Data Grid automatically converts your XMLList into an XMLListCollection; no need to do this yourself. See the help docs under "ListBase" for details of these conversions.
              • 4. Re: DataGrid headers from XML file
                Developer504 Level 1
                I'm not sure I understand what you mean by filtering, but could you suggest an alternative structure for my XML file?

                All I need is to be able to predict where each bank name goes because each one has to line up with a specific column so it will match the data. I'm not sure how else to structure the file but since I guess Flex won't read it this way I have to change it some other way. Would this work?

                <bankCodes>
                <banks bankId="bank1" bankName="ABC Bank"/>
                <banks bankId="bank2" bankName="National Bank" />
                <banks bankId="bank3" bankName="Regional Bank" />
                <banks bankId="bank4" bankName="City Bank"/>
                <banks bankId="bank5" bankName="Energy Bank" />
                <banks bankId="bank6" bankName="Credit Bank" />
                <banks bankId="bank7" bankName="Giant Bank"/>
                </bankCodes>

                And if so, how do I get the right value into the right column on my DataGrid?

                G.
                • 5. Re: DataGrid headers from XML file
                  rtalton Level 4
                  You are filtering when you write "{bkDs..banks[0].@bank1}".
                  I tend to structure xml data more like database records, avoiding attributes when I can. Just the way I approach, and I'm sure there are plenty of other ways.
                  Assign the following to your XMLList:
                  {bkDs=XMLList(event.result.bank)}
                  ...and assign it to the data grid dataProvider={bkDs}.
                  The column names should take care of themselves at this point.
                  • 6. Re: DataGrid headers from XML file
                    Level 7

                    "Developer504" <webforumsuser@macromedia.com> wrote in message
                    news:glaisj$qqc$1@forums.macromedia.com...
                    > Just tried that, I get the same result, the only change was that the
                    > second warning went away though the first one is still there.

                    I'm not sure what the second warning vs. the first one.


                    • 7. Re: DataGrid headers from XML file
                      Developer504 Level 1
                      rtalton,

                      Thanks for the help, I'm not certain where to put this code though.

                      "Assign the following to your XMLList:
                      {bkDs=XMLList(event.result.bank)}
                      ...and assign it to the data grid dataProvider={bkDs}."

                      Does this go in an enent handler for the HTTPService call?

                      G.
                      • 8. Re: DataGrid headers from XML file
                        rtalton Level 4
                        I put together a working example for you. Also fixed some typos I posted yesterday. This should help you see how to structure your xml data, retrieve it and set up the dataprovider, and also hook it into the data grid. Some notes:
                        1) The XMLList is automatically converted into an XMLLIstCollection by the DataGrid, so you don't need to do it. Your variable typed as XMLList was a good way to go!
                        2) You set up a resultEvent handler which is great. Be careful of the syntax here and point your XMLList var, "bkDs", to the repeating node of your XML, not the root tag. Once you do this, you no longer need to use filtering expressions in the dataprovider tag of your data Grid--no more compiler warnings about "Data binding will not be able to detect changes to...". The thing here is to filter data ahead of time before you put it into the Data Grid.
                        3) Now without all the attributes you were using in the data provider, I think this will be easier to manipulate, filter, sort and search using the XMLListCollection's various methods. And that will make your life much easier down the road as you add more capabilities to your app.

                        Try this sample out and edit to suit. Good luck!
                        • 9. DataGrid headers from XML file
                          Developer504 Level 1
                          Thanks alot for doing all that, i can't do it that way though, I need the bank names for the column headers, the actual data within the columns of the datagrid comes from another source. This is the code for my Datagrid:


                          • 10. Re: DataGrid headers from XML file
                            rtalton Level 4
                            Wow you just lost me. If I'm hearing you correctly, you want to use TWO different data providers for ONE data grid? One for the columns' headerText and one for the cells in that column? You really need to merge the two datasets together and use that as the dataprovider.
                            What your code shows is you are making the assumption that the order your cash balance data are in will be the same order as the bank names data. If that were true, you could just hard code the header text.
                            • 11. Re: DataGrid headers from XML file
                              Developer504 Level 1
                              yes that is true, but the client wants to be able to change it. All the data comes out of SQL tables fed by another application they use internally. So I can hard code the header text but if they change their banks later I might not be around to change their code :)
                              • 12. Re: DataGrid headers from XML file
                                Developer504 Level 1
                                The funny thing is, I could swear that original code used to work. I got from somebody on here, it seemed like it was working at one point, I don't know what changed.
                                • 13. DataGrid headers from XML file
                                  Developer504 Level 1
                                  I just figured it out i changed the expression to read:

                                  headerText="{bkWs.lastResult.banks.@bank1}"

                                  and that worked.

                                  Thanks for all the help though guys much appreciated.