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.
The following code snippet will help you getting started.
- <!-- Include Twitter Bootstrap and jQuery: -->
- <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css"/>
- <script type="text/javascript" src="js/jquery.min.js"></script>
- <script type="text/javascript" src="js/bootstrap.min.js"></script>
- <!-- Include the plugin's CSS and JS: -->
- <script type="text/javascript" src="js/bootstrap-multiselect.js"></script>
- <link rel="stylesheet" href="css/bootstrap-multiselect.css" type="text/css"/>
- <!-- Build your select: -->
- <select class="multiselect" multiple="multiple">
- <option value="cheese">Cheese</option>
- <option value="tomatoes">Tomatoes</option>
- <option value="mozarella">Mozzarella</option>
- <option value="mushrooms">Mushrooms</option>
- <option value="pepperoni">Pepperoni</option>
- <option value="onions">Onions</option>
- </select>
- <!-- Initialize the plugin: -->
- <script type="text/javascript">
- $(document).ready(function() {
- $('.multiselect').multiselect();
- });
- </script>
The best way learning from the examples is using firebug, chrome developer tools or similar tools for your browser. Examine the generated markup and used javascript code.
|
Normal select. The plugin will do single selection using radio buttons rather than multiple selection using checkboxes. |
|
Select with preselected options: <option value="cheese" selected>Cheese</option>
|
|
Multiselect with a 'Select all' option |
|
Multiselect with a 'Select all' option and filtering enabled using the enableFiltering option.
|
|
The 'Select all' option automatically repsonds to the selection of the user. In particular, it responds to the initial state of the select. |
|
As link using buttonClass: 'btn btn-link' .
|
|
Small button using buttonClass: 'btn btn-default btn-sm' .
|
|
Multiple select within a group with add-ons and default container for the plugin: buttonContainer: '<div class="btn-group" />' .
|
|
Using the onChange 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.
|
|
For long option lists the maxHeight option can be set.
|
|
The plugin supports disabled options, too: disabled="disabled"
|
|
Use the buttonWidth option to adjust the width of the button.
|
|
Using the onChange option to prevent user from deselecting selected options.
|
|
Option groups are detected automatically and for each option group an header element is added: <optgroup label="Mathematics">...</optgroup>
|
|
Option groups and options without any group are supported simultaneously. |
|
Using the selectedClass option to turn off the active class for selected options.
|
|
Specifiy an alternaitve label for the options: <option label="label"></option>
|
|
Make the menu drop right instead of dropping left with dropRight .
|
|
Using the data-role="multiselect" attribute for automatic wireup.
|
|
The multiselect will adopt the state of the select: <select disabled></select> .
|
|
The button will keep the tabindex of the select.
|
|
Using optgroups s with filtering and the select all option.
|
Option | Explanation | Usage |
---|---|---|
buttonText |
A function returning the string displayed if options are selected. All currently selected options and the select are passed as argument. In addition HTML can be added to the button, for example the caret icon seen in the examples. |
|
numberDisplayed |
This option can be used to define the number of displayed option before the text defined in nSelectedText is used. This option may not be available when using a custom buttonText function. |
|
buttonTitle |
Function defining the title of the button. Similar to the buttonText option. |
|
buttonClass |
The class of the dropdown button. Default: btn . |
|
buttonWidth |
The width of the dropdown button. Default: auto .
Allowed formats:
false .
|
|
buttonContainer |
The used container holding both the dropdown button and the dropdown menu. Default: <div class="btn-group" /> .
|
|
label |
Function to write the label of the item. |
|
selectedClass |
The class applied to the parent <li> of selected items. Default: active . |
|
onChange |
This event handler is triggered when the selected options are changed. |
|
onDropdownShow |
This event handler is triggered when the dropdown are shown. |
|
onDropdownHide |
This event handler is triggered when the dropdown are hidden. |
|
maxHeight |
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. |
|
includeSelectAllOption |
If set to true a 'Select all' option will be added.
|
|
selectAllText |
The label for the 'Select all' option. |
|
selectAllValue |
The value by which the select all option is identified. |
|
enableFiltering |
If set to true a search field will be added to filter the visible options.
|
|
enableCaseInsensitiveFiltering |
The same as enableFiltering but with case insensitive filtering.
|
|
filterPlaceholder |
The placeholder used in the search field if filtering is enabled. |
|
dropRight |
Will make the menu drop right if set to true .
|
|
This method is used to destroy the plugin on the given element - meaning unbinding the plugin. |
|
This method is used to refresh the checked checkboxes based on the currently selected options within the select. Click 'Select some options' so select some of the options (meaning added the |
|
Rebuilds the whole dropdown menu. All selected options will remain selected (if still existent!). |
|
Selects an option by its value. Works also using an array of values. |
|
Deselect an option by its value. Works also using an array of values. |
|
Provides data for building the select's options the following way:
|
|
Used to change configuration after initializing the multiselect. This may be useful in combination with |
|
Disable both the underlying select and the dropdown button. |
|
Enable both the underlying select and the dropdown button. |
|
Use "Select" and "Deselect" to select or deselect cheese and tomatoes. Use "Values" to display the currently selected elements by using |
|
Use the button to toggle the selection. |
|
When deselecting an option you will be asked for confirmation. |
|
Switching between different configuration sets is possible using a combination of |
|
Limit the number of selected options using the |
|
Record the order the options are selected. When selecting an item an ordering number will be incremented and saved within the option. |
|
Simulate single selections using checkboxes. The behavior will be similar to a multiselect with radio buttons. |
|
For additionaly styling of the multiselect button the CSS class multiselect
can be used.
Text alignment combined with fixed width and bold, underlined text for option group headers. |
|
Access the select all option by using the |
|
© 2012, 2013 David Stutz - dual licensed: Apache License v2.0, BSD 3-Clause License