Skip to content

Commit

Permalink
bugfix:When loading a page and one of the options has already a value…
Browse files Browse the repository at this point in the history
… selected the correct label was not being shown
  • Loading branch information
botelho committed Jan 18, 2013
1 parent 4607fe6 commit 68e8287
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions js/jquery.dropdown.js
Expand Up @@ -50,15 +50,15 @@

var self = this;
this.minZIndex = 1000;
this._transformSelect();
var value = this._transformSelect();
this.opts = this.listopts.children( 'li' );
this.optsCount = this.opts.length;
this.size = { width : this.dd.width(), height : this.dd.height() };

var elName = this.$el.attr( 'name' ), elId = this.$el.attr( 'id' ),
inputName = elName !== undefined ? elName : elId !== undefined ? elId : 'cd-dropdown-' + ( new Date() ).getTime();

this.inputEl = $( '<input type="hidden" name="' + inputName + '"></input>' ).insertAfter( this.selectlabel );
this.inputEl = $( '<input type="hidden" name="' + inputName + '" value="' + value + '"></input>' ).insertAfter( this.selectlabel );

this.selectlabel.css( 'z-index', this.minZIndex + this.optsCount );
this._positionOpts();
Expand All @@ -69,19 +69,26 @@
},
_transformSelect : function() {

var optshtml = '', selectlabel = '';
var optshtml = '', selectlabel = '', value = -1;
this.$el.children( 'option' ).each( function() {

var $this = $( this ),
val = isNaN( $this.attr( 'value' ) ) ? $this.attr( 'value' ) : Number( $this.attr( 'value' ) ) ,
classes = $this.attr( 'class' ),
selected = $this.attr( 'selected' ),
label = $this.text();

val !== -1 ?
classes !== undefined ?
optshtml += '<li data-value="' + val + '"><span class="' + classes + '">' + label + '</span></li>' :
optshtml += '<li data-value="' + val + '"><span>' + label + '</span></li>' :
if( val !== -1 ) {
optshtml +=
classes !== undefined ?
'<li data-value="' + val + '"><span class="' + classes + '">' + label + '</span></li>' :
'<li data-value="' + val + '"><span>' + label + '</span></li>';
}

if( selected ) {
selectlabel = label;
value = val;
}

} );

Expand All @@ -90,6 +97,8 @@
this.dd = $( '<div class="cd-dropdown"/>' ).append( this.selectlabel, this.listopts ).insertAfter( this.$el );
this.$el.remove();

return value;

},
_positionOpts : function( anim ) {

Expand Down

0 comments on commit 68e8287

Please sign in to comment.