2 Replies Latest reply on May 22, 2008 5:17 AM by Jaguar280

    using string variable to define object

    Jaguar280 Level 1
      Hey guys,

      im having trouble getting this code to work. I would really appreciate any help you can provide as its the only thing holding me back from finishing my project :-). what im doing is creating a map where when you click on a state, it brings up all the locations we have within that state. the locations for each state are within an xml file.

      the file has the following structure. it is just a list of locations within different states. i used the each state's two letter code for the structure.
      quote:


      <venuelist>
      <mi>
      <name></name>
      <address></address>
      <city></city>
      </mi>
      <mi>
      <name></name>
      <address></address>
      <city></city>
      </mi>
      <ma>
      <name></name>
      <address></address>
      <city></city>
      </ma>
      </venuelist>


      here is the result handler. right now it is set so that it always grabs the locations from michigan
      quote:

      public function resultHandler(event:ResultEvent):void {
      var arrvar:Object = event.result.venuelist.mi;
      arrcol = arrvar as ArrayCollection;
      dg.dataProvider = arrcol;
      }


      in my flex, i have a http service calling the xml file and assigning the entire xml collection to a variable. and then having it assigned as the dataprovider for my datagrid. when you click on a state, it creates a pop up with the data grid. i also have an alert that shows the two letter state code you click on
      quote:

      public function createPopUp(event:USAMapEvent):void {
      var stateSelected:String = event.code;
      Alert.show(stateSelected, 'Alert Box', mx.controls.Alert.OK);
      PopUpManager.addPopUp(panel, this, true);
      PopUpManager.centerPopUp(panel);
      }


      what i need to do is set the dataprovider for the datagrid to something like dg.dataProvider = arrcoll + stateSelected so that it will go to "
      "venuelist.stateSelected". what is the proper way to do this. any help is appreciated!
        • 1. using string variable to define object
          VarioPegged Level 2
          Use resultFormat="e4x" in your HTTPService. Declare a property to hold the result...

          private var xml:XML;

          Then your result handler might look something like this...

          private function resultHandler(event:ResultEvent):void {
          xml = event.result as XML;
          ...
          }

          And then you'll assign the datagrid's data provider a value dynamically when a user clicks a state (assuming that event.code will be "mi" or "ma", etc.) ...

          public function createPopUp(event:USAMapEvent):void {
          var stateSelected:String = event.code;
          dg.dataProvider = xml[stateSelected] as XMLList; //DP assigned based on stateSelected value
          Alert.show(stateSelected, 'Alert Box', mx.controls.Alert.OK);
          PopUpManager.addPopUp(panel, this, true);
          PopUpManager.centerPopUp(panel);
          }

          TS
          • 2. Re: using string variable to define object
            Jaguar280 Level 1
            Hey Variopegged!

            thank you so much for the reply! i follow your logic. i made the changes and everything works just like its supposed to :-). thank you once again... ive been messing with this issue for such a long time!