bootstrap-select

repository·main·Indexed 27 days ago

https://github.com/snapappointments/bootstrap-select

A jQuery plugin that enhances standard HTML select elements with intuitive multiselection, searching, and Bootstrap-compatible styling. Version 1.14.0-beta3 supports Bootstrap 3, 4, and 5. Key features include live search with custom keywords, selection limits, custom HTML content via data-content, and flexible styling for buttons and options.

Tokens
6.7K
Snippets
31
Records
41
Agent score
94%

What's inside bootstrap-select

  1. Optimize performance with virtual scroll

    main

    For selects containing a large number of options, enable virtualScroll to improve performance. This ensures only items within the viewport are rendered.

    • Set virtualScroll: true to enable it for all selects.
    • Set virtualScroll to an integer to only enable virtualization if the select has at least that many options (e.g., virtualScroll: 600).
  2. Configure placeholder and selected text

    main

    Set a placeholder

    Use the title attribute on the <select> element to define the default placeholder text shown when nothing is selected.

    Set alternative selected text

    Use the title attribute on an individual <option> to define the text that is displayed in the button once that specific option is selected.

    Format multiple selection text

    For multiple select boxes, use data-selected-text-format to control how selected items are displayed. Supported values:

    • values: A comma-delimited list of selected values (default).
    • count: Shows the value if one is selected; otherwise shows "X of Y selected".
    • count > x: Switches from values to count format once more than x items are selected.
    • static: Always shows the select title (placeholder).
  3. Configure live search in bootstrap-select

    main

    To enable a search box within the dropdown, set liveSearch: true. You can further customize the search behavior:

    • liveSearchNormalize: Set to true for accent-insensitive searching.
    • liveSearchPlaceholder: Provide a string to set the placeholder text in the search input.
    • liveSearchStyle:
      • 'contains' (default): Shows options containing the search text.
      • 'startsWith': Shows only options starting with the search text.
      • Function: Provide a function (optionString, searchText) => boolean to define custom matching logic.
    • noneResultsText: Customize the message shown when no matches are found (e.g., 'No results matched {0}').
  4. Disable select boxes, options, or groups

    main

    You can disable elements within the select picker using standard HTML attributes:

    • Disable the entire select box: Add the disabled attribute to the <select> element.
    • Disable specific options: Add the disabled attribute to an <option> element.
    • Disable option groups: Add the disabled attribute to an <optgroup> element.
    <!-- Disabled Select Box -->
    <select class="selectpicker" disabled>
      <option>Mustard</option>
    </select>
    
    <!-- Disabled Option -->
    <select class="selectpicker">
      <option>Mustard</option>
      <option disabled>Ketchup</option>
    </select>
    
    <!-- Disabled Optgroup -->
    <select class="selectpicker">
      <optgroup label="Picnic" disabled>
        <option>Mustard</option>
      </optgroup>
    </select>
  5. Limit the number of selections

    main

    Use the data-max-options attribute to restrict how many items a user can select in a multiple select box. This attribute can also be applied to <optgroup> elements to limit selections within a specific group.

    To customize the message shown when the limit is reached, use the maxOptionsText option (configuration via JavaScript).

    <select class="selectpicker" multiple data-max-options="2">
      <option>Mustard</option>
      <option>Ketchup</option>
      <option>Relish</option>
    </select>
  6. Configure bootstrap-select using data attributes

    main

    You can configure bootstrap-select options using HTML data attributes. To do this, append the option name to data-. For example, to set the style or the selected text format, use data-style="" or data-selected-text-format="count" on your <select> element.

    Security Restriction: For security reasons, the following options cannot be supplied via data attributes and must be configured via JavaScript:

    • sanitize
    • sanitizeFn
    • whiteList
  7. Configure multi-select display and limits

    main

    When using a multiple select, you can control how selected items are displayed and how many can be chosen:

    Displaying Selections

    • selectedTextFormat:
      • 'values' (default): Lists selected options separated by multipleSeparator.
      • 'static': Displays the select element's title.
      • 'count > x': Displays values until x items are selected, then shows the count.
      • 'count': Alias for 'count > 1'.
    • multipleSeparator: The character used to separate values in 'values' mode (default: ', ').
    • noneSelectedText: Text shown when nothing is selected (default: 'Nothing selected').
    • countSelectedText: A string or function to format the count text. If a function, it receives (selectedCount, totalCount) and must return a string. Use {0} for selected amount and {1} for total.

    Selection Limits

    • maxOptions: An integer that limits the number of selectable options. This can be applied to the whole select or to a specific <optgroup> via a data-attribute.
    • maxOptionsText: The text displayed when the limit is reached. Can be a string or a function returning an array where array[0] is for the whole select and array[1] is for an <optgroup>.
  8. Configure bootstrap-select dependencies

    main

    Bootstrap-select has specific dependency requirements based on the version of Bootstrap you are using:

    • Bootstrap v3: Requires jQuery v1.9.1+ and Bootstrap's dropdown.js component and CSS.
    • Bootstrap v4+: Requires jQuery v1.9.1+, Bootstrap's dropdown.js component, Bootstrap's CSS, and Popper.js.

    Note: Bootstrap 4 requires bootstrap-select v1.13.0 or higher. The library attempts to automatically detect your Bootstrap version, but manual configuration may be required if detection fails.

  9. Initialize standard, optgroup, and multiple select boxes

    main

    To use bootstrap-select, add the selectpicker class to your <select> element.

    • Standard select: Use <select class="selectpicker">.
    • With optgroups: Use <optgroup label="Group Name"> inside the select.
    • Multiple select: Add the multiple attribute to the <select> element.
    <!-- Standard -->
    <select class="selectpicker">
      <option>Mustard</option>
    </select>
    
    <!-- With Optgroups -->
    <select class="selectpicker">
      <optgroup label="Picnic">
        <option>Mustard</option>
      </optgroup>
    </select>
    
    <!-- Multiple -->
    <select class="selectpicker" multiple>
      <option>Mustard</option>
    </select>
  10. Enable live search and custom keywords

    main

    You can add a search input to your select box by adding the data-live-search="true" attribute.

    To improve searchability for specific options, use the data-tokens attribute to provide custom keywords that aren't necessarily in the visible text.

    <select class="selectpicker" data-live-search="true">
      <option data-tokens="ketchup mustard">Hot Dog, Fries and a Soda</option>
      <option data-tokens="mustard">Burger, Shake and a Smile</option>
    </select>
  11. Style the selectpicker button and options

    main

    Button styling

    Apply Bootstrap button classes to the select picker using the data-style attribute (e.g., btn-primary, btn-success, btn-danger).

    Visual indicators

    • Checkmarks: Add the show-tick class to the <select> to show a checkmark icon next to selected options.
    • Individual option styles: Any class or style attribute added to an <option> will be transferred to the rendered select box elements.

    Note: The show-menu-arrow class is deprecated and will be removed in v2.0.0.

    <!-- Custom button style -->
    <select class="selectpicker" data-style="btn-primary">
      <option>Mustard</option>
    </select>
    
    <!-- Show checkmarks -->
    <select class="selectpicker show-tick">
      <option>Mustard</option>
    </select>
    
    <!-- Custom option styling -->
    <select class="selectpicker">
      <option class="special">Ketchup</option>
      <option style="background: #5cb85c; color: #fff;">Relish</option>
    </select>