Autocomplete

Vue 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


Basic example

The filter property is required in order for component to work properly. The property accepts a function that is expected to return an array of results or a Promise that resolves to an array of results.

        
            
            <template>
              <MDBAutocomplete
                v-model="autocompleteBasic"
                :filter="filterBasic"
                label="Example label"
              />
            </template>
          
        
    
        
            
            <script>
              import { MDBAutocomplete } from "mdb-vue-ui-kit";
              import { ref } from "vue";
              export default {
                components: {
                  MDBAutocomplete
                },
                setup() {
                  const autocompleteBasic = ref("");
                  const optionsBasic = ref([
                    "One",
                    "Two",
                    "Three",
                    "Four",
                    "Five",
                    "Six",
                    "Seven",
                    "Eight"
                  ]);
                  const filterBasic = value => {
                    return optionsBasic.value.filter(item => {
                      return item.toLowerCase().startsWith(value.toLowerCase());
                    });
                  };
                  return {
                    autocompleteBasic,
                    filterBasic
                  };
                }
              };
            </script>
          
        
    

Display value

The displayValue property allow to separate original 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.

        
            
            <template>
              <MDBAutocomplete
                v-model="autocompleteDisplay"
                :filter="filterDisplay"
                :displayValue="displayValueDisplay"
                label="Example label"
              />
            </template>
          
        
    
        
            
            <script>
              import { MDBAutocomplete } from "mdb-vue-ui-kit";
              import { ref } from "vue";
              export default {
                components: {
                  MDBAutocomplete
                },
                setup() {
                  const autocompleteDisplay = ref("");
                  const optionsDisplay = ref([
                    { title: "One", subtitle: "Secondary text" },
                    { title: "Two", subtitle: "Secondary text" },
                    { title: "Three", subtitle: "Secondary text" },
                    { title: "Four", subtitle: "Secondary text" },
                    { title: "Five", subtitle: "Secondary text" },
                    { title: "Six", subtitle: "Secondary text" }
                  ]);
                  const filterDisplay = value => {
                    return optionsDisplay.value.filter(item => {
                      return item.title.toLowerCase().startsWith(value.toLowerCase());
                    });
                  };
                  const displayValueDisplay = value => value.title;
                  return {
                    autocompleteDisplay,
                    filterDisplay,
                    displayValueDisplay
                  };
                }
              };
            </script>
          
        
    


Threshold

Use threshold property to specify a minimum number of the characters in the input field needed to perform a search operation.

        
            
            <template>
              <MDBAutocomplete
                v-model="autocompleteThreshold"
                :filter="filterThreshold"
                :threshold="2"
                label="Example label"
                placeholder="Type 2 characters to search"
              />
            </template>
          
        
    
        
            
            <script>
              import { MDBAutocomplete } from "mdb-vue-ui-kit";
              import { ref } from "vue";
              export default {
                components: {
                  MDBAutocomplete
                },
                setup() {
                  const autocompleteThreshold = ref("");
                  const optionsThreshold = ref([
                    "One",
                    "Two",
                    "Three",
                    "Four",
                    "Five",
                    "Six",
                    "Seven",
                    "Eight"
                  ]);
                  const filterThreshold = value => {
                    return optionsThreshold.value.filter(item => {
                      return item.toLowerCase().startsWith(value.toLowerCase());
                    });
                  };
                  return {
                    autocompleteThreshold,
                    filterThreshold
                  };
                }
              };
            </script>
          
        
    

Custom item template

The itemContent property allow to display custom HTML in the result list. You can use the optionHeight and visibleOptions properties to modify the result list height when you want to display more content in the component dropdown.

        
            
            <template>
              <MDBAutocomplete
                v-model="autocompleteTemplate"
                :filter="filterTemplate"
                :displayValue="displayValueTemplate"
                :itemContent="itemTemplate"
                label="Example label"
              />
            </template>
          
        
    
        
            
            <script>
              import { MDBAutocomplete } from "mdb-vue-ui-kit";
              import { ref } from "vue";
              export default {
                components: {
                  MDBAutocomplete
                },
                setup() {
                  const autocompleteTemplate = ref("");
                  const optionsTemplate = ref([
                    { title: "One", subtitle: "Secondary text" },
                    { title: "Two", subtitle: "Secondary text" },
                    { title: "Three", subtitle: "Secondary text" },
                    { title: "Four", subtitle: "Secondary text" },
                    { title: "Five", subtitle: "Secondary text" },
                    { title: "Six", subtitle: "Secondary text" }
                  ]);
                  const itemTemplate = result => {
                    return `
                      <div class="autocomplete-custom-item-content">
                        <div class="autocomplete-custom-item-title">${result.title}</div>
                        <div class="autocomplete-custom-item-subtitle">${result.subtitle}</div>
                      </div>
                    `;
                  };
                  const filterTemplate = value => {
                    return optionsTemplate.value.filter(item => {
                      return item.title.toLowerCase().startsWith(value.toLowerCase());
                    });
                  };
                  const displayValueTemplate = value => value.title;
                  return {
                    autocompleteTemplate,
                    filterTemplate,
                    itemTemplate,
                    displayValueTemplate
                  };
                }
              };
            </script>
          
        
    

