简体   繁体   中英

vuejs select option change based on conditon

I try to get the user input value from a form input. Then the select option will disable certain selection based on the user input value. How can I achieve this feature in vuejs ? I am using bootstrap-vue framework. For example:

Form input:(user will type random animal name here)

<b-form-input type="text" v-model="animal_name"></b-form-input>

Form Select:(for this example I assume user will type in fish)

<b-form-select class="mb-2 mr-sm-2 mb-sm-0"
                           v-model="selectedAnimalType"
                           :options="animalType"
                         >
                        </b-form-select>

When User type in first, it will automatically disable the option which does not belong to fish.

How can I achieve this function using vuejs ?

Use a computed property and filter the select options:

computed: {
  filteredAnimalType () {
    if (this.animal_name) {
      // if animalType is a string array
      return this.animalType.filter(t => t.includes(this.animal_name))
      // otherwise filter on the desired property, e.g. t.taxonomy
    }
    return this.animalType
  }
}

Then bind the options as :options="filteredAnimalType"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM