Replace the content of a dropdown Button's Menu on the fly : DropDown Button « YUI Library « JavaScript DHTML






Replace the content of a dropdown Button's Menu on the fly

 

<!--
Software License Agreement (BSD License)
Copyright (c) 2009, Yahoo! Inc.
All rights reserved.

Redistribution and use of this software in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, this list of conditions and the
      following disclaimer.
    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
    * Neither the name of Yahoo! Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission of Yahoo! Inc.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Sources of Intellectual Property Included in the YUI Library

YUI is issued by Yahoo! under the BSD license above. Below is a list of certain publicly available software that is the source of intellectual property in YUI, along with the licensing terms that pertain to thosesources of IP. This list is for informational purposes only and is not intended to represent an exhaustive list of third party contributions to the YUI.

    * Douglas Crockford's JSON parsing and stringifying methods: In the JSON Utility, Douglas Crockford's JSON parsing and stringifying methods are adapted from work published at JSON.org. The adapted work is in the public domain.
    * Robert Penner's animation-easing algorithms: In the Animation Utility, YUI makes use of Robert Penner's algorithms for easing.
    * Geoff Stearns's SWFObject: In the Charts Control and the Uploader versions through 2.7.0, YUI makes use of Geoff Stearns's SWFObject v1.5 for Flash Player detection and embedding. More information on SWFObject can be found here (http://blog.deconcept.com/swfobject/). SWFObject is (c) 2007 Geoff Stearns and is released under the MIT License (http://www.opensource.org/licenses/mit-license.php).
    * Diego Perini's IEContentLoaded technique: The Event Utility employs a technique developed by Diego Perini and licensed under GPL. YUI's use of this technique is included under our BSD license with the author's permission.

-->

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>


    <meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Replacing the content of a Button's Menu</title>

<style type="text/css">
/*margin and padding on body element
  can introduce errors in determining
  element position and are not recommended;
  we turn them off as a foundation for YUI
  CSS treatments. */
body {
  margin:0;
  padding:0;
}
</style>

<link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/fonts/fonts-min.css" />
<link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/menu/assets/skins/sam/menu.css" />
<link rel="stylesheet" type="text/css" href="yui_2.7.0b-lib/button/assets/skins/sam/button.css" />
<script type="text/javascript" src="yui_2.7.0b-lib/yahoo-dom-event/yahoo-dom-event.js"></script>
<script type="text/javascript" src="yui_2.7.0b-lib/container/container_core-min.js"></script>
<script type="text/javascript" src="yui_2.7.0b-lib/menu/menu-min.js"></script>
<script type="text/javascript" src="yui_2.7.0b-lib/element/element-min.js"></script>
<script type="text/javascript" src="yui_2.7.0b-lib/button/button-min.js"></script>


<!--begin custom header content for this example-->
<style type="text/css">

  /*  Style the <fieldset> since the Reset CSS removes the default style. */
    #pizza-order-form fieldset {

        border: 2px groove #ccc;
        margin: .5em;
        padding: .5em;

    }

  pre {
    border: solid 1px #000;
    background-color: #ccc;
    padding: 10px;
    margin: 10px;
  }

  li.yui-button-selectedmenuitem {
    background: url(yui_2.7.0b-assets/button-assets/checkbox.png) left center no-repeat;
  }

</style>
<!--end custom header content for this example-->

</head>

<body class=" yui-skin-sam">


<h1>Replacing the content of a Button's Menu</h1>

<div class="exampleIntro">
  <p>
This example illustrates how to replace the content of a Button's Menu on 
the fly.
</p>      
</div>

<!--BEGIN SOURCE CODE FOR EXAMPLE  -->