Custom content

Pass any content to a default slot to display a custom content container with a class .autocomplete-custom-content at the end of the items list. You can use it to display a number of search results.

        
            
            <template>
              <MDBAutocomplete
                v-model="autocompleteContent"
                :filter="filterContent"
                @update="setResultsLength"
                label="Example label"
              >
                Search results: {{ contentResultsLength }}
              </MDBAutocomplete>
            </template>
          
        
    
        
            
            <script>
              import { MDBAutocomplete } from "mdb-vue-ui-kit";
              import { ref } from "vue";
              export default {
                components: {
                  MDBAutocomplete
                },
                setup() {
                  const autocompleteContent = ref("");
                  const optionsContent = ref([
                    "One",
                    "Two",
                    "Three",
                    "Four",
                    "Five",
                    "Six",
                    "Seven",
                    "Eight"
                  ]);
                  const contentResultsLength = ref(0);
                  const setResultsLength = e => {
                    contentResultsLength.value = e.length;
                  };
                  const filterContent = value => {
                    return optionsContent.value.filter(item => {
                      return item.toLowerCase().startsWith(value.toLowerCase());
                    });
                  };
                  return {
                    autocompleteContent,
                    contentResultsLength,
                    setResultsLength,
                    filterContent
                  };
                }
              };
            </script>
          
        
    

Validation

Looks good!
Input value is required
        
            
            <template>
              <form
                class="needs-validation"
                novalidate
                @submit.prevent="checkForm"
              >
                <div id="validation" class="form-outline">
                  <MDBAutocomplete
                    v-model="autocompleteValidation"
                    :filter="filterValidation"
                    validFeedback="Looks good!"
                    invalidFeedback="Input value is required!"
                    required
                    label="Example label"
                  />
                </div>
                <MDBBtn color="primary" size="sm" class="mt-3" type="submit">
                  Submit
                </MDBBtn>
              </form>
            </template>
          
        
    
        
            
            <script>
              import { MDBAutocomplete, MDBBtn } from "mdb-vue-ui-kit";
              import { ref } from "vue";
              export default {
                components: {
                  MDBAutocomplete, MDBBtn
                },
                setup() {
                  const autocompleteValidation = ref("");
                  const optionsValidation = ref([
                    "One",
                    "Two",
                    "Three",
                    "Four",
                    "Five",
                    "Six",
                    "Seven",
                    "Eight"
                  ]);
                  const filterValidation = value => {
                    return optionsValidation.value.filter(item => {
                      return item.toLowerCase().startsWith(value.toLowerCase());
                    });
                  };
                  const checkForm = e => {
                    e.target.classList.add("was-validated");
                  };
                  return {
                    autocompleteValidation,
                    filterValidation,
                    checkForm
                  };
                }
              };
            </script>
          
        
    

Auto select

Set autoSelect option to true to enable selecting on Tab press.

        
            
                <template>
                  <MDBAutocomplete v-model="autocompleteBasic" :filter="filterBasic" label="Example label" autoSelect />
                </template>
                
        
    
        
            
                    <script>
                      import { MDBAutocomplete } from "mdb-vue-ui-kit";
                      import { ref } from "vue";
                      export default {
                        components: {
                          MDBAutocomplete
                        },
                        setup() {
                          const autocompleteBasic = ref("");
                          const optionsBasic = ref([
                            "One",
                            "Two",
                            "Three",
                            "Four",
                            "Five",
                          ]);
                          const filterBasic = value => {
                            return optionsBasic.value.filter(item => {
                              return item.toLowerCase().startsWith(value.toLowerCase());
                            });
                          };
                          return {
                            autocompleteBasic,
                            filterBasic
                          };
                        }
                      };
                    </script>
                    
        
    

Autocomplete - API


Import

        
            
          <script>
            import { MDBAutocomplete } from "mdb-vue-ui-kit";
          </script>
        
        
    

Properties

Property Type Default Description
autoSelect Boolean false Enables auto selecting on Tab press
filter Function - Function that returns filtered data to the component
displayValue Function (value) => value Function executed for complex search results, to get value to display in the results list
filter Function - Function that returns filtered data to the component
itemContent Function - Function that returns custom template for result item
listHeight Number 190 Height of the result list
noResultsText String 'No results found' Message that will be displayed in the component dropdown if no result is found
threshold Number 300 Minimum input length to start search query
optionHeight Number 38 Set height for single item
visibleOptions Number 5 Set count of options visible without scroll

Methods

Name Description Example
open Manually opens a component dropdown $refs.autocompleteRef.open()
close Manually closes a component dropdown $refs.autocompleteRef.close()
toggle Manually toggles a component dropdown $refs.autocompleteRef.toggle()

Slots

Name Description
default Slot for custom list content

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.
        
            
          <template>
            <MDBSelect v-model:options="options1" @clear="handleClear" />
          </tem>