4 Replies Latest reply on Nov 10, 2006 1:14 AM by iquaaani

    xml to datagrid problem

    iquaaani
      I have of xml file that i'm trying to populate to datagrid. I'm able to populate all attributes to datagrid but the rest is missing. Let me show what i mean.
      This is the xml file
      <col>
      <c R = "250" G = "250" B = "250">item1</c>
      <c R = "170" G = "121" B = "67">item2</c>
      <c R = "159" G = "108" B = "72">item3</c>
      </col>
      and here is my mxml:
      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">
      <mx:XML format="e4x" id="myXmlFile" source="Database/myCol.xml"/>
      <mx:DataGrid x="12.5" y="43" dataProvider="{myXmlFile.c}" id="taulukko">
      <mx:columns>
      <mx:DataGridColumn headerText="Nimi" width="140" dataField="c"/>
      <mx:DataGridColumn headerText="R" dataField="@R"/>
      <mx:DataGridColumn headerText="G" dataField="@G"/>
      <mx:DataGridColumn headerText="B" dataField="@B"/>
      </mx:columns>
      </mx:DataGrid>
      </mx:Application>
      I can see the R G B values when i compile project but not item1 item 2 and item 3 which are at dataField="c"/
      where is the but

        • 1. Re: xml to datagrid problem
          ntsiii Level 3
          I would think that what you have should work.

          If you can't get it to work, use a labelFunction() for the first column. You will want somethin like:
          return item.toString();
          or item.valueOf().toString()

          I don't recall the exact syntax to get the textnode value .

          Tracy
          • 2. Re: xml to datagrid problem
            ntsiii Level 3
            Here is an example using labelFunction:

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApp()">
            <mx:Script><![CDATA[
            [Bindable]
            private var myXML:XML;
            private function initApp():void
            {
            myXML = <col>
            <c R = "250" G = "250" B = "250">item1</c>
            <c R = "170" G = "121" B = "67">item2</c>
            <c R = "159" G = "108" B = "72">item3</c>
            </col>;
            }//initApp

            private function lfTextNode(oItem:Object,iCol:int):String
            {
            var xmlItem:XML = XML(oItem); //this helps us see the xml object members
            return xmlItem.toString(); //works
            //return xmlItem.valueOf().toString(); //works
            //return xmlItem.text().toString(); //works
            }//lfTextNode

            ]]></mx:Script>

            <mx:DataGrid x="12.5" y="43" dataProvider="{myXML.c}" id="taulukko">
            <mx:columns>
            <mx:DataGridColumn headerText="Nimi" width="140" labelFunction="lfTextNode"/>
            <mx:DataGridColumn headerText="R" dataField="@R"/>
            <mx:DataGridColumn headerText="G" dataField="@G"/>
            <mx:DataGridColumn headerText="B" dataField="@B"/>
            </mx:columns>
            </mx:DataGrid>
            </mx:Application>
            • 3. Re: xml to datagrid problem
              ntsiii Level 3
              Hah! I fiured out how to do it with the labelField:

              To access the text node of the current item, use an asterisk(*):
              dataField="*"

              Tracy
              • 4. Re: xml to datagrid problem
                iquaaani Level 1
                quote:

                Originally posted by: ntsiii
                Hah! I fiured out how to do it with the labelField:

                To access the text node of the current item, use an asterisk(*):
                dataField="*"

                Tracy

                Thank you very much for the answer. After all the solution was quite simple. Thanks also for showing usage of label function.

                -Thomas