Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear v-autocomplete (multiple) search input after selecting the checkbox in the drop-down list?

I have multiple v-autocomplete component from Vuetify. Currently, for example if enter in search input "Cali" and check "California" value, "Cali" value still exist in search input. I need to clear entered value. For current, search value cleared when closing dropdown list, but I need clear it when I select checkbox.

<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
        multiple
      ></v-autocomplete>
new Vue({
  el: '#app',
  data () {
    return {
      loading: false,
      items: [],
      search: null,
      select: null,
      states: [
        'Alabama',
        'Alaska',
        'American Samoa',
        'Arizona',
        'Arkansas',
        'California',
        'Colorado',
        'Connecticut',
        'Delaware',
        'District of Columbia',
        'Federated States of Micronesia',
        'Florida',
        'Georgia',
        'Guam',
        'Hawaii',
        'Idaho',
        'Illinois',
        'Indiana',
        'Iowa',
        'Kansas',
        'Kentucky',
        'Louisiana',
        'Maine',
        'Marshall Islands',
        'Maryland',
        'Massachusetts',
        'Michigan',
        'Minnesota',
        'Mississippi',
        'Missouri',
        'Montana',
        'Nebraska',
        'Nevada',
        'New Hampshire',
        'New Jersey',
        'New Mexico',
        'New York',
        'North Carolina',
        'North Dakota',
        'Northern Mariana Islands',
        'Ohio',
        'Oklahoma',
        'Oregon',
        'Palau',
        'Pennsylvania',
        'Puerto Rico',
        'Rhode Island',
        'South Carolina',
        'South Dakota',
        'Tennessee',
        'Texas',
        'Utah',
        'Vermont',
        'Virgin Island',
        'Virginia',
        'Washington',
        'West Virginia',
        'Wisconsin',
        'Wyoming'
      ]
    }
  },
  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)
    }
  }
})

I want to clear value in search input after selecting value in dropdown list. Here is codepen example - https://codepen.io/anon/pen/ZZMKeL

like image 264
ismakv Avatar asked Apr 23 '19 09:04

ismakv


1 Answers

Add watcher for select value and reset search-input:

watch: {
  select() {
    this.search = ''
  }

Or react on one of the events:
<v-autocomplete @input="search = ''"
or
<v-autocomplete @change="search = ''"


Watch select because of v-model value (<v-autocomplete v-model="select"), and change
this.search because of search-input.sync value (:search-input.sync="search").
like image 152
Traxo Avatar answered Oct 01 '22 21:10

Traxo