3 Replies Latest reply on Apr 16, 2009 12:28 PM by m_hartnett

    Fireing click event programmatically

    iamfuric Level 2
      Hi
      I have tree object built from xml :

       

      treeXML:XML = <node label="label1" id="1"/>
      <node label="label1" id="2"/>
      <node label="label1" id="3"/>
      <node label="label1" id="4"/>

       

      <tree dataProvider={treeXML} labelField="@label"/>

       

      The question is :
      How can i fire (programmatically) click event on DisplayObject (item in tree) knowing it's "id" value.
      Thanks.
        • 1. Re: Fireing click event programmatically
          m_hartnett Level 3

          See if this helps.  Same concept except using a button click event.

           

           

           

           

           

           

           

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

          <mx:Application

           

           

          xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init()" xmlns:local="*"

          >

           

           

          <mx:Script>

          <![CDATA[

           

           

           

          private function changeText():void

          {

          txt.text =

           

          "You mouseOver the text and changed the text without pressing the button"

          }

          ]]>

           

           

          </mx:Script>

           

           

           

           

          <mx:Text id="txt" text="MouseOver this text but dont press the button." width="376" mouseOver="btn.dispatchEvent(new MouseEvent('click'))"

          />

           

           

          <mx:Button id="btn" label="Button" click="changeText()"

          />

          • 2. Re: Fireing click event programmatically
            iamfuric Level 2

            Thanks for the suggest, but what i need to do is to get reference to DisplayObject ( that  id represents) and after that call dispObj.dispatchEvent(click).

            So, the MAIN issue here is to get reference to tree item....

            • 3. Re: Fireing click event programmatically
              m_hartnett Level 3

              The object that the item in the tree is really a reference to the node of the XML data that is being used to create the tree.

              To get a reference to that you would need to search the XML list using e4x methods.

               

              Once you search the xml and get the node then you can execute some method.  You will not have a display object to dispatch an event unless you are using the parent window or the tree itself.

               

              This little app may help you.  It shows the search for the xml node.

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

               

               

               

               

               

               

               

               

               

               

               

               

               

               

               

               

               

               

               

               

               

               

               

               

               

               

               

              <mx:Application

               

               

              xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init()" xmlns:local="*"

              >

               

               

              <mx:Script>

              <![CDATA[

               

               

               

               

              private function getNodeData():void

              {

               

               

              var

              node:XML = XML(tree.selectedItem);

               

               

              if( node == null ) return

              ;

               

               

              var nodeId:String = node.attribute("eid"

              ).toString();

               

               

              var nodeLabel:String = node.attribute("label"

              ).toString();

              treeSelectedData.text =

               

              "Id: " + nodeId + " label: "

              + nodeLabel

               

              }

               

               

               

              private function findNodeById():void

              {

               

               

              var

              xmllistDescendants:XMLList = treeData.(@eid == nodeStepper.value);

               

              tree.selectedItem = xmllistDescendants[0];

               

               

              var

              node1:XML = XML(tree.selectedItem);

               

               

              if( node1 == null ) return

              ;

               

               

              var nodeId1:String = node1.attribute("eid"

              ).toString();

               

               

              var nodeLabel1:String = node1.attribute("label"

              ).toString();

              findNodeData.text =

               

              "Id: " + nodeId1 + " label: "

              + nodeLabel1

              }

               

              ]]>

               

               

              </mx:Script>

               

               

               

              <mx:XMLList id="treeData"

              >

               

               

              <node label="label1" eid="1"

              />

               

               

              <node label="label2" eid="2"

              />

               

               

              <node label="label3" eid="3"

              />

               

               

              <node label="label4" eid="4"

              />

               

               

              </mx:XMLList>

               

               

               

              <mx:Tree id="tree" dataProvider="{treeData}" labelField="@label" width="293"></mx:Tree>

               

               

              <mx:Button id="btn" label="Get Selected Data" click="getNodeData()"

              />

               

               

              <mx:Text text="Text" width="180" id="treeSelectedData"

              />

               

               

              <mx:Button label="Find in tree" click="findNodeById()"

              />

               

               

              <mx:NumericStepper id="nodeStepper" value="1" minimum="1" maximum="4" stepSize="1"

              />

               

               

              <mx:Text text="Text" width="180" id="findNodeData"

              />

              </mx:Application>

              1 person found this helpful