Select
Angular Bootstrap 5 Select component
Select fields components are used for collecting user provided information from a list of options.
Basic example
Basic example of select component that allows you to choose from a number of options.
Multiselect
Add [multiple]="true"
input to the select element to activate multiple mode.
Select with label
It is possible to add select label by creating element with
mdbLabel
directive and .form-label
class.
Select with placeholder
Use placeholder
option to set placeholder for select input. The placeholder will
be displayed when input is focused and no option is selected.
Disabled select
Add [disabled]="true"
input to the select element to disable the component.
Disabled options
Use disabled
input on option element to disable specific option.
Custom content
A custom content container with a class .select-custom-content
will be displayed
at the end of the select dropdown.
Visible options
Use visibleOptions
input to change the number of options that will be displayed
in the select dropdown without scrolling.
Secondary text
Add container with class .option-secondary-text
to the specific options to
display secondary text.
Search
Set filter
input to true
to enable options filtering.
Option groups
It is possible to group options by using mdb-option-group
component.
Select with icons
Add conatiner with option-icon-container
class to the specific option to display
option icon.
Validation
Use mdbValidate
directive to enable validation styles and
mdb-error
and mdb-success
components to display validation messages.
Note:
This example uses MDB Angular Validation. Remember to import MdbValidationModule
and ReactiveFormsModule
to enable reactive forms validation in Angular.
Set value
It is possible to set default value using Angular reactive or template driven forms methods.
Reactive forms
Create new formControl
with a value (or array of values in multiselect) to
select option with corresponding value.
Template driven forms
Add a value (or array of values in multiselect) to the ngModel
to select option
with corresponding value.
Select - API
Import
Inputs
Name | Type | Default | Description |
---|---|---|---|
autoSelect
|
boolean | false |
Enables auto selecting on Tab press. |
clearButton
|
boolean | false |
Adds clear button to the select input. |
clearButtonTabindex
|
number | 0 |
Enables setting a custom tabIndex for the clear button. |
disabled
|
boolean | false |
Changes select input state to disabled. |
displayedLabels
|
number | 5 |
The maximum number of comma-separated list of options displayed on the Multiselect. If a user selects more options than that value, the X options selected text will be displayed instead. If set to -1, the limit is off. |
dropdownClass
|
string | '' |
Enables adding custom classes to the select dropdown when it is shown. Note that these classes will be added in
addition to any classes specified in the template. To add multiple classes, separate them with spaces:
'class-1 class-2' .
|
dropdownHeight
|
number | visibleOptions * optionHeight |
Enables setting a specific select dropdown height in pixels. |
filter
|
boolean | false |
Displays filter input that allow to search for specific option. |
filterDebounce
|
number | 300 |
Adds delay to the options list updates when using filter input. Improves performance of the select with filter. |
filterFn
|
(option: string, filterValue: string) => boolean | (option, filterValue) => option.includes(filterValue) |
Defines filtering function. |
filterPlaceholder
|
string | 'Search...' |
Changes select search input placeholder. |
highlightFirst
|
boolean | true |
Enables highlight on first option. |
inputFilterId
|
string | '' |
Declares id attribute for filter input element. Helps with accessibility issues. |
inputId
|
string | '' |
Declares id attribute for input element. Helps with accessibility issues. |
multiple
|
boolean | false |
Enables selecting multiple options. |
notFoundMsg
|
string | 'No results found' |
Defines custom not found message for select search box. |
optionHeight
|
number | 38 |
Changes the single option height value. |
placeholder
|
string | '' |
Defines placeholder for select input element. |
required
|
boolean | false |
Enables marking the select field as required. |
size
|
'default' | 'lg' | 'sm' | 'default' |
Defines input's size. Available options: 'sm', 'lg', 'default'. |
tabindex
|
number | 0 |
Defines the tabindex attribute value for select input element. |
visibleOptions
|
number | 5 |
Changes number of visible options in options list. |
Outputs
Name | Type | Description |
---|---|---|
closed
|
EventEmitter<void> | Event fired when the select is closed. |
opened
|
EventEmitter<void> | Event fired when the select is opened. |
valueChange
|
EventEmitter<any> | Event fired when the select value is changed. It returns single value for single select and an array for multiple select. |
selected
|
EventEmitter<MdbOptionComponent> | Event fired when a value is selected in the select field. It returns only the values selected during the event (unlike valueChange event, which returns all selected values). The event can return a single value or an array if multiple values are selected simultaneously. |
deselected
|
EventEmitter<MdbOptionComponent | MdbOptionComponent[]> | Event fired when a value is deselected in the select field. It returns only the values deselected during the event. The event can return a single value or an array if multiple values are deselected simultaneously. |
Methods
Name | Description | Example |
---|---|---|
close
|
Manually close the component |
select.close()
|
open
|
Manually open the component |
select.open()
|
CSS variables
As part of MDB’s evolving CSS variables approach, select now use local CSS variables for enhanced real-time customization. Values for the CSS variables are set via Sass, so Sass customization is still supported, too.
Select CSS variables are in different classes which belong to this component. To make it easier to use them, you can find below all of the used CSS variables.