Jay Taylor's notes

back to listing index

Bootstrap-select

[web search]
Original source (silviomoreto.github.io)
Tags: open-source code oss bootstrap silviomoreto.github.io
Clipped on: 2014-02-21

bootstrap-select

A custom select for @twitter Bootstrap using button dropdown.

Example

Make this:

Become this:

  1. <select class="selectpicker">
  2. <option>Mustard</option>
  3. <option>Ketchup</option>
  4. <option>Relish</option>
  5. </select>

It also works with option groups:

  1. <select class="selectpicker">
  2. <optgroup label="Picnic">
  3. <option>Mustard</option>
  4. <option>Ketchup</option>
  5. <option>Relish</option>
  6. </optgroup>
  7. <optgroup label="Camping">
  8. <option>Tent</option>
  9. <option>Flashlight</option>
  10. <option>Toilet Paper</option>
  11. </optgroup>
  12. </select>

And with multiple selects:

  1. <select class="selectpicker" multiple>
  2. <option>Mustard</option>
  3. <option>Ketchup</option>
  4. <option>Relish</option>
  5. </select>

You can set different Bootstrap classes via the data-style attribute:

  1. <select class="selectpicker" data-style="btn-primary">
  2. </select>
  3. <select class="selectpicker" data-style="btn-info">
  4. </select>
  5. <select class="selectpicker" data-style="btn-success">
  6. </select>
  7. <select class="selectpicker" data-style="btn-warning">
  8. </select>
  9. <select class="selectpicker" data-style="btn-danger">
  10. </select>
  11. <select class="selectpicker" data-style="btn-inverse">
  12. </select>

You can add a search input by passing data-live-search="true" attribute:

You can also use the title attribute as an alternative to display when the option is selected:

Using the title attribute on a multiple select will show the default prompt text when nothing is selected (note: this will not work on single select elements as they must have a selected value):

  1. <select class="selectpicker" multiple title='Choose one of the following...'>
  2. <option>Mustard</option>
  3. <option>Ketchup</option>
  4. <option>Relish</option>
  5. </select>

Using the data-selected-text-format attribute on a multiple select you can specify how the selection is displayed.

The supported values are:

  • values A comma delimted list of selected values. (default)
  • count If one item is selected, then the value is shown, if more than one is selected then the number of selected items is displayed, eg 2 of 6 selected
  • count > x Where X is the number of items selected when the display format changes from values to count
  1. <select class="selectpicker" multiple data-selected-text-format="count">
  2. <option>Mustard</option>
  3. <option>Ketchup</option>
  4. <option>Relish</option>
  5. </select>
  1. <select class="selectpicker" multiple data-selected-text-format="count>3">
  2. <option>Mustard</option>
  3. <option>Ketchup</option>
  4. <option>Relish</option>
  5. <option>Onions</option>
  6. </select>

You can also show the tick icon on single select with the show-tick class:

  1. <select class="selectpicker show-tick">
  2. <option>Mustard</option>
  3. <option>Ketchup</option>
  4. <option>Relish</option>
  5. </select>

The bootstrap menu arrow can be added with the show-menu-arrow class:

  1. <select class="selectpicker show-menu-arrow">
  2. <option>Mustard</option>
  3. <option>Ketchup</option>
  4. <option>Relish</option>
  5. </select>

