8 Replies Latest reply on Aug 10, 2007 2:21 PM by ntsiii

    Populate datagrid problem

    P Steven Level 1
      Hi there

      I am having problems populating my datagrid with xml data returned from an instance of a class.

      Here is the xml data that is returned from the instance

      <summaryScreen>
      <exam>
      <type>GCE</type>
      <qualification>Extended Project Qualification</qualification>
      <code>1991</code>
      <students>12</students>
      <marked>4</marked>
      <submitted>8</submitted>
      <series>november</series>
      </exam>
      <exam>
      <type>GCE</type>
      <code>1991</code>
      <qualification>Extended Project Qualification - TH</qualification>
      <students>14</students>
      <marked>3</marked>
      <submitted>4</submitted>
      <series>september</series>
      </exam>
      <exam/>
      </summaryScreen>

      And here is my code for the datagrid

      <mx:DataGrid dataProvider="{_summaryScreenData.getXMLData()}">

      <mx:columns>
      <mx:DataGridColumn headerText="Type" dataField="type" width="60" />
      <mx:DataGridColumn headerText="Qualification" dataField="qualification" width="140"/>
      <mx:DataGridColumn headerText="Code" dataField="code" width="140" />
      <mx:DataGridColumn headerText="Students" dataField="students" width="70" />
      <mx:DataGridColumn headerText="Marked" dataField="marked" width="70" />
      <mx:DataGridColumn headerText="Submitted" dataField="submitted" width="90"/>
      <mx:DataGridColumn headerText="Series" dataField="series" width="70"/>
      <mx:DataGridColumn headerText="Days Left" dataField="daysLeft"/>
      </mx:columns>

      </mx:DataGrid>

      Can anyone point me in the right direction to get this data into the datagrid?

      Thanks!

        • 1. Re: Populate datagrid problem
          Suren_kancherla Level 1
          Hi,
          please look at your XML there is a extra and wrong "<exam/>" closing tag.

          still you have a problem. Plz look at the code which I tried it worked fine.


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

          <mx:Model id="xyz">
          <summaryScreen>
          <exam>
          <type>GCE</type>
          <qualification>Extended Project Qualification</qualification>
          <code>1991</code>
          <students>12</students>
          <marked>4</marked>
          <submitted>8</submitted>
          <series>november</series>
          </exam>
          <exam>
          <type>GCE</type>
          <code>1991</code>
          <qualification>Extended Project Qualification - TH</qualification>
          <students>14</students>
          <marked>3</marked>
          <submitted>4</submitted>
          <series>september</series>
          </exam>
          </summaryScreen>
          </mx:Model>

          <mx:Script>
          <![CDATA[
          import mx.utils.ArrayUtil;
          ]]>
          </mx:Script>

          <mx:DataGrid dataProvider="{mx.utils.ArrayUtil.toArray(xyz.exam)}">

          <mx:columns>
          <mx:DataGridColumn headerText="Type" dataField="type" width="60" />
          <mx:DataGridColumn headerText="Qualification" dataField="qualification" width="140"/>
          <mx:DataGridColumn headerText="Code" dataField="code" width="140" />
          <mx:DataGridColumn headerText="Students" dataField="students" width="70" />
          <mx:DataGridColumn headerText="Marked" dataField="marked" width="70" />
          <mx:DataGridColumn headerText="Submitted" dataField="submitted" width="90"/>
          <mx:DataGridColumn headerText="Series" dataField="series" width="70"/>
          <mx:DataGridColumn headerText="Days Left" dataField="daysLeft"/>
          </mx:columns>

          </mx:DataGrid>
          </mx:Application>
          • 2. Populate datagrid problem
            P Steven Level 1
            Thank you so much for replying - I have spent all day trying to figure this out - and whilst it is not working properly yet - it is close

            Unfortunately it is displaying the tags and the values instead of just the values inside my datagrid

            e.g
            <type>GCE</type> is being displayed inside the datagrid cell instead of just GCE

            I have traced the following:

            trace (_summaryScreenData.getXMLData().exam);

            and get

            <exam>
            <type>GCE</type>
            <code>1991</code>
            <qualification>Extended Project Qualification</qualification>
            </exam>
            <exam>
            <type>GCE</type>
            <code>1991</code>
            <qualification>Extended Project Qualification - TH</qualification>
            </exam>


            I also traced it after calling
            myData = (mx.utils.ArrayUtil.toArray(_summaryScreenData.getXMLData().exam));
            trace (myData);

            and I get the exact same results
            <exam>
            <type>GCE</type>
            <code>1991</code>
            <qualification>Extended Project Qualification</qualification>
            </exam>
            <exam>
            <type>GCE</type>
            <code>1991</code>
            <qualification>Extended Project Qualification - TH</qualification>
            </exam>

            I have set the dataprovider as follows:
            <mx:DataGrid dataProvider="{myData}">

            And the myData is set as follows

            public var myData:Array;

            Any ideas?
            • 3. Re: Populate datagrid problem
              Suren_kancherla Level 1
              Please send your code, so that i could help you
              • 4. Re: Populate datagrid problem
                P Steven Level 1
                Hi there

                Do you mean paste the code here or send them to you?

                Thanks

                Paul
                • 5. Re: Populate datagrid problem
                  P Steven Level 1
                  I have discovered the problem seems to be happening when there are 2 sets of data

                  I have put some dummy xml data to return as follows. When there is only data for one exam, the datagrid is populated correctly. When there are 2 sets of exam data as in the example below, it displays the tags inside the datagrid cells as well as the values.

                  Any ideas how to solve this?

                  public function getXMLData():XML {

                  var testXML:XML = <summaryScreen>
                  <exam>
                  <examType>GCE</examType>
                  <qualification>Maths</qualification>
                  <code>1212</code>
                  </exam>

                  <exam>
                  <examType>GCE</examType>
                  <qualification>English</qualification>
                  <code>1212</code>
                  </exam>

                  </summaryScreen>


                  return testXML;
                  }
                  • 6. Re: Populate datagrid problem
                    ntsiii Level 3
                    Don't use "mx.utils.ArrayUtil.toArray" with XML. It is only for arrays as you get with mx:Model.

                    Is your class and function bindable? There is no argument on the function, so how is the framework going to know when to fire the binding?

                    Are you not getting binding warnings?

                    Use a lableFunction so you can debug the item objects in the dataProvider.

                    Tracy
                    • 7. Re: Populate datagrid problem
                      P Steven Level 1
                      Thank you Tracy

                      I have removed the mx.utils.ArrayUtil code and changed the data type of my variable holding the returned XML to XMLList and it works!!

                      Thank you for helping me sort this.

                      I will read up on this labelFunction - I have not come across this before.
                      • 8. Re: Populate datagrid problem
                        ntsiii Level 3
                        You use labelFunction if your xml item nodes are complex, and the data you want to display in nested nodes. dataField can only see data in the attributes of the item node, or in the text node of the first level children of the item node.

                        If the item nodes are more complex, then a lableFunction() can be used to programatically access any data in the item, and return a string to use as the label in the column.

                        I have a couple examples on cflex.net.

                        Tracy