gianlucagiacometti/bootstrap-select

Extension for the Boostrap select element

Installs: 3

Dependents: 0

Suggesters: 0

Security: 0

Stars: 4

Watchers: 1

Forks: 0

Open Issues: 0

Language:JavaScript

Type:bootstrap-extension

1.5.2 2023-01-01 16:43 UTC

This package is auto-updated.

Last update: 2024-04-18 16:35:43 UTC


README

Extension class for the Bootstrap (https://getbootstrap.com) Select element

This program is written in plain JavaScript and is designed for Bootstrap 5.2.x 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 already recursive.

The bsSelect class uses the Input and the Dropdown elements of Bootstrap, for a maximum level of compatibility.

This program is released under the MIT licence.

Requirements

Bootstrap >= 5.2.2

Boostrap Icons >= 1.9 (used only by search and option images)

NOTE: Version numbers will follow the version numbers of Bootstrap releases (i.e. 1.5.2.2.1 - Bootstrap release version in bold)

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 authomatic way to create a new bsSelect class element.

You may:

  1. Add to the <select> element the class "bootstrap-select".

     <select id="myselect" class="bootstrap-select">...</select>
    
  2. 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")
    
  3. 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 is an optional object. 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 <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 name of the class which will be added to the <i> element of the icon.
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

Methods

Name Description Parameters Example
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")
{ mode: "asc" } => Sorts the option texts from A to Z.
{ mode: "desc" } => Sorts the option texts from Z to A.
{ disabled: "ignore" } => Sorts the option texts not including the disabled options.
{ disabled: "top" } => Moves all the disabled options to the top of the list.
{ disabled: "bottom" } => Moves all the disabled options to the bottom of the list.
{ disabled: "include" } => Includes all the disabled options in the sorting.
{ sortDisabled: "true" } => Sorts the disabled options according to the mode parameter (this parameter may be set ONLY if the parameter disabled is set to "top" or "bottom").
{ sortDisabled: "false" } => Does not sort the disabled options (this parameter may be set ONLY if the parameter disabled is set to "top" or "bottom").
{ emptyDisabledValues: "top" } => Moves all disabled options with empty value ("") to the top of the list.
{ emptyDisabledValues: "include" } => Includes all disabled options with empty value ("") in the sorting (this value of the parameter may be set ONLY if the parameter disabled is set to "include").
{ optionGroups: "include" } => Includes the option groups in the sorting, according to the parameter mode and to the text of their attribute label.
{ optionGroups: "ignore" } => Keeps the position of the option groups fixed in the list and sorts the groups of options in between separately.
insert(options, parent) Inserts a single <option> element, an array of <option> elements, an empty <optgroup> element or an array of empty <optgroup> elements. Acceptable options are both html strings or JavaScript DOM elements and their combinations:
1. '<option value"myvalue">My text</option>'
2. ['<option value="anothervalue">Another text</option>', '<option value="" disabled>Disabled value</option>']
3. option (where option is an element created by: option = document.createElement("option"); option.value = "2"; option.text = "New option")
4. [option, otherdomoptionelement]
5. [option, '<optgroup id="my-group-id" label="My group"></optgroup>']

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-dgit index of an <optgroup> element" => The options will be inserted as children of that <optgroup>.
remove(parameters) Remove one or more <option> or <optgroup> elements according to the specified parameters. value: single value
value: array of values
id: single element id
id: array of element ids
index: a single 16-digit random index of the element
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)

Further developments

  1. Update ARIA roles and attributes