1 Reply Latest reply on Oct 6, 2009 5:44 PM by CaliCanuck

    Tree Sending Data to JavaScript - Help!

    CaliCanuck

      I am trying to call a function in JavaScript and apss a parameter to it. The code I have below is not working. The label I have in HTML always says undefined after I call the ExternalInterface function in my mxml function. Can someone please take a look and tell me what I am doing wrong?

       

      mxml code:

       

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

      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"

         width="404" height="356" preinitialize="init()">

        <mx:Script>

          <![CDATA[

         

            [Bindable]

            public var treeData:Array = new Array();

         

            public function init():void{

         

              var tmpObjectGrains:Object = new Object();

          var tmpObjectDairy:Object = new Object();

          var tmpObjectFruits:Object = new Object();

          var tmpObjectMeats:Object = new Object();

          var tmpObjectNuts:Object = new Object();

         

          tmpObjectGrains['label']="BOC";

          tmpObjectGrains['children']=[{label:'Line 1'},

                                           {label:'Line 2'},

                                           {label:'Line 3'},

                                           {label:'Line 4'}];

          treeData.push(tmpObjectGrains);

         

          tmpObjectDairy['label']="TOC";

              tmpObjectDairy['children']=[{label:'Line 10'},

                                          {label:'Line 20'},

                                          {label:'Line 30'}];

          treeData.push(tmpObjectDairy);

         

          tmpObjectFruits['label']="SLOC";

      tmpObjectFruits['children']=[{label:'Line X'},

                                           {label:'Line Y'},

                                           {label:'Line Z'}];

              treeData.push(tmpObjectFruits);   

           }

       

           public function jsPassData(event:Event):void

           {

           if (ExternalInterface.available)

             {     

               ExternalInterface.call("PassData", treeView1.selectedItem);

               lblMessage.text = "Data sent!";

             }

             else

             {

               lblMessage.text = "Error sending data!"

             }

           }

         ]]>

      </mx:Script>

        <mx:Panel id="pnlMain" x="0" y="0" width="404" height="356"

           layout="absolute" title="JavaScript Test">

           <mx:Tree id="treeView1" x="0" y="0" dataProvider="{treeData}" labelField="label" change="jsPassData(event);" width="268" height="219"></mx:Tree>

          <mx:Label x="276" y="22" id="lblMessage"/>

        </mx:Panel>

      </mx:Application>

       

       

       

       

      JavaScript function:

       

       

      <script language="JavaScript" type="text/javascript">

       

      function PassData(data)

      {

        if(person == null)

        {

          alert("Select a line");

        }

        else

        {

          document.getElementById('TreeView').innerHTML = data.label;

        }

      }

       

       

      </script>