Classes added to the options are transferred to the select:

  1. <select class="selectpicker">
  2. <option>Mustard</option>
  3. <option class="special">Ketchup</option>
  4. <option>Relish</option>
  5. </select>
  1. .special {
  2. font-style: italic;
  3. font-weight: bold !important;
  4. color:#bc0000 !important;
  5. background:#000;

Apply .span* class to the selects to set the width.

  1. <div class="row-fluid">
  2. <select class="selectpicker span2">
  3. </select>
  4. </div>

Alternatively, use the data-width attribute to set the width of the select. Set data-width to 'auto' to automatically adjust the width of the select to its widest option. An exact value can also be specified, e.g., 300px or 50%.

  1. <div class="row-fluid">
  2. <select class="selectpicker" data-width="auto">
  3. </select>
  4. <select class="selectpicker" data-width="100px">
  5. </select>
  6. <select class="selectpicker" data-width="75%">
  7. </select>
  8. </div>

Add the disabled attribute to the select to apply the disabled class.

  1. <select class="selectpicker" disabled>
  2. <option>Mustard</option>
  3. <option>Ketchup</option>
  4. <option>Relish</option>
  5. </select>

Adding disabled="disabled" to an option transfers to the menu.

  1. <select class="selectpicker" disabled>
  2. <option>Mustard</option>
  3. <option disabled="disabled">Ketchup</option>
  4. <option>Relish</option>
  5. </select>

Adding disabled="disabled" to an option group cascades to the children.

  1. <select class="selectpicker">
  2. <optgroup label="Picnic" disabled>
  3. <option>Mustard</option>
  4. <option>Ketchup</option>
  5. <option>Relish</option>
  6. </optgroup>
  7. <optgroup label="Camping">
  8. <option>Tent</option>
  9. <option>Flashlight</option>
  10. <option>Toilet Paper</option>
  11. </optgroup>
  12. </select>

The size option is set to 'auto' by default. When size is set to 'auto', the menu always opens up to show as many items as the window will allow without being cut off. Set size to false to always show all items. The size of the menu can also be specifed using the data-size attribute.

  1. <select class="selectpicker">
  2. </select>
  3. Note: this is the same as:
  4. <select class="selectpicker" data-size="auto">
  5. </select>

Specify a number for data-size to choose the maximum number of items to show in the menu.

  1. <select class="selectpicker" data-size="5">
  2. </select>

You can use the plugin scrollYou cross-browser native scroll. The example below behave like mountain lion.

  1. <select class="selectpicker" data-size="5">
  2. </select>

Add data-divider="true" to an option to turn it into a divider.

  1. <select class="selectpicker">
  2. <option data-divider="true"></option>
  3. </select>

Add subtext to an option or optgroup with the data-subtext attribute:

With showSubtext set to true.
  1. <select class="selectpicker">
  2. <option data-subtext="Heinz">Ketchup</option>
  3. </select>

Add an icon to an option or optgroup with the data-icon attribute:

  1. <select class="selectpicker">
  2. <option data-icon="icon-heart">Ketchup</option>
  3. </select>

Insert custom HTML into the option with the data-content attribute:

  1. <select class="selectpicker">
  2. <option data-content="<span class='label label-success'>Relish</span>">Relish</option>
  3. </select>

Add a header to the dropdown menu, e.g. header: 'Select a condiment' or data-header="Select a condiment"

  1. <select class="selectpicker" data-header="Select a condiment">
  2. </select>

Append the select to a specific element, e.g. container: 'body' or data-container=".main-content"

  1. <div style=";">
  2. <select class="selectpicker">
  3. </select>
  4. <select class="selectpicker" data-container="body">
  5. </select>
  6. <div>

dropupAuto is set to true by default, which automatically determines whether or not the select should be a dropup. If dropupAuto is set to false, manually make the select a dropup menu by adding the .dropup class to the select.

  1. <select class="selectpicker dropup">
  2. </select>

Usage

Create your <select> with the .selectpicker class.

  1. <select class="selectpicker">
  2. <option>Mustard</option>
  3. <option>Ketchup</option>
  4. <option>Relish</option>
  5. </select>

Enable Bootstrap-Select via JavaScript:

  1. $('.selectpicker').selectpicker();

Options can be passed via data attributes or JavaScript.

  1. $('.selectpicker').selectpicker({
  2. style: 'btn-info',
  3. size: 4

Options

Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-, as in data-style="".

Name type default description
container string | false false appends the select to a specific element or selector, e.g., container: 'body' | '.main-body'
countSelectedText string '{0} of {1} selected' sets the format for the text displayed when selectedTextFormat is count or count > #. {0} is the selected amount. {1} is total available for selection.
dropupAuto boolean true checks to see which has more room, above or below. If the dropup has enough room to fully open normally, but there is more room above, the dropup still opens normally. Otherwise, it becomes a dropup. If dropupAuto is set to false, dropups must be called manually.
header string false adds a header to the top of the menu; includes a close button by default
hideDisabled boolean false removes disabled options and optgroups from the menu data-hide-disabled: true
selectedTextFormat 'values' | 'count' | 'count > #' (where # is an integer) null specifies how the selection is displayed with a multiple select (see here
size 'auto' | integer | false 'auto'

when set to 'auto', the menu always opens up to show as many items as the window will allow without being cut off

set to false to always show all items

showSubtext boolean false subtext associated with a selected option will be displayed in the button data-show-subtext: true
showIcon boolean true Display icon(s) associated with selected option(s) in the button. If false, icons will not be displayed in the button.
showContent boolean true Display custom HTML associated with selected option(s) in the button. If false, the option value will be displayed instead.
style string | null null

apply a class to the button, see bootstrap buttons styles

title string | null null

Set the default text for bootstrap-select

width 'auto' | '#px' | '#%' | null (where # is an integer) null

auto automatically adjusts the width of the select to accommodate its widest option

set the width of the select manually, e.g. 300px or 50%

Methods

val()

You can set the selected value by calling the val method on the element.

  1. $('.selectpicker').selectpicker('val', 'Mustard');
  2. $('.selectpicker').selectpicker('val', ['Mustard','Relish']);

This is different to calling val() directly on the select element. If you call val() on the element directly the bootstrap-select ui will not refresh (as the change event only fires from user interaction). If you use val() directly, you will need to manually re-render the ui

  1. $('.selectpicker').val('Mustard');
  2. $('.selectpicker').selectpicker('render');
  3. //this is the equivalent of the above
  4. $('.selectpicker').selectpicker('val', 'Mustard');

selectAll()

This will select all items in a multiple select.

  1. $('.selectpicker').selectpicker('selectAll');

deselectAll()

This will deselect all items in a multiple select.

  1. $('.selectpicker').selectpicker('deselectAll');

render()

You can force a re-render of the bootstrap-select ui with the render method. This is useful if you programatically change any underlying values that affect the layout of the element.

  1. $('.selectpicker').selectpicker('render');

mobile()

Enable mobile scrolling by calling $('.selectpicker').selectpicker('mobile'). The method for detecting the browser is left up to the user. This enables the device's native menu for select menus.

  1. if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
  2. $('.selectpicker').selectpicker('mobile');

setStyle()

Modify the class(es) associated with either the button itself or its container.

If changing the class on the container:

  1. $('.selectpicker').addClass('span12').selectpicker('setStyle');

If changing the class(es) on the button (altering data-style):

  1. // Replace Class
  2. $('.selectpicker').selectpicker('setStyle', 'btn-danger');
  3. // Add Class
  4. $('.selectpicker').selectpicker('setStyle', 'btn-large', 'add');
  5. // Remove Class
  6. $('.selectpicker').selectpicker('setStyle', 'btn-large', 'remove');

refresh()

To programmatically update a select with JavaScript, first manipulate the select, then use the refresh method to update the UI to match the new state. This is necessary when removing or adding options, or when disabling/enabling a select via JavaScript.

  1. $('.selectpicker').selectpicker('refresh');
  1. <select class="selectpicker remove-example">
  2. <option value="Mustard">Mustard</option>
  3. <option value="Ketchup">Ketchup</option>
  4. <option value="Relish">Relish</option>
  5. </select>
  6. <button class="btn btn-warning rm-mustard">Remove Mustard</button>
  7. <button class="btn btn-danger rm-ketchup">Remove Ketchup</button>
  8. <button class="btn btn-success rm-relish">Remove Relish</button>
  9. $('.rm-mustard').click(function() {
  10. $('.remove-example').find('[value=Mustard]').remove();
  11. $('.remove-example').selectpicker('refresh');
  1. $('.ex-disable').click(function() {
  2. $('.disable-example').prop('disabled',true);
  3. $('.disable-example').selectpicker('refresh');
  4. $('.ex-enable').click(function() {
  5. $('.disable-example').prop('disabled',false);
  6. $('.disable-example').selectpicker('refresh');

hide()

To programmatically hide the bootstrap-select use the hide method to hide it.

  1. $('.selectpicker').selectpicker('hide');

show()

To programmatically show the bootstrap-select use the show method to show it.

  1. $('.selectpicker').selectpicker('show');