14 Replies Latest reply on Aug 10, 2009 7:52 PM by frogman.pep

    Trouble passing Value from MenuEvent

    frogman.pep Level 1

      In the code that follows I can't figurre out how to get a value for e.label for the lone Menu elements, i.e. "menuItems.push({label:"Model-A"});". Thanks in advance for any help.

       

      private function init():void{
           var menuItems:Array = new Array();
                          
           var subMenuItems:ArrayCollection = new ArrayCollection([{label:"Interior", children:[{label:"Structure"}, {label:"Rods & Reels"},{label:"Literature"}, {label:"Accessories"}]}, {label:"Exterior"}]);
           menuItems.push({label:"Trailer", children:subMenuItems});               
           menuItems.push({label:"Model-A"});
                          
           subMenuItems = new ArrayCollection([{label:"Trailer", children:[{label:"Interior"}, {label:"Exterior "}]}, {label:"Model-A"}]);
           menuItems.push({label:"Restoration", children:subMenuItems});
                          
           menuItems.push({label:"Combo"});
                          
           menuData = new ArrayCollection(menuItems);
      
      private function menuClick(e:MenuEvent):void{
           var picItems:Array = new Array();
           
           for (var i:int = picData.length - 1; i >=0; i--){
               if(e.label == picData[i].category){     
           picItems.push(picData[i])
           }
           tileData = new ArrayCollection(picItems);          
         }
      }               
      }
      
        • 1. Re: Trouble passing Value from MenuEvent
          Gregory Lafrance Level 6

          What problem are you having?

           

          You probably need to use e.target.label or e.currentTarget.label, and you might need to cast it:

           

          Menu(e.target).label or Menu(e.currentTarget).label

           

          OR

           

          MenuItem(e.target).label or MenuItem(e.currentTarget).label

           

          If this post answers your question or helps, please mark it as such.

          1 person found this helpful
          • 2. Re: Trouble passing Value from MenuEvent
            frogman.pep Level 1

            The problem I'm having is both "menuItems.push({label:"Model-A"});" & menuItems.push({label:"Combo"});" are not returning a value for e.label when it's run in the if statement of the menuClick method.  All the other menu items return proper values.  I tried your suggestions and recieved the error, "Property label not found on mx.controls.MenuBar.  And when I try to pass it as MenuItem(e.target).label, "1180: Call to a possibly undefined method MenuItem." is returned.  I did try inputing menuItems(e.target).label, yet the same error is returned.

             

            Thanks for your time Greg.

            • 3. Re: Trouble passing Value from MenuEvent
              Kenny Yates

              frogman,

               

              You have the right idea just the wrong location to grab your values.

              Use e.label to get the value from your ItemClick event.

               

              Here is an example application that I created to show you how to use the values.

              Load this into a test application and play with it, you will get the idea

               

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
              <mx:Script>
                  <![CDATA[
                      import mx.controls.Alert;
                      import mx.collections.ArrayCollection;
                      import mx.events.MenuEvent;
                     
                      [Bindable]
                      public var menuData:ArrayCollection;
                 
                      private function init():void
                      {
                       var menuItems:Array = new Array();
                              
                       var subMenuItems:ArrayCollection = new ArrayCollection([{label:"Interior", children:[{label:"Structure"},
                       {label:"Rods & Reels"},{label:"Literature"}, {label:"Accessories"}]}, {label:"Exterior"}]);
                       menuItems.push({label:"Trailer", children:subMenuItems});  
                       menuItems.push({label:"Model-A"});
                                 
                       subMenuItems = new ArrayCollection([{label:"Trailer", children:[{label:"Interior"},
                       {label:"Exterior "}]}, {label:"Model-A"}]);
                       menuItems.push({label:"Restoration", children:subMenuItems});
                       menuItems.push({label:"Combo"});
                      
                       menuData = new ArrayCollection(menuItems);
                   }
                 
                  private function menuClick(e:MenuEvent):void
                  {
                      Alert.show("what: " + e.label);
                  }

               

                  ]]>
              </mx:Script>
              <mx:MenuBar id="mbTest" dataProvider="{menuData}" itemClick="{menuClick(event)}"/>
              </mx:Application>

               

              HTH,

              Kenny Yates

              • 4. Re: Trouble passing Value from MenuEvent
                frogman.pep Level 1

                Ok, I can see where the confusion might lie, when I posted the original code I didn't paste the entire code and/or explain the entire situation.  Was thinking it was more of a syntax mistake and if I kept the code brief it'd be easier, plus it was before I went to sleep last night, so maybe I was being lazy as well.  Below is the entire code with the purpose of taking whatever value is clicked in the menubar and loading small thumbnail pics into a TileList that corespond to similar values in the category node of an .xml file.  Hope this clairifes.  Thanks for all your help.

                 

                ?xml version="1.0" encoding="utf-8"?>
                <mx:Canvas  xmlns:mx="http://www.adobe.com/2006/mxml"
                   xmlns:comp="components.*"
                   creationComplete="init()">
                  <mx:Script>
                        <![CDATA[
                         import mx.events.MenuEvent;
                         import mx.events.ItemClickEvent;

                            import mx.collections.ArrayCollection;
                  
                   [Bindable]
                   private var menuData:ArrayCollection;
                  
                   [Bindable]
                   public var picItems:Array;
                  
                   [Bindable]
                   public var tileData:ArrayCollection;
                  
                   [Bindable]
                   public var picData:ArrayCollection;
                  
                   [Bindable]
                   public var selectedPic:Object;
                  
                   private function init():void
                   {
                    var menuItems:Array = new Array();
                   
                    var subMenuItems:ArrayCollection = new ArrayCollection([{label:"Interior", children:[{label:"Structure"}, {label:"Rods & Reels"},
                    {label:"Literature"}, {label:"Accessories"}]}, {label:"Exterior"}]);
                    menuItems.push({label:"Trailer", children:subMenuItems});
                  
                    menuItems.push({label:"Model-A"});
                   
                    subMenuItems = new ArrayCollection([{label:"Trailer", children:[{label:"Interior"}, {label:"Exterior "}]}, {label:"Model-A"}]);
                    menuItems.push({label:"Restoration", children:subMenuItems});
                   
                    menuItems.push({label:"Combo"});
                   
                    menuData = new ArrayCollection(menuItems);
                    trace(picData.length);
                   
                   }  
                  
                   private function menuClick(e:MenuEvent):void{
                    var picItems:Array = new Array();
                    //Run a filter on picData here...
                    for (var i:int = picData.length - 1; i >=0; i--){
                     if(e.label == picData[i].category){     
                      picItems.push(picData[i])
                      trace (menuData);                    
                     }
                     tileData = new ArrayCollection(picItems);    
                    }
                   }
                  
                   private function displayPicDetails(event:Event):void{
                    currentState='picDetails';
                    selectedPic = new Object();
                    selectedPic = event.currentTarget.selectedItem;
                    picState.load('assets/pics/' + selectedPic.filename + '.jpg');
                   }
                  ]]>
                </mx:Script>

                <mx:states>
                  <mx:State name="picDetails">
                   <mx:AddChild>
                    <mx:Image id="picState"
                       y="-5" x="2"/>
                   </mx:AddChild>
                  </mx:State>
                </mx:states>

                <mx:MenuBar  id="menuList"
                     labelField="label"
                     y="370"
                     itemClick="{menuClick(event)}"
                           dataProvider="{menuData}"
                           fontSize="16"/>

                <mx:TileList id="picThumbnails"
                     width="635"
                     height="215"
                     y="390"
                     columnWidth="150"
                     rowHeight="100"
                     dataProvider="{tileData}"
                     change="displayPicDetails(event)"
                     itemRenderer="components.PicsDisplay"/>
                </mx:Canvas>

                • 5. Re: Trouble passing Value from MenuEvent
                  Kenny Yates Level 2

                  Frogman,

                  It did seem too obvious of a solution.

                  Now that i have the entire picture i will take a look at what you have 

                  and will figure out a solution.

                  It may be a bit but i will get back with you today.

                   

                  Kenny

                  • 6. Re: Trouble passing Value from MenuEvent
                    Kenny Yates Level 2

                    frogman,

                     

                    Ok, here we go, now I have a better idea of what you are working on.

                     

                    First off a couple of items come to mind, you are using XML as the Data source for the pictures (I am guessing URLs to them).

                    So first thing I would do is to use the XML as the data provider for the TileList.

                     

                    Secondly if you ARE going to use an Array Collection for your Data Provider for the TileList, then here is a more efficient way of looping through the data.

                     

                    MENU CLICK FUNCTION:

                    private function menuClick(e:MenuEvent):void
                        {
                            var picItems:Array = new Array();


                           //for some reason it helps to set this local var here makes comparison much more efficient
                            var selectedItem:String = e.label;
                           
                            //Run a filter on picData here...
                            for each(var item:Object in picData)//since all items inside the Array Collection are of type Object
                            {
                                if(selectedItem == item.category)
                                {    
                                  picItems.push(item)//this would load up your picItems Array only with items that match the criteria
                                  trace (menuData);                   
                                }

                            }

                           //then load your tileData AC after the "for" loop
                            tileData = new ArrayCollection(picItems);
                        }

                     

                    Now if you are going to just use the XML as the Data Provider for your list of possible images from your service (which I would recommend since you are not editing the XML I believe), you would use something a bit different.

                     

                    And based on the XML schema I could not really give you a good example of that process right now since it would depend on the XML Schema structure.

                     

                    HTH,

                    Kenny Yates

                    1 person found this helpful
                    • 7. Re: Trouble passing Value from MenuEvent
                      frogman.pep Level 1

                      This is helpful and I'd give you 5 points but I can't log in now (sending

                      through email, something about site doing temporary maintenance), but this

                      still doesn't solve the issue of recieving values for the menu items Model-A

                      & Combo, ones without any children.  If it'd be beneficial to zip the

                      project and you are willing for me to email it to you, I'll fire away.

                      • 8. Re: Trouble passing Value from MenuEvent
                        Kenny Yates Level 2

                        Yeah noticed they are down.

                        Lets hold off on sending your project as i am responding and getting 

                        mail by IPhone let me look at what i have and see what i can figure out.

                         

                        Kenny

                        • 9. Re: Trouble passing Value from MenuEvent
                          Kenny Yates Level 2

                          Ok, i have you a rather cludgy work around and it's a bit more static 

                          than i like.

                          I can send it to you in a couple hours when i get back to my laptop.

                          Too involved to explain on my IPhone mail.

                          Will that work for you?

                           

                          Kenny

                          • 10. Re: Trouble passing Value from MenuEvent
                            frogman.pep Level 1

                            Yeah mang, won't turn down any offer to help, we'll see if it'll do.  Am

                            working on it with another buddy and will post any solution if we come

                            across anything.  Thx.

                            • 11. Re: Trouble passing Value from MenuEvent
                              Kenny Yates Level 2

                              Gotcha.

                              Basic concept is to have both a itemClick event AND a click event for 

                              the MenuBar comp.

                              First one is of type menu event so can grab event.label value.

                              Second one is mouse event, you would have to grab the 

                              menubar.selectedIndex check its value see if it's 1 or 3 index(Model-A 

                              or Combo) if so declare new MenuBarItem = menubar.getItemAt(INDEX).

                              Then you can use the MenuBarItem.data.label.

                              And there u go.

                               

                              Kenny

                              • 12. Re: Trouble passing Value from MenuEvent
                                frogman.pep Level 1

                                The following is the completed code that gets the job done.  My buddy said

                                he looked into selectedIndex but it always came back as -1.  Let me know if

                                you think of anything else, but at the least this is working with no forseen

                                errors/problems for now.  Thanks a bunch for your help, I'm rather excited

                                to have this done.  I'll make sure to get you some points when the forums

                                are back on.

                                 

                                 

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

                                 

                                <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml"

                                 

                                xmlns:comp="components.*"

                                 

                                creationComplete="init()">

                                 

                                <mx:Script>

                                 

                                <![CDATA[

                                 

                                import mx.events.MenuEvent;

                                 

                                import mx.events.ItemClickEvent;

                                 

                                import mx.collections.ArrayCollection;

                                 

                                 

                                private var menuData:ArrayCollection;

                                 

                                 

                                public var tileData:ArrayCollection;

                                 

                                 

                                public var picData:ArrayCollection;

                                 

                                 

                                public var selectedPic:Object;

                                 

                                private function init():*void

                                *

                                 

                                {

                                 

                                var menuItems:Array = new Array();

                                 

                                var subMenuItems:ArrayCollection = new ArrayCollection([{label:*

                                "Interior", children:[{label:"Structure"*}, ,

                                 

                                , ]}, ]);

                                 

                                menuItems.push();

                                 

                                menuItems.push();

                                 

                                subMenuItems = new ArrayCollection([, ]}, ]);

                                 

                                menuItems.push();

                                 

                                menuItems.push();

                                 

                                menuData = new ArrayCollection(menuItems);

                                 

                                }

                                 

                                private function menuClick(e:MenuEvent):void{

                                 

                                var picItems:Array = new Array();

                                 

                                var selectedItem:String = e.label;

                                 

                                for each(var item:Object in picData){

                                 

                                if(selectedItem == item.category){

                                 

                                picItems.push(item)

                                 

                                }

                                 

                                tileData = new ArrayCollection(picItems);

                                 

                                }

                                 

                                }

                                 

                                private function displayPicDetails(event:Event):void{

                                 

                                currentState='picDetails';

                                 

                                selectedPic = new Object();

                                 

                                selectedPic = event.currentTarget.selectedItem;

                                 

                                picState.load('assets/pics/' + selectedPic.filename + '.jpg');

                                 

                                }

                                 

                                private function topMenuClick(e:MouseEvent):void{

                                 

                                var MenuItems:Array = new Array();

                                 

                                var selectedItem:String = e.target.data.label;

                                 

                                for each(var item:Object in picData){

                                 

                                if(selectedItem == item.category){

                                 

                                MenuItems.push(item)

                                 

                                }

                                 

                                tileData = new ArrayCollection(MenuItems);

                                 

                                }

                                 

                                }

                                 

                                ]]>

                                 

                                </mx:Script>

                                 

                                <mx:states>

                                 

                                <mx:State name="picDetails">

                                 

                                <mx:AddChild>

                                 

                                <mx:Image id="picState"

                                 

                                y="-5" x="2"/>

                                 

                                </mx:AddChild>

                                 

                                </mx:State>

                                 

                                </mx:states>

                                 

                                <mx:MenuBar id="menuList"

                                 

                                labelField="label"

                                 

                                y="370"

                                 

                                click="{topMenuClick(event)}"

                                 

                                itemClick="{menuClick(event)}"

                                 

                                dataProvider=""

                                 

                                fontSize="16"/>

                                 

                                <mx:TileList id="picThumbnails"

                                 

                                width="635"

                                 

                                height="215"

                                 

                                y="390"

                                 

                                columnWidth="150"

                                 

                                rowHeight="100"

                                 

                                dataProvider=""

                                 

                                change="displayPicDetails(event)"

                                 

                                itemRenderer="components.PicsDisplay"/>

                                 

                                </mx:Canvas

                                • 13. Re: Trouble passing Value from MenuEvent
                                  Kenny Yates Level 2

                                  frogman,

                                   

                                  Sorry it took so long to respond.

                                   

                                  Here is the work around - still cannot log into Adobe Forms.

                                   

                                  HTH,

                                  Kenny Yates

                                   

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

                                   

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

                                  layout="absolute" creationComplete="init()">

                                   

                                  <mx:Script>

                                   

                                  <![CDATA[

                                   

                                  import mx.controls.menuClasses.MenuBarItem;

                                   

                                  import mx.controls.Alert;

                                   

                                  import mx.collections.ArrayCollection;

                                   

                                  import mx.events.MenuEvent;

                                   

                                             

                                   

                                  public var menuData:ArrayCollection;

                                   

                                  private function init():void

                                   

                                              {

                                   

                                  var menuItems:Array = new Array();

                                   

                                  var subMenuItems:ArrayCollection = new

                                  ArrayCollection([{label:"Interior", children:[{label:"Structure"},

                                   

                                             ,,

                                  ]}, ]);

                                   

                                             menuItems.push();

                                   

                                             menuItems.push();

                                   

                                             subMenuItems = new ArrayCollection([,

                                   

                                             ]}, ]);

                                   

                                             menuItems.push();

                                   

                                             menuItems.push();

                                   

                                             menuData = new ArrayCollection(menuItems);

                                   

                                         }

                                   

                                  private function menuClick(e:MenuEvent):void

                                   

                                        {

                                   

                                  var picItems:Array = new Array();

                                   

                                              Alert.show("what: " + e.label);

                                   

                                        }

                                   

                                  private function barClick(m:MouseEvent):void

                                   

                                        {

                                   

                                  var selIndex:String = String(mbTest.selectedIndex);

                                   

                                  if(selIndex == "1" || selIndex == "3")

                                   

                                              {

                                   

                                  var mItem:MenuBarItem = mbTest.getChildAt(Number(selIndex)+1) as

                                  MenuBarItem

                                   

                                                   Alert.show("what: " + mItem.data.label);

                                   

                                              }

                                   

                                        }

                                   

                                        ]]>

                                   

                                  </mx:Script>

                                   

                                  <mx:MenuBar id="mbTest" dataProvider=""

                                  click="{barClick(event)}"  itemClick="{menuClick(event)}"

                                  labelField="label"/>

                                   

                                  </mx:Application

                                  • 14. Re: Trouble passing Value from MenuEvent
                                    frogman.pep Level 1

                                    Thx for the follow up, I'll check it out and test it later, am fed up with code for now.  Now that I've got something working I need to get some other work done.  Will get you all some points tomorrow.  Thanks for all the help!