gianlucagiacometti / bootstrap-select
Extension for the Bootstrap select element
Package info
github.com/gianlucagiacometti/bootstrap-select
Language:JavaScript
Type:bootstrap-extension
pkg:composer/gianlucagiacometti/bootstrap-select
Requires
- twbs/bootstrap: ^5.3
- twbs/bootstrap-icons: ^1
README
Extension class for the Bootstrap (https://getbootstrap.com) select element.
This program is written in plain JavaScript and is designed for Bootstrap 5.3 and modern browsers.
The minimum version of ECMAScript is 2017. I reserve the right to switch to newer versions of ECMAScript at any time without further notice.
Although the W3C Recommendations do not mention it for HTML 5, the HTML 4.01 Recommendations (https://www.w3.org/TR/html401/interact/forms.html#h-17.6) state that: "Note. Implementors are advised that future versions of HTML may extend the grouping mechanism to allow for nested groups (i.e., OPTGROUP elements may nest). This will allow authors to represent a richer hierarchy of choices."
For this reason, the bsSelect class is recursive. Nested optgroups are not valid current HTML authoring markup and should not be treated as a supported public feature, but unexpected nested structures are handled defensively.
The bsSelect class uses the Input and Dropdown elements of Bootstrap, for a maximum level of compatibility.
This program is released under the MIT licence.
Versioning
Bootstrap Select versions follow the supported Bootstrap compatibility line.
For example:
5.3.xtargets Bootstrap5.3.x.5.4.xwill target Bootstrap5.4.x, if Bootstrap introduces changes that require a dedicated compatibility line.6.x.ywill target Bootstrap6.x.
Patch versions inside each compatibility line are used for fixes and small improvements.
Older 1.x versions are historical package versions. Starting with 5.3.1, the package version follows the Bootstrap compatibility line.
Requirements
- Bootstrap 5.3.x
- Bootstrap Icons ^1 (used only by search and option images)
Optional jQuery integration
Bootstrap Select is a plain JavaScript component and does not require jQuery.
If you want a jQuery-style application layer, gianlucagiacometti/web-toolbox provides jquery-bootstrap-select. The wrapper keeps Bootstrap Select as the underlying component and adds jQuery initialisation, helper methods, and large-list rendering modes.
composer require gianlucagiacometti/web-toolbox
Suggested script order:
<script src="/assets/jquery/jquery.min.js"></script> <script src="/assets/bootstrap/js/bootstrap.bundle.min.js"></script> <script src="/assets/bootstrap-select/bootstrap-select.js"></script> <script src="/assets/jquery-bootstrap-select/jquery-bootstrap-select.js"></script>
$("select.bootstrap-select").jqueryBootstrapSelect();
Usage
Add these lines in the html <head> section:
<link rel="stylesheet" href="bootstrap-select.css">
<script src="bootstrap-select.js"></script>
NOTE: the values of "href" and "src" attributes must reproduce the folder structure where the two files are located.
The "selectInserted" CSS animation provides an automatic way to create a new bsSelect class element.
You may:
-
Add to the <select> element the class "bootstrap-select".
<select id="myselect" class="bootstrap-select">...</select> -
Add the "bootstrap-select" class dynamically via JavaScript when the DOM is already loaded.
<select id="myselect">...</select> document.querySelector("#myselect").classList.add("bootstrap-select") -
Create a new class element manually via JavaScript without using the "bootstrap-select" class (the value of randomIndex must be a 16-digit string).
<select id="myselect">...</select> let randomIndex = "" + Date.now() + Math.floor(Math.random() * 1000) let mySelectElement = new bsSelect("myselect", randomIndex, classElementParameters)The classElementParameters argument is optional. The default value is {}.
Options
Optional parameters for the Class element
| Property | Type | Default | Accepted values | Description |
|---|---|---|---|---|
| visibility | text | collapsed | collapsed, expanded | Sets the visibility of the dropdown menu at the time of creation. |
Optional classes for the <select> element
| Class | Description |
|---|---|
| form-select-lg | Styles the <select> element as the Bootstrap <input> element with the class form-control-lg. |
| form-select-sm | Styles the <select> element as the Bootstrap <input> element with the class form-control-sm. |
| form-select-plaintext | Styles the <select> element as the Bootstrap <input> element with the class form-control-plaintext. |
| label-floating | Styles the <select> element as the Bootstrap <input> element with a floating label (alternative to label-outline). |
| label-outline | New style for Bootstrap <input> elements, which also applies to .bootstrap-select class (alternative to label-floating). |
| searchable | Makes the <select> element searchable. |
Optional properties for the <select> element
| Property | Type | Default | Description |
|---|---|---|---|
| disabled | boolean | false | Disables the <select> element. |
| multiple | boolean | false | Creates a multiple choice <select> element. |
Optional attributes for the <select> element
| Attribute | Type | Description |
|---|---|---|
| data-bs-search-text | text | The placeholder text in the search input field. If this attribute is not used, the default placeholder text will be: "Search..." |
| data-bs-toggle-button | boolean | A toggle all checkbox shows if this attribute exists and is set to "true". |
Optional properties for the <option> element
| Property | Type | Default | Description |
|---|---|---|---|
| disabled | boolean | false | Disables the <option> element. |
| selected | boolean | false | Marks the <option> element as selected. |
Optional attributes for the <option> element
| Attribute | Type | Description |
|---|---|---|
| data-bs-select-option-icon | text | The name of the Bootstrap icon which will be prepended to the text of the <option> element (without "bi-"). |
| data-bs-select-option-icon-class | text | The class or classes added to the option icon <i> element. If data-bs-select-option-icon is not set, the class is used directly, allowing custom icon libraries such as flag-icons. |
| data-bs-select-option-image | text | The "src" attribute of the image which will be inserted at the very right of the text of the <option> element. |
| data-bs-select-option-image-class | text | The name of the class which will be added to the <img> element of the <option> image. |
| data-bs-select-option-comment | text or html | The content of the comment which will be inserted below the text of the <option> element. The comment has a default style. All additional styles must be specified in the html as classes or inline styles. |
| data-bs-select-option-divider | text | If exists and set to "true", transforms the <option> element into a <hr> divider element. Dividers are supported inside optgroups. Root-level dividers are not guaranteed to preserve semantic position after sorting. |
Methods
| Name | Description | Parameters / examples |
|---|---|---|
| sort(parameters) | Sorts the options of the <select> element. | mode: "asc" / "desc" (default: "asc") disabled: "ignore" / "top" / "bottom" / "include" (default: "ignore") sortDisabled: "true" / "false" (default: "true") emptyDisabledValue: "top" / "include" (default: "top") optionGroups: "include" / "ignore" (default: "include") |
| insert(options, parent) | Inserts <option> and <optgroup> elements. Supports single elements, arrays, HTML strings, DOM elements, empty optgroups and populated optgroups. Disabled inserted optgroups disable their children. Divider options inside optgroups render as visual dividers and are not selectable. | The optional parameter parent may be: "0" (default value) => the options will be inserted as children of the listbox. "The attribute id of an <optgroup> element" => the options will be inserted as children of that <optgroup>. "The random 16-digit index of an <optgroup> element" => the options will be inserted as children of that <optgroup>. Example: FORM.select['my-multiple-select'].insert('<optgroup id="new-group" label="New group"><option value="101">One hundred one</option><option value="102">One hundred two</option></optgroup>') |
| remove(parameters) | Removes one or more <option> or <optgroup> elements according to the specified parameters. | value: single value values: array of values id: single element id ids: array of element ids index: a single 16-digit random index of the element String shorthand is supported: remove('new-group') is equivalent to remove({ id: 'new-group' }). |
| value() value(values, parameters) |
Gets or sets selected values. | If neither values nor parameters are specified, the method gets the selected value of the non-multiple <select> element or the array of values for the multiple <select> element. Accepted values are: 1. single value for a non-multiple <select> element (the first element will be taken as the selected value if an array is provided) 2. a single value or an array of values for a multiple <select> element Accepted parameters are: { swap: "true" } (default value) => the new values will be the only selected options. { swap: "false" } => the new values will be added to the existing selected values. { disabled: "false" } (default value) => disabled options will NOT be set as selected if their value is provided. { disabled: "true" } => disabled options will be set selected if their value is provided. |
| disabled(parameters) | Sets the <select> element as disabled or not disabled. | Accepted parameters are: true false (default value if the parameter is omitted or is not a boolean) |
| readonly(parameters) | Sets the <select> element as readonly. Its effect is similar to the effect of the method disabled, but the background of element will remain unchanged. | Accepted parameters are: true false (default value if the parameter is omitted or is not a boolean) |