Commit 07bf8acd authored by Chris Hynes's avatar Chris Hynes

Added support for single selection

parent 68891ec0
...@@ -114,7 +114,7 @@ ...@@ -114,7 +114,7 @@
<td> <td>
<select id="example1"> <select id="example1">
<option value="cheese" selected>Cheese</option> <option value="cheese" selected>Cheese</option>
<option value="tomatoes" selected>Tomatoes</option> <option value="tomatoes">Tomatoes</option>
<option value="mozarella">Mozzarella</option> <option value="mozarella">Mozzarella</option>
<option value="mushrooms">Mushrooms</option> <option value="mushrooms">Mushrooms</option>
<option value="pepperoni">Pepperoni</option> <option value="pepperoni">Pepperoni</option>
...@@ -122,7 +122,7 @@ ...@@ -122,7 +122,7 @@
</select> </select>
</td> </td>
<td> <td>
Normal select. The plugin will add the <code>multiple="multiple"</code> attribute automatically. The first option is selected automatically by the browser when ommitting the <code>multiple</code> attribute. But note: If the <code>mutliple</code> attribute is omitted and more than one option is selected by default, the browser will select the last selected option before the plugin has the chance to add the <code>mutliple</code> attribute. Normal select. The plugin will do single selection using radio buttons rather than multiple selection using checkboxes.
</td> </td>
</tr> </tr>
<tr> <tr>
......
...@@ -54,10 +54,7 @@ ...@@ -54,10 +54,7 @@
this.options = this.getOptions(options); this.options = this.getOptions(options);
this.$select = $(select); this.$select = $(select);
// Manually add the multiple attribute, if its not already set. this.options.multiple = this.$select.attr('multiple') == "multiple";
if (!this.$select.attr('multiple')) {
this.$select.attr('multiple', true);
}
this.$container = $(this.options.buttonContainer) this.$container = $(this.options.buttonContainer)
.append('<button type="button" class="multiselect dropdown-toggle ' + this.options.buttonClass + '" data-toggle="dropdown">' + this.options.buttonText(this.getSelected(), this.$select) + '</button>') .append('<button type="button" class="multiselect dropdown-toggle ' + this.options.buttonClass + '" data-toggle="dropdown">' + this.options.buttonText(this.getSelected(), this.$select) + '</button>')
...@@ -113,7 +110,7 @@ ...@@ -113,7 +110,7 @@
}, },
buttonClass: 'btn', buttonClass: 'btn',
dropRight: false, dropRight: false,
selectedClass: 'active', selectedClass: 'active',
buttonWidth: 'auto', buttonWidth: 'auto',
buttonContainer: '<div class="btn-group" />', buttonContainer: '<div class="btn-group" />',
...@@ -134,7 +131,9 @@ ...@@ -134,7 +131,9 @@
// Support the label attribute on options. // Support the label attribute on options.
var label = ($(element).attr('label') !== undefined) ? $(element).attr('label') : $(element).text(); var label = ($(element).attr('label') !== undefined) ? $(element).attr('label') : $(element).text();
var value = $(element).val(); var value = $(element).val();
var li = $('<li><a href="javascript:void(0);" style="padding:0;"><label style="margin:0;padding:3px 20px 3px 20px;height:100%;cursor:pointer;"><input style="margin-bottom:5px;" type="checkbox" /></label></a></li>'); var inputType = this.options.multiple ? "checkbox" : "radio";
var li = $('<li><a href="javascript:void(0);" style="padding:0;"><label style="margin:0;padding:3px 20px 3px 20px;height:100%;cursor:pointer;"><input style="margin-bottom:5px;" type="' + inputType + '" /></label></a></li>');
var selected = $(element).prop('selected') || false; var selected = $(element).prop('selected') || false;
var checkbox = $('input', li); var checkbox = $('input', li);
...@@ -183,7 +182,7 @@ ...@@ -183,7 +182,7 @@
}, this)); }, this));
// Bind the change event on the dropdown elements. // Bind the change event on the dropdown elements.
$('ul li input[type="checkbox"]', this.$container).on('change', $.proxy(function(event) { $('ul li input', this.$container).on('change', $.proxy(function(event) {
var checked = $(event.target).prop('checked') || false; var checked = $(event.target).prop('checked') || false;
if (this.options.selectedClass) { if (this.options.selectedClass) {
...@@ -200,6 +199,16 @@ ...@@ -200,6 +199,16 @@
if (checked) { if (checked) {
option.attr('selected', 'selected'); option.attr('selected', 'selected');
option.prop('selected', 'selected'); option.prop('selected', 'selected');
var $optionsNotThis = this.$container.find("input").not($(event.target));
if (!this.options.multiple) {
$optionsNotThis.prop("checked", false).change();
}
if (this.options.selectedClass == "active") {
$optionsNotThis.parents("a").css("outline", "");
}
} }
else { else {
option.removeAttr('selected'); option.removeAttr('selected');
...@@ -255,7 +264,7 @@ ...@@ -255,7 +264,7 @@
} }
if (event.keyCode == 32 || event.keyCode == 13) { if (event.keyCode == 32 || event.keyCode == 13) {
var $checkbox = $current.find('input[type="checkbox"]'); var $checkbox = $current.find('input');
$checkbox.prop("checked", !$checkbox.prop("checked")); $checkbox.prop("checked", !$checkbox.prop("checked"));
$checkbox.change(); $checkbox.change();
...@@ -350,10 +359,10 @@ ...@@ -350,10 +359,10 @@
// For IE 9 support. // For IE 9 support.
getSelected: function() { getSelected: function() {
if (navigator.appName == 'Microsoft Internet Explorer') { if (navigator.appName == 'Microsoft Internet Explorer') {
var regex = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})"); var regex = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
if (regex.exec(navigator.userAgent) != null) { if (regex.exec(navigator.userAgent) != null) {
return $('option:selected', this.$select); return $('option:selected', this.$select);
} }
} }
return $('option[selected]', this.$select); return $('option[selected]', this.$select);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment