Browse Source

improve percent width detection. issue #115

Igor Vaynberg 13 years ago
parent
commit
77de06ecf3
1 changed files with 10 additions and 0 deletions
  1. 10 0
      select2.js

+ 10 - 0
select2.js

@@ -1044,9 +1044,12 @@
         // abstract
         getContainerWidth: function () {
             var style, attrs, matches, i, l;
+
+            // see if there is width specified in opts
             if (this.opts.width !== undefined)
                 return this.opts.width;
 
+            // next check if there is inline style on the element that contains width
             style = this.opts.element.attr('style');
             if (style !== undefined) {
                 attrs = style.split(';');
@@ -1057,6 +1060,13 @@
                         return matches[1];
                 }
             }
+
+            // next check if css('width') can resolve a width that is percent based, this is sometimes possible
+            // when attached to input type=hidden or elements hidden via css
+            style = this.opts.element.css('width');
+            if (style.indexOf("%") > 0) return style;
+
+            // finally, fallback on the calculated width of the element
             return (this.opts.element.width() === 0 ? 'auto' : this.opts.element.width() + 'px');
         }
     });