diff --git a/js/jquery.nice-select.js b/js/jquery.nice-select.js index f1f9d0d..1894cdc 100644 --- a/js/jquery.nice-select.js +++ b/js/jquery.nice-select.js @@ -25,17 +25,20 @@ var $dropdown = $select.next(); var $options = $select.find('option'); + var $optgroups = $select.find('optgroup'); var $selected = $select.find('option:selected'); - $dropdown.find('.current').html($selected.data('display') || $selected.text()); + $dropdown.find('.current').html($selected.data('display') || $selected.text()); $options.each(function(i) { var $option = $(this); var display = $option.data('display'); + var group = $option.parents('optgroup').data('i'); $dropdown.find('ul').append($('
') .attr('data-value', $option.val()) .attr('data-display', (display || null)) + .attr('data-group', (group || null)) .addClass('option' + ($option.is(':selected') ? ' selected' : '') + ($option.is(':disabled') ? ' disabled' : '')) @@ -43,6 +46,15 @@ ); }); + $optgroups.each(function(i, g) { + label = $(g).attr('label') + $dropdown.find('ul li').filter(function() { + return $(this).data('group') == $(g).data('i') + }) + .wrapAll('') + .parent() + .prepend('' + label + '') + }); } }); @@ -147,4 +159,4 @@ }; -}(jQuery)); \ No newline at end of file +}(jQuery));