3 Replies Latest reply on May 29, 2008 9:58 AM by Newsgroup_User

    Problem with datagrid

    Level 7
      Hi,
      I'm newbie in Flex, and I would like to as for help with passing results
      from httpservice result to data grid.
      I have the flex code:
      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute"
      creationComplete="myHTTPService()">
      <mx:Script source="../scripts/main.as" />
      <mx:DataGrid left="0" right="0" top="0" bottom="0" dataProvider="{dg}">
      <mx:columns>
      <mx:DataGridColumn headerText="Test ID" dataField="testid"/>
      <mx:DataGridColumn headerText="Name" dataField="name"/>
      <mx:DataGridColumn headerText="Email" dataField="email"/>
      </mx:columns>
      </mx:DataGrid>
      </mx:Application>

      and main.as file:

      / ActionScript file
      import mx.collections.ArrayCollection;
      import mx.controls.Alert;
      import mx.rpc.events.FaultEvent;
      import mx.rpc.events.ResultEvent;
      import mx.rpc.http.mxml.HTTPService;


      [Bindable]
      public var dg:ArrayCollection = new ArrayCollection();


      public function myHTTPService():void {
      var service:HTTPService = new HTTPService();
      service.url = " http://localhost/Test/test.php";
      service.addEventListener(ResultEvent.RESULT,successResult);
      service.addEventListener(FaultEvent.FAULT,faultResult);
      service.send();
      }


      public function successResult(event:ResultEvent):void {

      //dg = (event.result);

      Alert.show("Success!");
      }


      public function faultResult(event:FaultEvent):void {
      Alert.show("Fault :-( ");
      }



      I have the problem with the commented line: dg = event.result

      Could anyone help mi? How to pass data from even.result to
      arraycollection???



      Best regards

      Mariusz


        • 1. Re: Problem with datagrid
          Level 7
          In addition:
          my php return this:
          ...
          print "<people>";
          while( $Row = mysql_fetch_object( $Result ) )
          {
          print
          "<person><testid>".$Row->testid."</testid><name>".$Row->name."</name><email>".$Row->email ."</email></person>";
          }
          print "</people>";

          And I check, that exacly this is returned in evenResult, but I still cant
          convert this into ArrayCollection :-(((






          • 2. Re: Problem with datagrid
            ntsiii Level 3
            The quickest solution is to:
            1) Change the model variable to:
            public var _xlcDg:XMLListCollection
            1) set resultFormat="e4x" on the HTTPService instance
            2) in the handler, do:
            ...
            var xmlResult:XML = XML(event.result);
            trace(xmlResult.toXMLString()); //to see what you have
            var xlPersons:XMLList = xmlResult.person; //you can use the XMLlist directly, but it is not best practice
            trace(xlPersons.length); //what you expect?
            _xlcDg = new XMLListCollection(xlPersons);

            If you want an ArrayCollection, create a class to be your value object, then loop through the XMLList and create a new instance of the VO, assign the propderty values form the xml node, then addItem() to put it in the array collection.

            Tracy
            • 3. Re: Problem with datagrid
              Level 7
              Many thanks!!!
              Of course your way with XMLListCollection works!
              Now I will try to create own class, to use ArrayCollection.

              Thank you Tracy.

              > If you want an ArrayCollection, create a class to be your value object,
              > then
              > loop through the XMLList and create a new instance of the VO, assign the
              > propderty values form the xml node, then addItem() to put it in the array
              > collection.
              >
              > Tracy
              >