7 Replies Latest reply on Apr 12, 2007 1:22 PM by JeremySeverson

    DataGrid and XML with only 1 entry

    JeremySeverson
      Hi All,

      I have a datagrid set up that is displaying an xml file. Seems to work fine until there is only 1 entry in my xml file, then I get no content displayed in the datagrid.

      Here is my code that I am using for my app,

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" initialize="xmlDat.send()">

      <mx:Script>
      <![CDATA[
      import mx.controls.dataGridClasses.DataGridColumn;
      import mx.collections.ArrayCollection;
      import mx.rpc.events.ResultEvent;

      [Bindable]
      private var myData:ArrayCollection;

      private function resultHandler(event:ResultEvent):void{
      myData = event.result.projects.project;
      }
      ]]>
      </mx:Script>

      <mx:HTTPService id="xmlDat" url="data/whiteboard.xml" result="resultHandler(event)"/>

      <mx:Panel layout="absolute" title="Project Whiteboard" height="600" width="1000" verticalCenter="-1" horizontalCenter="-1">
      <mx:DataGrid dataProvider="{myData}" width="980" height="560" y="0" x="0" verticalScrollPolicy="on" variableRowHeight="true" editable="false">
      <mx:columns>
      <mx:DataGridColumn headerText="Due Date" dataField="dueDate" width="85"/>
      <mx:DataGridColumn headerText="DVD ID" dataField="dvdId" width="65"/>
      <mx:DataGridColumn headerText="School Name" dataField="schoolName" width="275"/>
      <mx:DataGridColumn headerText="City" dataField="city" width="100"/>
      <mx:DataGridColumn headerText="St" dataField="state" width="35"/>
      <mx:DataGridColumn headerText="Qty" dataField="quantity" width="45"/>
      <mx:DataGridColumn headerText="Image Prep" dataField="imagePrep" itemRenderer="render.imgRender"/>
      <mx:DataGridColumn headerText="Video Prep" dataField="videpPrep" itemRenderer="render.vidRender"/>
      <mx:DataGridColumn headerText="Sequencing" dataField="sequencing" itemRenderer="render.seqRender"/>
      <mx:DataGridColumn headerText="FTP" dataField="ftp" itemRenderer="render.ftpRender"/>
      </mx:columns>
      </mx:DataGrid>
      </mx:Panel>

      </mx:Application>
        • 1. Re: DataGrid and XML with only 1 entry
          babo_ya Level 3
          Not sure but update
          myData = event.result.projects.project;
          To
          myData = event.result.projects;
          • 2. Re: DataGrid and XML with only 1 entry
            JeremySeverson Level 1
            that won't work because then I won't get any data.

            I guess it would of probally helped if I included the xml structure so here it is,

            <?xml version="1.0"?>
            <projects>
            <project>
            <dueDate>10/20/1979</dueDate>
            <dvdId>456789</dvdId>
            <schoolName>Blaine Highschool</schoolName>
            <city>Blaine</city>
            <state>MN</state>
            <quantity>225</quantity>
            <imagePrep>
            </imagePrep>
            <videoPrep>
            </videoPrep>
            <sequencing>
            <station>Ftp Station 1</station>
            <color>FF0000</color>
            </sequencing>
            <ftp>
            <station>Ftp Station 1</station>
            <color>FF0000</color>
            <station>Ftp Station 2</station>
            <color>0000FF</color>
            <station>Ftp Station 3</station>
            <color>00FF00</color>
            </ftp>
            </project>
            </projects>
            • 3. Re: DataGrid and XML with only 1 entry
              JeremySeverson Level 1
              Anyone have any knowledge on this?
              • 4. Re: DataGrid and XML with only 1 entry
                peterent Level 2
                When there is only 1 entry in the result you don't have an Array any more, you have a scalar. That's because the Player doesn't know you intend or want to always have an Array.

                Look up ArrayUtils and the toArray function. That should help.
                • 5. Re: DataGrid and XML with only 1 entry
                  JeremySeverson Level 1
                  quote:

                  Originally posted by: peterent
                  When there is only 1 entry in the result you don't have an Array any more, you have a scalar. That's because the Player doesn't know you intend or want to always have an Array.

                  Look up ArrayUtils and the toArray function. That should help.


                  This sounds like just what I need. The only problem I am having now is how to implement it into my code. When I add it to the code it works for when I have one entry but if it is more than that then no data shows.

                  Anyone have some ideas on this? Here is my updated code that works for one entry in the xml but not more than that.

                  <?xml version="1.0" encoding="utf-8"?>
                  <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApp()">

                  <mx:Script>
                  <![CDATA[
                  import mx.controls.dataGridClasses.DataGridColumn;
                  import mx.collections.ArrayCollection;
                  import mx.rpc.events.ResultEvent;
                  import mx.utils.ArrayUtil;

                  [Bindable]private var myData:ArrayCollection;

                  private var uintPollInterval:uint = 10000;
                  private var timer:Timer = new Timer(uintPollInterval);

                  private function initApp():void
                  {
                  xmlDat.send();
                  timer.addEventListener(TimerEvent.TIMER, onTimer);
                  timer.start();
                  }

                  private function onTimer(event:TimerEvent):void
                  {
                  xmlDat.send();
                  timer.start();
                  }

                  ]]>
                  </mx:Script>

                  <mx:HTTPService id="xmlDat" url="data/whiteboard.xml" result="resultHandler(event)"/>

                  <mx:Panel layout="vertical" title="Project Whiteboard" height="100%" width="1000" verticalCenter="-1" horizontalCenter="-1">
                  <mx:DataGrid dataProvider="{ArrayUtil.toArray(xmlDat.lastResult.projects.project)}" width="980" height="100%" y="0" x="0" verticalScrollPolicy="on" variableRowHeight="true" editable="false">
                  <mx:columns>
                  <mx:DataGridColumn headerText="Due Date" dataField="dueDate" width="85"/>
                  <mx:DataGridColumn headerText="DVD ID" dataField="dvdId" width="65"/>
                  <mx:DataGridColumn headerText="School Name" dataField="schoolName" width="275"/>
                  <mx:DataGridColumn headerText="City" dataField="city" width="100"/>
                  <mx:DataGridColumn headerText="St" dataField="state" width="35"/>
                  <mx:DataGridColumn headerText="Qty" dataField="quantity" width="45"/>
                  <mx:DataGridColumn headerText="Image Prep" dataField="imagePrep" itemRenderer="render.imgRender"/>
                  <mx:DataGridColumn headerText="Video Prep" dataField="videpPrep" itemRenderer="render.vidRender"/>
                  <mx:DataGridColumn headerText="Sequencing" dataField="sequencing" itemRenderer="render.seqRender"/>
                  <mx:DataGridColumn headerText="FTP" dataField="ftp" itemRenderer="render.ftpRender"/>
                  </mx:columns>
                  </mx:DataGrid>
                  <mx:ControlBar horizontalAlign="right">
                  <mx:Button label="Update" click="xmlDat.send()"/>
                  </mx:ControlBar>
                  </mx:Panel>

                  </mx:Application>
                  • 6. Re: DataGrid and XML with only 1 entry
                    Rahil Kidwai
                    Write a simple method that takes your xml as argument. The method should return an array containing objects filled with data extracted from the xml.
                    Call this method to set the dataprovider of the grid.
                    • 7. Re: DataGrid and XML with only 1 entry
                      JeremySeverson Level 1
                      quote:

                      Originally posted by: Rahil Kidwai
                      Write a simple method that takes your xml as argument. The method should return an array containing objects filled with data extracted from the xml.
                      Call this method to set the dataprovider of the grid.


                      Seems like the way to go, but could you supply with an example. Does not have to pretain to my variables below, just somethiing to see and understand the structure you described.

                      Thanks for any help you can provide