<script type="text/javascript">

  YAHOO.util.Event.onContentReady("pizza-order-form", function () {
    
    //  Change the default node name for the ButtonGroup element 
    //  so that the fieldset can be used.
    
    YAHOO.widget.ButtonGroup.prototype.NODE_NAME = "FIELDSET";
  

    //  Transform the existing radio buttons into a ButtonGroup control
  
    var oButtonGroup = new YAHOO.widget.ButtonGroup("pizza-types-fields"),

      oType1Label = YAHOO.util.Dom.get("type1-label"),
      oType2Label = YAHOO.util.Dom.get("type2-label"),

      oButton1 = oButtonGroup.getButton(0),
      oButton2 = oButtonGroup.getButton(1);


    //  Set the "label" attribute of each Button to the text of 
    //  its corresponding <label> element

    oButton1.set("label", oType1Label.innerHTML);
    oButton2.set("label", oType2Label.innerHTML);      


    //  Remove the original labels, since Buttons of type "radio" 
    //  are self labelling

    oButtonGroup.removeChild(oType1Label);
    oButtonGroup.removeChild(oType2Label);      
  

    //  Utility function to transform the data in HTML <option> 
    //  elements into JavaScript arrays of MenuItem 
    //  configuration properties
  
    var createMenuItemData = function (optiongroup) {
    
      var aOptions = YAHOO.util.Dom.get(optiongroup).getElementsByTagName("option"),
        aMenuItemData = [],
        nOptions = aOptions.length,
        oOption;
      
      for (var i = 0; i < nOptions; i++) {
        
        oOption = aOptions[i];
        aMenuItemData[i] = { text: oOption.text , value: oOption.value };

      }

      aMenuItemData.splice(0, 0, "Select Size");
      
      return aMenuItemData;
      
    };


    //  Read the data out of the <select>'s <optgroup> elements so 
    //  that it can be used to create MenuItems in a Button's Menu

    var aThinCrustSizes = createMenuItemData("thin-crust-sizes"),
      aDeepDishSizes = createMenuItemData("deep-dish-sizes");


    var onCheckedChange = function (event, menuitems) {

      var oMenu;

      if (this.get("checked")) {

        oMenu = oMenuButton.getMenu();

        //  Use the "inDocument" method of the Dom utility to 
        //  determine if the Button's Menu has been rendered.

        if (YAHOO.util.Dom.inDocument(oMenuButton.getMenu().element)) {

          //  If the Menu has been rendered, use Menu's "clearContent"
          //  method to remove all existing MenuItems, then repopulate 
          //  the Menu using the "addItems" method.

          oMenu.clearContent();
          oMenu.addItems(menuitems);
          
          //  Using the "clearContent" method removes all content
          //  from a Menu instance, effectively restoring it to its 
          //  pre-rendered state.  For this reason, it is necessary
          //  to call the "render" method after using "clearContent"
          //  so that the Menu's content is rendered into the Menu's 
          //  body element (<div class="bd">).  However, since the 
          //  Menu's root element is already in the page, it is not
          //  necessary to pass an element reference when calling the 
          //  "render" method.
          
          oMenu.render();

        }
        else {
          
          //  By default a Button's Menu is lazyloaded - meaning the 
          //  creation and rendering of all MenuItems is 
          //  deferred until the Menu is intially requested by the 
          //  user.  If the "menu" attribute of a Button is set to 
          //  an array of MenuItem configuration properties, as in 
          //  this example, Button sets the "itemData" property of its 
          //  Menu to the value of the "menu" attribute.  When the 
          //  Button's Menu is first shown, Menu uses the value of 
          //  the "itemData" to create and render all MenuItems.
          
          //  If the user clicks either the "Deep Dish" or 
          //  "Thin Crust" Buttons before the content of the 
          //  "Pizza Size" Menu has been rendered, simply set the 
          //  Menu's "itemData" property to the to the array of 
          //  MenuItem configuration properties that should be used 
          //  to build the Menu when it is lazy loaded.
          
          oMenu.itemData = menuitems;
        }          
        
      }

    };

    
    //  Register a change event handler for each radio button's 
    //  "checked" attribute that will rebuild the content of the 
    //  menu button's Menu instance when the user toggles between 
    //  the deep dish and thin crust pizzas

    oButton1.on("checkedChange", onCheckedChange, aThinCrustSizes);
    oButton2.on("checkedChange", onCheckedChange, aDeepDishSizes);      


    //  Remove the existing <select> element from the <form>, and 
    //  replace it with a menu button
    

    var oPizzaSizeSelect = YAHOO.util.Dom.get("pizza-size");
    
    oPizzaSizeSelect.parentNode.removeChild(oPizzaSizeSelect);


    var oMenuButton = new YAHOO.widget.Button({  type: "menu", 
                            label: "Select Size", 
                            name: "pizza-size", 
                            menu: aThinCrustSizes, 
                            container: "pizza-size-fields" });


    //  "selectedMenuItemChange" event handler for a Button that will set 
    //  the Button's "label" attribute to the value of the "text" 
    //  configuration property of the MenuItem that was clicked.

    var onSelectedMenuItemChange = function (event) {

      var oMenuItem = event.newValue;

      this.set("label", oMenuItem.cfg.getProperty("text"));

    };


    //  Register a "selectedMenuItemChange" event handler that will sync the 
    //  Button's "label" attribute to the MenuItem that was clicked.

    oMenuButton.on("selectedMenuItemChange", onSelectedMenuItemChange);


    //  "render" event handler for a Button's Menu - responsible for setting
    //   the default value for the Button's "selectedMenuItem" attribute.

    var onMenuRender = function (type, args, button) {

      button.set("selectedMenuItem", this.getItem(0));

    };


    //  "submit" event handler for a Button's parent form - repsonsible for 
    //  rendering a Menu that was to be lazy loaded, but never clicked on, 
    //  and therefore never rendered.

    var onFormSubmit = function (event, button) {

      var oMenuItem = button.get("selectedMenuItem"),
        UA = YAHOO.env.ua,
        oEvent,
        oMenu;

      if (!oMenuItem) {

        //  Pause submission of the form until the Button's Menu 
        //  is rendered
        YAHOO.util.Event.preventDefault(event);

        oMenu = button.getMenu();

        oMenu.addItems(oMenu.itemData);

        oMenu.subscribe("render", function () {

          var bSubmitForm;

          if (UA.ie) {
            bSubmitForm = this.fireEvent("onsubmit");
          }
          else {  // Gecko, Opera, and Safari

            oEvent = document.createEvent("HTMLEvents");
            oEvent.initEvent("submit", true, true);
            bSubmitForm = this.dispatchEvent(oEvent);

          }

          //  In IE and Safari, dispatching a "submit" event to a form 
          //  WILL cause the form's "submit" event to fire, but WILL  
          //  NOT submit the form.  Therefore, we need to call the 
          //  "submit" method as well.

          if ((UA.ie || UA.webkit) && bSubmitForm) {
            this.submit();
          }

        }, this, true);

        oMenu.render(oMenu.cfg.getProperty("container"));

      }

    };


    oMenuButton.on("appendTo", function () {

      var oMenu = this.getMenu();

      //  If a Button's "selectedMenuItem" attribute is set, the selected 
      //  MenuItem's name and value will be part of the form's data set 
      //  when its parent form is submitted.  For Buttons with Menus built
      //  entirely from script, the "selectedMenuItem" property is not 
      //  set by default.  To set the "selectedMenuItem" to a default 
      //  value, simply register a "render" event handler for the Button's
      //  Menu that sets the Button's "selectedMenuItem" attribute to the
      //  desired item in the Menu.

      oMenu.subscribe("render", onMenuRender, this);


      //  The items in a Button's Menu are lazy loaded by default: loaded 
      //  when the Button is initially clicked.  If the user never clicks 
      //  on the Button, its Menu will never be rendered, meaning the 
      //  "render" event handler registered above will never be called, 
      //  and the default value for the Button's "selectMenuItem"
      //  attribute will never be set.  Therefore, add a "submit" event 
      //  handler to the Button's parent form that will render the Menu 
      //  if the Button's "selectedMenuItem" attribute is not set.

      YAHOO.util.Event.on(this.getForm(), "submit", onFormSubmit, this);

    });
  

    var oPlaceOrder = new YAHOO.widget.Button("place-order");
    
  });

</script>


<form id="pizza-order-form" method="post" action="button-test.html">

  <fieldset>
    <legend>Pizza Order Form</legend>

    <fieldset id="pizza-types-fields">
      <legend>Pizza Type</legend>
      <label id="type1-label" for="type1">Thin Crust</label>
      <input type="radio" name="pizza-type" id="type1" value="1" checked>
      <label id="type2-label" for="type2">Deep Dish</label>
      <input type="radio" name="pizza-type" id="type2" value="2">
    </fieldset>

     <div id="pizza-size-fields">
      <label for="pizza-size">Size: </label>
      <select name="pizza-size" id="pizza-size">
        <option selected label="none" value="none">Select a size</option>
        <optgroup label="Thin Crust" id="thin-crust-sizes">
          <option value="1.1">Small</option>
          <option value="1.2">Medium</option>
          <option value="1.3">Large</option>
          <option value="1.4">Extra Large</option>
        </optgroup>
        <optgroup label="Deep Dish" id="deep-dish-sizes">
          <option value="2.1">Regular</option>
          <option value="2.2">Large</option>
        </optgroup>
      </select>
    </div>

  </fieldset>
  
  <input type="submit" id="place-order" name="place-order" value="Place Order">

</form>

<!--END SOURCE CODE FOR EXAMPLE  -->

</body>
</html>
<!-- presentbright.corp.yahoo.com uncompressed/chunked Thu Feb 19 10:53:07 PST 2009 -->

   
  








yui_2.7.0b.zip( 4,431 k)

Related examples in the same category

1.Create and use a Menu Button from HTML markup
2.Create and use a Menu Button from Javascript
3.Create and use a Split Button from HTML markup
4.Create and use a Split Button from Javascript
5.Dropdown Button based color picker
6.Fixed Width Menu Button
7.Lazy load on/off and dropdown button
8.Dropdown button with slider
9.Combine a Split Button with a Slider to create an opacity slider button, similar to Adobe Photoshop
10.Create a Menu Button whose Menu instance displays a Calendar