Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Selectmenu: improved value option, added demo file

  • Loading branch information...
commit 4d56732195a9dd2e5e919b1a537a2f73dffa5c41 1 parent 9c43d64
Felix Nagel fnagel authored
8 tests/visual/selectmenu/disabled.html
@@ -121,12 +121,12 @@
121 121 <option value="someotherfile">Some other file</option>
122 122 </optgroup>
123 123 </select>
124   - <br />
125   - <button id="disable_select">Toggle disable select</button>
126   - <button id="disable_option">Toggle disable option</button>
127   - <button id="disable_optgroup">Toggle disable optgroup</button>
128 124 </fieldset>
129 125 </form>
  126 +<br />
  127 +<button id="disable_select">Toggle disable select</button>
  128 +<button id="disable_option">Toggle disable option</button>
  129 +<button id="disable_optgroup">Toggle disable optgroup</button>
130 130
131 131 </div><!-- End demo -->
132 132
62 tests/visual/selectmenu/option.html
... ... @@ -0,0 +1,62 @@
  1 +<!DOCTYPE html>
  2 +<html lang="en">
  3 +<head>
  4 + <meta charset="utf-8">
  5 + <title>jQuery UI Selectmenu - Event functionality</title>
  6 + <link rel="stylesheet" href="../../../themes/base/jquery.ui.all.css">
  7 + <script src="../../../jquery-1.6.2.js"></script>
  8 + <script src="../../../ui/jquery.ui.core.js"></script>
  9 + <script src="../../../ui/jquery.ui.widget.js"></script>
  10 + <script src="../../../ui/jquery.ui.position.js"></script>
  11 + <script src="../../../ui/jquery.ui.button.js"></script>
  12 + <script src="../../../ui/jquery.ui.menu.js"></script>
  13 + <script src="../../../ui/jquery.ui.selectmenu.js"></script>
  14 + <link rel="stylesheet" href="../../../demos/demos.css">
  15 + <script>
  16 + $(function() {
  17 + var mySelectmenu = $('select').selectmenu();
  18 +
  19 + $("#value").click( function() {
  20 + mySelectmenu.selectmenu("option", "value", "Faster");
  21 + });
  22 +
  23 + $("#dropdown").toggle( function() {
  24 + mySelectmenu.selectmenu("option", "dropdown", false);
  25 + }, function() {
  26 + mySelectmenu.selectmenu("option", "dropdown", true);
  27 + });
  28 + });
  29 + </script>
  30 + <style>
  31 + form { margin: 20px 0 0 0 }
  32 + fieldset { border: 0; }
  33 + select { width: 200px; }
  34 + .overflow ul { height: 200px; overflow: auto; }
  35 + </style>
  36 +</head>
  37 +<body>
  38 +
  39 +<div class="demo">
  40 +
  41 +<form action="#">
  42 + <fieldset>
  43 + <label for="speed">Select a speed:</label>
  44 + <select name="speed" id="speed">
  45 + <option value="Slower">Slower</option>
  46 + <option value="Slow">Slow</option>
  47 + <option value="Medium" selected="selected">Medium</option>
  48 + <option value="Fast">Fast</option>
  49 + <option value="Faster">Faster</option>
  50 + </select>
  51 + </fieldset>
  52 +</form>
  53 +<br />
  54 +<!-- <button id="dropdown">Toggle dropdown style</button> -->
  55 +<button id="value">Set option value = 'Faster'</button>
  56 +
  57 +</div><!-- End demo -->
  58 +
  59 +
  60 +
  61 +</body>
  62 +</html>
28 ui/jquery.ui.selectmenu.js
@@ -49,12 +49,15 @@ $.widget( "ui.selectmenu", {
49 49 // quick array of button and menu id's
50 50 self.ids = [ selectmenuId + '-button', selectmenuId + '-menu' ];
51 51
52   - // get options
  52 + // save options
53 53 self.items = self.element.find( 'option' );
54 54
55   - // set options
56   - options.value = self.element[0].value;
57   - options.disabled = ( self.element.attr( 'disabled' ) ) ? true : false;
  55 + // set current value
  56 + if ( options.value ) {
  57 + self.element[0].value = options.value;
  58 + } else {
  59 + options.value = self.element[0].value;
  60 + }
58 61
59 62 // catch click event of the label
60 63 self.element.bind( 'click.selectmenu', function() {
@@ -82,7 +85,8 @@ $.widget( "ui.selectmenu", {
82 85 primary: ( options.dropdown ? 'ui-icon-triangle-1-s' : 'ui-icon-triangle-2-n-s' )
83 86 }
84 87 });
85   -
  88 +
  89 + // wrap and insert new button
86 90 self.newelementWrap = $( options.wrapperElement )
87 91 .append( self.newelement )
88 92 .insertAfter( self.element );
@@ -167,6 +171,7 @@ $.widget( "ui.selectmenu", {
167 171 id: self.ids[1]
168 172 });
169 173
  174 + // wrap list
170 175 self.listWrap = $( options.wrapperElement )
171 176 .addClass( self.widgetBaseClass + '-menu' )
172 177 .css( "width", ( options.dropdown ) ? self.element.width() : self.element.width() - options.iconWidth )
@@ -176,6 +181,7 @@ $.widget( "ui.selectmenu", {
176 181 self._initSource();
177 182 self._renderMenu( self.list, options.source );
178 183
  184 + // init menu widget
179 185 self.list
180 186 .data( 'element.selectelemenu', self.element )
181 187 .menu({
@@ -185,7 +191,7 @@ $.widget( "ui.selectmenu", {
185 191
186 192 if ( item.index != self.element[0].selectedIndex ) flag = true;
187 193
188   - self._setSelected( event, item );
  194 + self._setOption( "value", item.value );
189 195 item.element = self.items[ item.index ];
190 196 self._trigger( "select", event, { item: item } );
191 197
@@ -331,12 +337,6 @@ $.widget( "ui.selectmenu", {
331 337 return this.list.find( "li" ).not( '.ui-selectmenu-optgroup' ).eq( this.element[0].selectedIndex );
332 338 },
333 339
334   - _setSelected: function( event, item ) {
335   - this.newelement.children( '.ui-button-text' ).text( item.label );
336   - this.element[0].selectedIndex = item.index;
337   - this.options.value = item.value;
338   - },
339   -
340 340 _toggle: function( event ) {
341 341 if ( this.opened ) {
342 342 this.close( event );
@@ -347,11 +347,13 @@ $.widget( "ui.selectmenu", {
347 347
348 348 _setOption: function( key, value ) {
349 349 this._super( "_setOption", key, value );
  350 +
350 351 if ( key === "appendTo" ) {
351 352 this.listWrap.appendTo( $( value || "body", this.element[0].ownerDocument )[0] );
352 353 }
353   - if ( key === "value" && value) {
  354 + if ( key === "value" && value !== undefined ) {
354 355 this.element[0].value = value;
  356 + this.newelement.children( '.ui-button-text' ).text( this.items.eq( this.element[0].selectedIndex ).text() );
355 357 }
356 358 if ( key === "disabled" ) {
357 359 this.newelement.button( "option", "disabled", value );

0 comments on commit 4d56732

Please sign in to comment.
Something went wrong with that request. Please try again.