2 Replies Latest reply on Nov 4, 2007 8:51 AM by ntsiii

    Importing a text file

    pc_acke Level 1
      Hi,

      I'm trying to show different text documents in Flex.
      I have three elements in the app.
      - A tree with a number of nodes, where som children represent text documents.
      - A title window.
      - A text area.

      What I wish to do is the following:
      When I select a child node representing a document, the same document should be loaded into the text area.
      (Using a text area might not be the right thing to do here)

      The documents are, for now at least, placed in the assets folder in the app.

      Regards
      Acke
        • 1. Re: Importing a text file
          atta707 Level 2
          I'm hoping you know which document to select after user has selected a tree node.

          After that you'll use HTTPService to get the contents of the text file from server to the client and just put show the contents in the text area.

          in flex builder help search for: Using an XML file as a data provider

          You'll be doing something like:


          <?xml version="1.0"?>
          <!-- charts/HTTPServiceDataProvider.mxml -->
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
          width="100%" height="100%" creationComplete="srv.send()">
          <mx:HTTPService id="srv" url="../assets/data.xml"/>

          <mx:Panel title="Line Chart">
          <mx:LineChart id="chart"
          dataProvider="{srv.lastResult.data.result}"
          >
          <mx:horizontalAxis>
          <mx:CategoryAxis categoryField="month"/>
          </mx:horizontalAxis>
          <mx:series>
          <mx:LineSeries yField="apple" name="Apple"/>
          <mx:LineSeries yField="orange" name="Orange"/>
          <mx:LineSeries yField="banana" name="Banana"/>
          </mx:series>
          </mx:LineChart>
          </mx:Panel>
          </mx:Application>
          • 2. Re: Importing a text file
            ntsiii Level 3
            I want to weigh in here advising against binding directly to lastResult. It is just too hard to debug, and gives you no opportunity to massage your data or know exactly when it arrives. Below are some snippets showing the approach I suggest. It uses a dataGrid, but a similar process would be used to populate a tree. You might use XML instead of XMLListCollection.
            Tracy

            Sample code using HTTPService, e4x, handler function to populate a list item.
            Also shows usage of AsyncToken.

            The DataGrid tag:
            <mx:DataGrid id="dg" dataProvider="{_xlcMyListData}" .../>


            The HTTPService tag:
            <mx:HTTPService id="service" resultFormat="e4x" result="onResult(event)" fault="..../>

            Script block declaration:
            import mx.rpc.Events.ResultEvent;
            [Bindable]private var _xlcMyListData:XMLListCollection;

            Invoke send:
            var oRequest:Object = new Object();
            oRequest.Arg1 = "value1";
            var callToken:AsyncToken = service.send(oRequest);
            token.callId = "myQuery1";

            Result Handler function:
            private function onResult(oEvent:ResultEvent):void {
            var xmlResult:XML = XML(event.result); //converts result Object to XML. can also use "as" operator
            var xlMyListData:XMLList = xmlResult.myListData; //depends on xml format, is row data
            _xlcMyListData = new XMLListCollection(xlMyListData); //wrap the XMLList in a collection
            trace(_xlcMyListData.toXMLString()); //so you can see exactly how to specify dataField or build labelFunction
            var callToken:AsyncToken = oEvent.token;
            var sCallId = callToken.callId; //"myQuery1"
            switch(sCallId) { //Process the result conditionally
            case "myQuery1":
            doQuery2(); //do whatever. this example calls another data service query
            break;
            ...
            }
            }//onResult