Commit b9378c3f authored by David Stutz's avatar David Stutz

Scrollbar support, option names.

Added support for adding a maximum height property to the dropdown list.
Renamed some options.
parent 6a3fbe8e
......@@ -2,6 +2,8 @@
Bootstrap Multiselect is a JQuery based plugin to provide an intuitive user interface for using select inputs with the multiple attribute present. Instead of a select a bootstrap button will be shown as dropdown menu containing the single options as checkboxes.
**Note:** The option names may have changed due to the latest updates.
## Demo
A demo of different configurations can be found [here](http://davidstutz.github.com/bootstrap-multiselect/).
......@@ -22,17 +24,17 @@ These examples can also be seen in action in index.html:
$('#example1').multiselect();
$('#example2').multiselect();
$('#example3').multiselect({
button: 'btn btn-link'
buttonClass: 'btn btn-link'
});
$('#example4').multiselect({
button: 'btn btn-small'
buttonClass: 'btn btn-small'
});
$('#example5').multiselect({
button: 'btn btn-primary disabled'
buttonClass: 'btn btn-primary disabled'
});
$('#example6').multiselect();
$('.example7').multiselect({
container: '<span class="dropdown" />',
buttonContainer: '<span class="dropdown" />',
});
});
</script>
......@@ -124,23 +126,23 @@ These examples can also be seen in action in index.html:
## Configuration Options
**button**
**buttonClass**
Define the appearance of the button using classes. See the [Bootstrap documentation](http://twitter.github.com/bootstrap/base-css.html#buttons) for more information.
$(document).ready(function() {
$('.multiselect').multiselect({
'none': 'select something...'
buttonClass: 'btn btn-small'
});
});
**width**
**buttonWidth**
The width of the dropdown button.
$(document).ready(function() {
$('.multiselect').multiselect({
'width': 'auto', // Default
buttonWidth: 'auto', // Default
});
});
......@@ -150,52 +152,63 @@ The width can be defined using all formats accepted by CSS:
50%
auto
**text**
**buttonText**
Defining the text of the button. Must be a function returning a string. All currently selected options are passed as parameter.
$(document).ready(function() {
$('.multiselect').multiselect({
'text': function(options) {
buttonText: function(options) {
if (options.length == 0) {
return 'None selected';
return 'None selected <b class="caret"></b>';
}
else if (options.length > 3) {
return options.length + ' selected';
return options.length + ' selected <b class="caret"></b>';
}
else {
var selected = '';
options.each(function() {
selected += $(this).text() + ', ';
});
return selected.substr(0, selected.length -2);
return selected.substr(0, selected.length -2) + ' <b class="caret"></b>';
}
},
});
});
**container**
**buttonContainer**
The used container holding both the dropdown button and the dropdown menu.
$(document).ready(function() {
$('.multiselect').multiselect({
container: '<span class="dropdown" />',
buttonContainer: '<span class="dropdown" />',
});
});
**onchange**
**onChange**
Assign an event handler to the change event:
$(document).ready(function() {
$('.multiselect').multiselect({
onchange:function(element, checked){
onChange:function(element, checked){
alert('Change event invoked!');
console.log(element);
}
});
});
**maxHeight**
Define the maximum height property of the dropdown list. If the maximum height of the option list is exceeded a scrollbar will be displayed.
$(document).ready(function() {
$('.multiselect').multiselect({
// Or false for no maximum height.
maxHeight: 400,
});
});
## Methods
......
......@@ -17,26 +17,40 @@
<script type="text/javascript">
$(document).ready(function() {
$('#example1').multiselect();
$('#example2').multiselect();
$('#example3').multiselect({
button: 'btn btn-link'
buttonClass: 'btn btn-link'
});
$('#example4').multiselect({
button: 'btn btn-small'
buttonClass: 'btn btn-small'
});
$('#example5').multiselect({
button: 'btn btn-primary disabled'
buttonClass: 'btn btn-primary disabled'
});
$('#example6').multiselect();
$('.example7').multiselect({
container: '<span class="dropdown" />',
buttonContainer: '<span class="dropdown" />',
});
$('#example9').multiselect({
onchange:function(element, checked){
onChange:function(element, checked){
alert('Change event invoked!');
console.log(element);
}
});
for (var i = 1; i <= 100; i++) {
$('#example11').append('<option value="' + i + '">Options ' + i + '</option>');
}
$('#example11').multiselect({
maxHeight: 200,
})
});
</script>
<div class="container">
......@@ -46,6 +60,9 @@
Bootstrap Multiselect is a JQuery based plugin to provide an intuitive user interface for using select inputs with the multiple attribute present. Instead of a select a bootstrap button will be shown as dropdown menu containing the single options as checkboxes.
</p>
</div>
<p class="alert alert-info">
<b>Note:</b> The option names may have changed due to the latest updates.
</p>
<div class="page-header">
<h1>Examples</h1>
</div>
......@@ -95,7 +112,7 @@
</select>
</td>
<td>
As link using <code>button: 'btn btn-link'</code>.
As link using <code>buttonClass: &apos;btn btn-link&apos;</code>.
</td>
</tr>
<tr>
......@@ -110,7 +127,7 @@
</select>
</td>
<td>
Small button using <code>button: 'btn btn-small'</code>.
Small button using <code>buttonClass: &apos;btn btn-small&apos;</code>.
</td>
</tr>
<tr>
......@@ -125,7 +142,7 @@
</select>
</td>
<td>
Disabled using <code>button: 'btn btn-primary disabled'</code>.
Disabled using <code>buttonClass: &apos;btn btn-primary disabled&apos;</code>.
</td>
</tr>
<tr>
......@@ -145,7 +162,7 @@
</div>
</td>
<td>
Multiple select within a group with add-ons and default container for the plugin: <code>container: '&lt;div class="btn-group" /&gt;'</code>.
Multiple select within a group with add-ons and default container for the plugin: <code>buttonContainer: &apos;&lt;div class="btn-group" /&gt;&apos;</code>.
</td>
</tr>
<tr>
......@@ -168,7 +185,7 @@
</div>
</td>
<td>
Multiple selects within a group using <code>container: '&lt;span class="dropdown" /&gt;'</code>.
Multiple selects within a group using <code>buttonContainer: &apos;&lt;span class="dropdown" /&gt;&apos;</code>.
</td>
</tr>
<tr>
......@@ -183,7 +200,17 @@
</select>
</td>
<td>
Using the <code>onchange</code> option you can add an event handler to the change event. The event handler gets the selected option as first parameter and a variable saying whether the option is checked or not as second one.
Using the <code>onChange</code> option you can add an event handler to the change event. The event handler gets the selected option as first parameter and a variable saying whether the option is checked or not as second one.
</td>
</tr>
<tr>
<td>
<select id="example11" multiple="multiple">
</select>
</td>
<td>
For long option lists the <code>maxheight</code> option can be set.
</td>
</tr>
</table>
......@@ -193,24 +220,25 @@
<script type="text/javascript">
$(document).ready(function() {
$('#example8').multiselect({
container: '<span />',
buttonContainer: '<span />',
});
$('#example8-destroy').on('click', function() {
$('#example8').multiselect('destroy');
});
$('#example10').multiselect({
container: '<span />',
buttonContainer: '<span />',
});
$('#example10-select').on('click', function() {
$('option[value="tomatoes"]', $('#example10')).attr('selected', true);
$('option[value="mushrooms"]', $('#example10')).attr('selected', true);
$('option[value="onions"]', $('#example10')).attr('selected', true);
$('option[value="tomatoes"]', $('#example10')).prop('selected', 'selected');
$('option[value="mushrooms"]', $('#example10')).prop('selected', 'selected');
$('option[value="onions"]', $('#example10')).prop('selected', 'selected');
alert('Selected Tomatoes, Mushrooms and Onions.');
});
$('#example10-deselect').on('click', function() {
$('option', $('#example10')).each(function(element) {
$(this).attr('selected', false);
})
$(this).removeAttr('selected');
});
});
$('#example10-refresh').on('click', function() {
$('#example10').multiselect('refresh');
......@@ -278,21 +306,21 @@
&lt;script type=&quot;text/javascript&quot;&gt;
$(document).ready(function() {
$(&#39;.multiselect&#39;).multiselect({
&apos;button&apos;: &apos;btn&apos;,
&apos;width&apos;: &apos;auto&apos;,
&apos;text&apos;: function(options) {
buttonClass: &apos;btn&apos;,
buttonWidth: &apos;auto&apos;,
buttonText: function(options) {
if (options.length == 0) {
return &apos;None selected&apos;;
return &apos;None selected &lt;b class="caret"&gt;&lt;/b&gt;&apos;;
}
else if (options.length > 3) {
return options.length + &apos; selected&apos;;
return options.length + &apos; selected &lt;b class="caret"&gt;&lt;/b&gt;&apos;;
}
else {
var selected = &apos;&apos;;
options.each(function() {
selected += $(this).text() + &apos;, &apos;;
});
return selected.substr(0, selected.length -2);
return selected.substr(0, selected.length -2) + &apos; &lt;b class="caret"&gt;&lt;/b&gt;&apos;;
}
}
});
......@@ -335,26 +363,26 @@
</thead>
<tbody>
<tr>
<td><code>text</code></td>
<td>A function returning the string displayed if options are selected. All currently selected options are passed as argument.</td>
<td><code>buttonText</code></td>
<td>A function returning the string displayed if options are selected. All currently selected options are passed as argument. In addition HTML can be added to the button, for example the caret icon seen in the examples.</td>
<td>
<pre>
&lt;script type=&quot;text/javascript&quot;&gt;
$(document).ready(function() {
$(&#39;.multiselect&#39;).multiselect({
&apos;text&apos;: function(options) {
buttonText: function(options) {
if (options.length == 0) {
return &apos;None selected&apos;;
return &apos;None selected &lt;b class="caret"&gt;&lt;/b&gt;&apos;;
}
else if (options.length > 3) {
return options.length + &apos; selected&apos;;
return options.length + &apos; selected &lt;b class="caret"&gt;&lt;/b&gt;&apos;;
}
else {
var selected = &apos;&apos;;
options.each(function() {
selected += $(this).text() + &apos;, &apos;;
});
return selected.substr(0, selected.length -2);
return selected.substr(0, selected.length -2) + &apos; &lt;b class="caret"&gt;&lt;/b&gt;&apos;;
}
}
});
......@@ -364,14 +392,14 @@
</td>
</tr>
<tr>
<td><code>button</code></td>
<td><code>buttonClass</code></td>
<td>The width of the dropdown button. Default: <code>btn</code>.</td>
<td>
<pre>
&lt;script type=&quot;text/javascript&quot;&gt;
$(document).ready(function() {
$(&#39;.multiselect&#39;).multiselect({
&apos;button&apos;: &apos;btn-primary btn-large&apos;,
buttonClass: &apos;btn-primary btn-large&apos;,
});
});
&lt;/script&gt;
......@@ -379,7 +407,7 @@
</td>
</tr>
<tr>
<td><code>width</code></td>
<td><code>buttonWidth</code></td>
<td>
The width of the dropdown button. Default: <code>auto</code>.
Allowed formats:
......@@ -394,7 +422,7 @@
&lt;script type=&quot;text/javascript&quot;&gt;
$(document).ready(function() {
$(&#39;.multiselect&#39;).multiselect({
&apos;width&apos;: &apos;300px&apos;,
buttonWidth: &apos;300px&apos;,
});
});
&lt;/script&gt;
......@@ -402,7 +430,7 @@
</td>
</tr>
<tr>
<td><code>container</code></td>
<td><code>buttonContainer</code></td>
<td>
The used container holding both the dropdown button and the dropdown menu. Default: <code>&lt;div class="btn-group" /&gt;</code>.
</td>
......@@ -411,7 +439,7 @@
&lt;script type=&quot;text/javascript&quot;&gt;
$(document).ready(function() {
$(&#39;.multiselect&#39;).multiselect({
&apos;container&apos;: &apos;&lt;span class="dropdown" /&gt;&apos;,
buttonContainer: &apos;&lt;span class="dropdown" /&gt;&apos;,
});
});
&lt;/script&gt;
......@@ -419,20 +447,37 @@
</td>
</tr>
<tr>
<td><code>onchange</code></td>
<td><code>onChange</code></td>
<td>
The used container holding both the dropdown button and the dropdown menu. Default: <code>&lt;div class="btn-group" /&gt;</code>.
This event handler is triggered when the selected options are changed.
</td>
<td>
<pre>
&lt;script type=&quot;text/javascript&quot;&gt;
$(document).ready(function() {
$(&#39;.multiselect&#39;).multiselect({
&apos;onchange&apos;: function(element, checked) {
onChange: function(element, checked) {
alert(&apos;Change event invoked!&apos;);
},
});
});
&lt;/script&gt;
</pre>
</td>
</tr>
<tr>
<td><code>maxHeight</code></td>
<td>
Used for a long list of options this option defines the maximum height of the dropdown menu. If the size is exceeded a scrollbar will appear.
</td>
<td>
<pre>
&lt;script type=&quot;text/javascript&quot;&gt;
$(document).ready(function() {
$(&#39;.multiselect&#39;).multiselect({
maxHeight: 400,
});
});
&lt;/script&gt;
</pre>
</td>
......
......@@ -24,10 +24,19 @@
this.options = this.getOptions(options);
this.select = $(select);
this.container = $(this.options.container)
.append('<button type="button" style="width:' + this.options.width + '" class="dropdown-toggle ' + this.options.button + '" data-toggle="dropdown">' + this.options.text($('option:selected', select)) + ' <b class="caret"></b></button>')
this.container = $(this.options.buttonContainer)
.append('<button type="button" style="width:' + this.options.buttonWidth + '" class="dropdown-toggle ' + this.options.buttonClass + '" data-toggle="dropdown">' + this.options.buttonText($('option:selected', select)) + '</button>')
.append('<ul class="dropdown-menu"></ul>');
// Set max height of dropdown menu to activate auto scrollbar.
if (this.options.maxHeight) {
$('ul', this.container).css({
'max-height': this.options.maxHeight + 'px',
'overflow-y': 'auto',
'overflow-x': 'hidden',
});
}
// Manually add the multiple attribute, if its not already set.
if (!this.select.attr('multiple')) {
this.select.attr('multiple', true);
......@@ -36,12 +45,13 @@
// Build the dropdown.
$('option', this.select).each($.proxy(function(index, element) {
if ($(element).is(':selected')) {
$(element).attr('selected', true);
$(element).attr('selected', 'selected');
$(element).prop('selected', 'selected');
}
$('ul', this.container).append('<li><a href="javascript:void(0);" style="padding:0;"><label style="margin:0;padding: 3px 20px 3px 20px;width:100%;height:100%;cursor:pointer;"><input style="margin-bottom:5px;" type="checkbox" value="' + $(element).val() + '" /> ' + $(element).text() + '</label</a></li>');
$('ul', this.container).append('<li><a href="javascript:void(0);" style="padding:0;"><label style="margin:0;padding:3px 20px 3px 20px;width:100%;height:100%;cursor:pointer;"><input style="margin-bottom:5px;" type="checkbox" value="' + $(element).val() + '" /> ' + $(element).text() + '</label</a></li>');
var selected = $(element).attr('selected') || false;
var selected = $(element).prop('selected') || false;
var checkbox = $('ul li input[value="' + $(element).val() + '"]', this.container);
checkbox.prop('checked', selected);
......@@ -66,12 +76,19 @@
}
var option = $('option[value="' + $(event.target).val() + '"]', this.select);
option.attr('selected', checked);
if (checked) {
option.attr('selected', 'selected');
option.prop('selected', 'selected');
}
else {
option.removeAttr('selected');
}
console.log(option);
var options = $('option:selected', this.select);
$('button', this.container).html(this.options.text(options) + ' <b class="caret"></b>');
$('button', this.container).html(this.options.buttonText(options));
this.options.onchange(option, checked);
this.options.onChange(option, checked);
}, this));
$('ul li a', this.container).on('click', function(event) {
......@@ -82,28 +99,34 @@
Multiselect.prototype = {
defaults: {
button: 'btn',
width: 'auto',
// Default text function will either print 'None selected' in case no option is selected,
// or a list of the selected options up to a length of 3 selected options.
// If more than 3 options are selected, the number of selected options is printed.
text: function(options) {
buttonText: function(options) {
if (options.length == 0) {
return 'None selected';
return 'None selected <b class="caret"></b>';
}
else if (options.length > 3) {
return options.length + ' selected';
return options.length + ' selected <b class="caret"></b>';
}
else {
var selected = '';
options.each(function() {
selected += $(this).text() + ', ';
});
return selected.substr(0, selected.length -2);
return selected.substr(0, selected.length -2) + ' <b class="caret"></b>';
}
},
container: '<div class="btn-group" />',
onchange:function(){}
// Is triggered on change of the selected options.
onChange: function() {
},
buttonClass: 'btn',
buttonWidth: 'auto',
buttonContainer: '<div class="btn-group" />',
// Maximum height of thet dropdown menu.
// If maximum height is exceeded a scrollbar will be displayed.
maxHeight: 400,
},
constructor: Multiselect,
......@@ -131,7 +154,7 @@
}
}, this));
$('button', this.container).html(this.options.text($('option:selected', this.select)) + ' <b class="caret"></b>');
$('button', this.container).html(this.options.buttonText($('option:selected', this.select)));
},
// Get options by merging defaults and given options.
......
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