9 Replies Latest reply on Jan 5, 2007 7:20 PM by kimbflex

    xml node name to datagrid

    kimbflex

      How do I put the names of my xml nodes into a datagrid column?

      e.g. xml:
      <records>
      <first_name d="kim"/>
      <middle_names d="r"/>
      <surname d="b"/>
      </records>

      e.g. mxml:
      <mx:DataGrid dataProvider="{gData}" height="269" id="dgrid">
      <mx:columns>
      <mx:DataGridColumn headerText="Record" dataField="what_do_i_put_in_here???"/>
      <mx:DataGridColumn headerText="Data" dataField="@d"/>
      </mx:columns>
      </mx:DataGrid>
        • 1. Re: xml node name to datagrid
          ntsiii Level 3
          You must use a labelFunction. Return either xmlItem.name() or localName(). See the XML class in the docs for more info.

          There are some examples on CFLEX:
          http://www.cflex.net

          Post back if you need more help.

          Tracy
          • 2. Re: xml node name to datagrid
            davidmedifit Level 1
            KimbFlex,
            This is what I did, and there are some changes I made to your XML - Tracy, I value your opinion, if you see something "wrong" with what I'm doing, please post back.

            XML:
            <records>
            <first_name>kim</first_name>
            <middle_names>r</middle_names>
            <surname>b</surname>
            </records>
            <records>
            <first_name>paul</first_name>
            <middle_names>q</middle_names>
            <surname>x</surname>
            </records>

            I added a variable in the script block:
            [Bindable] private var nameList:ArrayCollection;

            <mx:HTTPService id="gData2" url="test2.xml" useProxy="false" result="nameList=gData2.lastResult.records" />
            <mx:DataGrid dataProvider="{nameList}" height="269" id="dgrid">
            <mx:columns>
            <mx:DataGridColumn headerText="first name" dataField="first_name"/>
            <mx:DataGridColumn headerText="last name" dataField="surname"/>
            </mx:columns>
            </mx:DataGrid>

            WARNING: I noticed that this code shows nothing in the datagrid when there is ONE "records" node in the XML - I'm pretty sure that is to do with converting the result to an arrayCollection - I think I've come across this issue before, and I'm pretty sure I've overcome it too. I'll dig back through my notes and see what I can find. This may work for you in you KNOW you have more than one record.

            If you find a better solution, please post back, I'd be interested to see what your finished code looked like.

            Cheers,

            David
            • 3. Re: xml node name to datagrid
              ntsiii Level 3
              Perhaps I mis-understood the question. I thought we were trying to display "the names of my xml nodes...".

              David, why do you advise changing the xml format? What Kimbflex has should work fine. I interpreted the post to mean that the "data" column, referencing the @d attribute was working.

              Tracy
              • 4. Re: xml node name to datagrid
                kimbflex Level 1
                Yeah, more help please!

                kimb
                • 5. Re: xml node name to datagrid
                  ntsiii Level 3
                  So where are you at now? Am I correct in my interpretation of you question?
                  Tracy
                  • 6. Re: xml node name to datagrid
                    kimbflex Level 1
                    Yes you are correct but what do I put in the 'what_do_i_put_in_here' bit of my example code? I tried what you suggested.

                    kim
                    • 7. Re: xml node name to datagrid
                      kimbflex Level 1
                      hmm,still can't see any way to do this, you would think that just the name() property would work.

                      Am I missing something?
                      • 8. Re: xml node name to datagrid
                        ntsiii Level 3
                        No, dataField requires that the data be in a first level property of the item. For xml this is either the text node, or an immediate child node.

                        You must use a labelFunction. Return either xmlItem.name() or localName(). See the XML class in the docs for more info.

                        There are some examples on CFLEX:
                        http://www.cflex.net

                        Tracy
                        • 9. Re: xml node name to datagrid
                          kimbflex Level 1
                          that is a pain, I tried that but it doesn't work in the context of what I was trying to achieve.

                          It seems like Flex/ XML is trying really hard to be data friendly but compared to something like php/mysql it is still pretty poor, or at least pretty hard to understand.

                          Don't make us jump through hoops.

                          kimb