10 Replies Latest reply on Apr 1, 2008 4:28 PM by jfizer

    Setting MenuItem toggled property in actionscript.

    jfizer Level 1
      So the common method for setting up a menubar is to have the menu items defined in a block of XML. However, in so doing I cant find a method for changing the properties of a menu item at run time in actionscript. Anyone out there have a solution or can point me in the right direction?
        • 1. Re: Setting MenuItem toggled property in actionscript.
          ntsiii Level 3
          Get a reference to the node you want using an e4x expression, eg:

          var xmlMenuItem:XML = xmlMenu..menuitem.(@label=="whatever")[0];
          then set whatever attribute value you want:
          xmlMenuItem.@someAttribute = somevalue;

          Tracy
          • 2. Re: Setting MenuItem toggled property in actionscript.
            jfizer Level 1
            I've been trying this to no avail. I keep getting "@label is not defined".

            var xmlMenuItem:XML = xmlMenu..menuitem.(@label=="Chart")[0]

            <mx:XMLList id="xmlMenu">
            <menuitem label="File">
            <menuitem label="Session" data="export" enabled="true"/>
            </menuitem>
            <menuitem label="Chart">
            <menuitem label="Type" >
            <menuitem label="Area" id="areaChart" type="radio" groupName="chartType" data="Chart" toggled="false" enabled="true"/>
            <menuitem label="Bar" id="barChart" type="radio" groupName="chartType" data="Chart" toggled="false" enabled="true"/>
            <menuitem label="Column" id="colChart" type="radio" groupName="chartType" data="Chart" toggled="true" enabled="true"/>
            <menuitem label="Line" id="lineChart" type="radio" groupName="chartType" data="Chart" toggled="false" enabled="true"/>
            <menuitem label="Pie" id="pieChart" type="radio" groupName="chartType" data="Chart" toggled="false" enabled="true"/>
            </menuitem>
            </mx:XMLList>
            • 3. Re: Setting MenuItem toggled property in actionscript.
              ntsiii Level 3
              That is because the expression is examining all the nodes, and raises that exception on nodes that do not have the "label" attribute.

              To avoid this, whenever you are not CERTAIN that all nodes will have an attribute (including the root) use the attribute() method:

              var xmlMenuItem:XML = xmlMenu..menuitem.(attribute("label")=="Chart")[0]

              I pretty much use this form allways.

              Tracy
              • 4. Re: Setting MenuItem toggled property in actionscript.
                jfizer Level 1
                While using the attribute parameter stops the "@label is not defined" error, it returns null to the XML object.
                • 5. Re: Setting MenuItem toggled property in actionscript.
                  ntsiii Level 3
                  Ok, one odd thing I see is that you are using an XMLList.

                  Change that to XML, add a root node, like <menu> and try again.

                  Tracy
                  • 6. Re: Setting MenuItem toggled property in actionscript.
                    jfizer Level 1
                    quote:

                    Originally posted by: ntsiii
                    Ok, one odd thing I see is that you are using an XMLList.

                    Change that to XML, add a root node, like <menu> and try again.

                    Tracy


                    Cant use XML as the DataProvider for a MenuBar as far as I know. When I change the type to XML and add a root node the entire XML string shows up as a single menu with no children.
                    • 7. Re: Setting MenuItem toggled property in actionscript.
                      ntsiii Level 3
                      Yeah, pass xmlMenu.menuitem as the dataProvider.
                      Tracy
                      • 8. Re: Setting MenuItem toggled property in actionscript.
                        ntsiii Level 3
                        Have you ever had success using e4x expressions against XMLList?

                        This may be possible, and the reason I don't was because I was doing something wrong when I tried.

                        I do know that what you are attempting is doable, I do it on nearly all of my projects, including the current one.

                        • 9. Re: Setting MenuItem toggled property in actionscript.
                          jfizer Level 1
                          quote:

                          Originally posted by: ntsiii
                          Have you ever had success using e4x expressions against XMLList?

                          This may be possible, and the reason I don't was because I was doing something wrong when I tried.

                          I do know that what you are attempting is doable, I do it on nearly all of my projects, including the current one.



                          There are examples of using them in the documentation, however I'm not having much success myself. Seems like a lot of hoops to jump through for something as simple as toggling a radio button menu item.
                          • 10. Re: Setting MenuItem toggled property in actionscript.
                            jfizer Level 1
                            Got something that works, even if it is a bit of a kludge. Using e4x to pull in just the section of the XMLList that pertains to the chart settings, then looping over each item in the list to toggle off the menu items and toggle on the new selection.

                            var item:XML;
                            var chartMenu:XMLList = xmlMenu..menuitem.(attribute("groupName")=="chartType");
                            for each(item in chartMenu) {
                            item.@toggled=false;
                            if(item.@id==importXML.chart.@type) { item.@toggled=true; }
                            }