7 Replies Latest reply on Dec 18, 2007 8:06 AM by MayLam18

    How come my datagrid shows nothing?

    MayLam18 Level 1
      Hi Everyone,

      How come nothing show up in the datagrid? Please help.

      How come LINE XXX shows nothing?

      LINE KKK generates the following xml document:

      <GetDialectsResponse xmlns:xsd=" http://www.w3.org/2001/XMLSchema" xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" xmlns=" http://FaciNet.com/" xmlns:soap=" http://schemas.xmlsoap.org/soap/envelope/">
      <GetDialectsResult>
      <Dialect>
      <ValueID>1</ValueID>
      <Description>English (Canada)</Description>
      </Dialect>
      <Dialect>
      <ValueID>2</ValueID>
      <Description>Spanish (Mexico)</Description>
      </Dialect>
      <Dialect>
      <ValueID>3</ValueID>
      <Description>Spanish (Honduras)</Description>
      </Dialect>
      <Dialect>
      <ValueID>4</ValueID>
      <Description>Portuguese (Brazil)</Description>
      </Dialect>
      <Dialect>
      <ValueID>5</ValueID>
      <Description>Turkish (Turkey)</Description>
      </Dialect>
      </GetDialectsResult>
      </GetDialectsResponse>

      The flex 2 code is shown below:

      <?xml version="1.0" encoding="utf-8"?>

      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="WS.GetDialects.send()">

      <mx:Script>
      <![CDATA[
      import mx.rpc.events.ResultEvent;
      import mx.collections.XMLListCollection;

      [Bindable]private var _xlcMyListData:XMLListCollection;

      [Bindable]private var _xmlResult:XML;
      [Bindable]private var _myXmlList:XMLList;


      private function resultHandler(event:ResultEvent):void {

      _xmlResult = XML(event.result); //converts result Object to XML. can also use "as" operator
      trace(_xmlResult.toXMLString()); // LINE KKK


      var xlMyListData:XMLList = _xmlResult.Dialect;
      _xlcMyListData = new XMLListCollection(xlMyListData);
      trace(_xlcMyListData.toXMLString()); // LINE XXX

      }
      ]]>
      </mx:Script>


      <mx:WebService id="WS" wsdl=" http://localhost:49320/CRM-WS-2/Service.asmx?wsdl">
      <mx:operation name="GetDialects" resultFormat="e4x" result="resultHandler(event)" />
      </mx:WebService>

      <mx:Button id="test0" x="225" y="10" label="Button" click=" WS.GetDialects.send()"/>

      <mx:DataGrid dataProvider="{_xlcMyListData}">
      <mx:columns>
      <mx:DataGridColumn dataField="@ValueID" headerText="Value ID" />
      <mx:DataGridColumn dataField="@Description" headerText="Description" />
      </mx:columns>
      </mx:DataGrid>

      </mx:Application>
        • 1. Re: How come my datagrid shows nothing?
          peterent Level 2
          Your XML appears to be structured like this:
          <GetDialectsResponse ...>
          -------<GetDialectsResult>
          ------------<Dialect>
          -----------------<ValueID>1</ValueID>
          -----------------<Description>English (Canada)</Description>
          -----------</Dialect>

          and you are trying to build an XMLList of <Dialect> nodes using:
          var xlMyListData:XMLList = _xmlResult.Dialect;

          The trouble is Dialect is not a direct child of the root node. If you want a list of the <Dialect> nodes, do this instead:
          var xlMyListData:XMLList = _xmlResult..Dialect;

          Note the 2 dots vs. the 1 dot you are using.

          Finally, your DataGridColumns are set up to @Value, but none of your nodes have attributes: <Dialect Value="something" /> so you don't want to use the @ in front of the name.
          • 2. Re: How come my datagrid shows nothing?
            MayLam18 Level 1
            Hi Peter,

            After I make the changes. My datagrid still shows nothing. Please help. Thanks.

            May

            quote:

            Originally posted by: peterent
            Your XML appears to be structured like this:
            <GetDialectsResponse ...>
            -------<GetDialectsResult>
            ------------<Dialect>
            -----------------<ValueID>1</ValueID>
            -----------------<Description>English (Canada)</Description>
            -----------</Dialect>

            and you are trying to build an XMLList of <Dialect> nodes using:
            var xlMyListData:XMLList = _xmlResult.Dialect;

            The trouble is Dialect is not a direct child of the root node. If you want a list of the <Dialect> nodes, do this instead:
            var xlMyListData:XMLList = _xmlResult..Dialect;

            Note the 2 dots vs. the 1 dot you are using.

            Finally, your DataGridColumns are set up to @Value, but none of your nodes have attributes: <Dialect Value="something" /> so you don't want to use the @ in front of the name.



            • 3. Re: How come my datagrid shows nothing?
              peterent Level 2
              Can you post some of the code you've changed?
              • 4. Re: How come my datagrid shows nothing?
                MayLam18 Level 1
                Hi Peter,

                Below is my code. Thanks.

                <?xml version="1.0" encoding="utf-8"?>

                <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="WS.GetDialects.send()">

                <mx:Script>
                <![CDATA[
                import mx.rpc.events.ResultEvent;
                import mx.collections.XMLListCollection;


                [Bindable]private var _xlcMyListData:XMLListCollection;
                [Bindable]private var _xmlResult:XML;

                private function resultHandler(event:ResultEvent):void {

                _xmlResult = XML(event.result); //converts result Object to XML. can also use "as" operator
                trace(_xmlResult.toXMLString()); // LINE KKK, working because it can display the xml document.


                var xlMyListData:XMLList = _xmlResult..Dialect; //depends on xml format, is row data
                _xlcMyListData = new XMLListCollection(xlMyListData); //wrap the XMLList in a collection
                trace(_xlcMyListData.toXMLString()); // LINE XXX which shows nothing.


                }
                ]]>
                </mx:Script>


                <mx:WebService id="WS" wsdl=" http://localhost:49320/CRM-WS-2/Service.asmx?wsdl">
                <mx:operation name="GetDialects" resultFormat="e4x" result="resultHandler(event)" />
                </mx:WebService>

                <mx:Button id="test0" x="225" y="10" label="Button" click=" WS.GetDialects.send()"/>

                <mx:DataGrid dataProvider="{_xlcMyListData}">
                <mx:columns>
                <mx:DataGridColumn dataField="ValueID" headerText="Value ID" />
                <mx:DataGridColumn dataField="Description" headerText="Description" />
                </mx:columns>
                </mx:DataGrid>

                </mx:Application>


                quote:

                Originally posted by: peterent
                Can you post some of the code you've changed?


                • 5. Re: How come my datagrid shows nothing?
                  peterent Level 2
                  I don't see anything wrong in your code. Try using the debugger (in Flex Builder it is pretty easy) and set a break point in your result handler. Then after you've created the collection, take a look at its contents. You may just be off a bit in your expections of the XML.
                  • 6. Re: How come my datagrid shows nothing?
                    dave cragg Level 2
                    In addition to Peter's suggestions, you'll have to deal with the xml namespace in the xml sample above. This has caused me trouble before. Below is one way to handle it. I'm not sure if it's the best way, so if anyone can add to this, please do.

                    At the top of your script (e.g. below the import statements) add this:

                    namespace FaciNS = " http://FaciNet.com/";
                    use namespace FaciNS;

                    That should get the Line XXX trace to display the XMLList.

                    To get the data to display in the grid, add a labeFunction attribute to the dataGridColumn declarations.

                    <mx:DataGridColumn dataField="ValueID" headerText="Value ID" labelFunction="myFunc" />
                    <mx:DataGridColumn dataField="Description" headerText="Description" labelFunction="myFunc" />

                    Then add the following function to your script:

                    private function myFunc(item:Object, column:DataGridColumn):String {

                    var xml:XML = XML(item);
                    return xml.FaciNS::[column.dataField];
                    }
                    • 7. Re: How come my datagrid shows nothing?
                      MayLam18 Level 1
                      Thanks for all your help Peter and Dave. Dave your suggestion is working. Because now my datagrid can show the data.

                      Thanks again!

                      May

                      quote:

                      Originally posted by: dave cragg
                      In addition to Peter's suggestions, you'll have to deal with the xml namespace in the xml sample above. This has caused me trouble before. Below is one way to handle it. I'm not sure if it's the best way, so if anyone can add to this, please do.

                      At the top of your script (e.g. below the import statements) add this:

                      namespace FaciNS = " http://FaciNet.com/";
                      use namespace FaciNS;

                      That should get the Line XXX trace to display the XMLList.

                      To get the data to display in the grid, add a labeFunction attribute to the dataGridColumn declarations.

                      <mx:DataGridColumn dataField="ValueID" headerText="Value ID" labelFunction="myFunc" />
                      <mx:DataGridColumn dataField="Description" headerText="Description" labelFunction="myFunc" />

                      Then add the following function to your script:

                      private function myFunc(item:Object, column:DataGridColumn):String {

                      var xml:XML = XML(item);
                      return xml.FaciNS::[column.dataField];
                      }