Autocomplete
Bootstrap 5 Autocomplete component
Autocomplete component predicts the words being typed based on the first few letters given by the user. You can search the list using basic scroll and the keyboard arrows
Note: Read the API tab to find all available options and advanced customization
*
*
UMD autoinits are enabled
by default. This means that you don't need to initialize
the component manually. However if you are using MDBootstrap ES format then you should pass
the required components to the initMDB
method.
Basic example
The filter
option is required in order for component to work properly. The option
accepts a function that is expected to return an array of results or a
Promise
that resolves to an array of results.
Display value
The displayValue
option allow to separate oryginal result value from the value
that will be displayed in the result list or input (after selection). Its useful when the data
returned by the filter
function is an array of objects. You can specify which
parameter of the object should be displayed.
Asynchronous search
The function passed to the filter
option can return a Promise
that
resolves to an array of results. By default the component expects to receive data as an array
of strings. If you want to return an array of objects instead, you can use
displayValue
option to specify which parameter should be used as a display value
of the specific result.
Threshold
Use threshold
option to specify a minimum number of the characters in the input
field needed to perform a search operation.
Custom item template
The itemContent
option allow to display custom HTML in the result list. You can
use the listHeight
option to modify the result list height when you want to
display more content in the component dropdown.
Custom content
A custom content container with a class .autocomplete-custom-content
will be displayed at the
end of the autocomplete dropdown. You can use it to display a number of
search results.
Validation
The input value is automatically validated to ensure that it is a properly formatted email address.
Auto select
Set autoSelect
option to true
to enable selecting on Tab press.
Autocomplete - API
Import
Importing components depends on how your application works. If you intend to use the MDBootstrap ES
format, you must
first import the component and then initialize it with the initMDB
method. If you are going to use the UMD
format,
just import the mdb-ui-kit
package.
Usage
Via JavaScript
Via jQuery
Note: By default, MDB does not include jQuery and you have to add it to the project on your own.
Options
Options can be passed via data attributes or JavaScript. For data attributes, append the option name to
data-mdb-
, as in data-mdb-container="body"
.
Name | Type | Default | Description |
---|---|---|---|
autoSelect
|
Boolean | false |
Enables auto selecting on Tab press |
container |
String | 'body' |
Allows to set the parent element for the autocomplete. |
customContent
|
String | '' |
Custom HTML template that will be displayed at the end of the component dropdown |
debounce
|
Number | 300 |
Delay between search queries in milliseconds, improves the component performance |
displayValue
|
Function | null | (value) => value |
Function executed for complex search results, to get value to display in the results list |
filter |
Function | null |
Function that returns filtered data to the component |
itemContent
|
Function | null | null |
Function that returns custom template for result item |
listHeight
|
Number | 190 |
Height of the result list |
noResults
|
String | 'No results found' |
Message that will be displayed in the component dropdown if no result is found |
threshold
|
Number | 0 |
Minimum input length to start search query |
Methods
Name | Description | Example |
---|---|---|
open |
Manually opens a component dropdown |
myAutocomplete.open()
|
close |
Manually closes a component dropdown |
myAutocomplete.close()
|
dispose
|
Disposes an autocomplete instance |
myAutocomplete.dispose()
|
getInstance
|
Static method which allows you to get the autocomplete instance associated to a DOM element. |
Autocomplete.getInstance(myAutocomplete)
|
getOrCreateInstance
|
Static method which returns the autocomplete instance associated to a DOM element or create a new one in case it wasn't initialized. |
Autocomplete.getOrCreateInstance(myAutocomplete)
|
search
|
Initialize search results for a specific value |
myAutocomplete.search('')
|
Events
Name | Description |
---|---|
open.mdb.autocomplete
|
This event fires immediately when the autocomplete dropdown is opened. |
close.mdb.autocomplete
|
This event fires immediately when the autocomplete dropdown is closed. |
itemSelect.mdb.autocomplete
|
This event fires immediately when the autocomplete item is selected. |
update.mdb.autocomplete
|
This event fires immediately when the autocomplete results list is updated. |