7

I'm using Vuetify autocomplete component and I want it to display a default value in its input field

I tried to pass just value as prop and change v-model field to any string but it doesn't work - an input field is empty unless I choose value form the list

Vuetify official example

<v-autocomplete
        v-model="select"
        :loading="loading"
        :items="items"
        :search-input.sync="search"
        cache-items
        class="mx-3"
        flat
        hide-no-data
        hide-details
        label="What state are you from?"
        solo-inverted
      ></v-autocomplete>

new Vue({
  el: '#app',
  data () {
    return {
      loading: false,
      items: [],
      search: null,
      select: 'Alabama',
      states: [
        'Alabama',
        'Alaska',
        'American Samoa',
        'Arizona',
        'Arkansas',
        'California',
        'Colorado',
      ]
    }
  },
  watch: {
    search (val) {
      val && val !== this.select && this.querySelections(val)
    }
  },
  methods: {
    querySelections (v) {
      this.loading = true
      // Simulated ajax query
      setTimeout(() => {
        this.items = this.states.filter(e => {
          return (e || '').toLowerCase().indexOf((v || '').toLowerCase()) > -1
        })
        this.loading = false
      }, 500)
    }
  }
})
4

4 Answers 4

12

I ran into this and what I did was passing to the v-model, the element at the 0 index position from the list:

:items="items"
v-model="items[0]"
Sign up to request clarification or add additional context in comments.

Comments

11

The v-autocomplete component will not display "Alabama" since "Alabama" is not found in items. All you need to do is add "Alabama" to items to make it initially valid:

data () {
    return {
      loading: false,
      items: ['Alabama'], // <- add the default here
      search: null,
      select: 'Alabama',
      states: [ ...

1 Comment

This works, but there's no reason to be "forced" to add such an item to the item list
5

you can also use auto-select-first attribute along with default value in data.

   <v-autocomplete
          v-model="selectedItem"
          auto-select-first
          :items="items">
   </v-autocomplete>

  data() {
    return {
      items: ['Default'],
      selectedItem: "Default",
    }
}

Comments

2

I ran into the same problem, And finally figured it out Use item-text and item-value to show the items And init the v-model with the key Of the item you want to display

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.