Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set v-select value dynamically?

My Vuetify v-select element looks this way:

<v-select
   v-bind:items="languages"
   v-model="setLocale"
   label="Language:"
   auto prepend-icon="map"
   item-value="fetchedLocale"
   hide-details
   id="langSelect"
   >

In data you can find:

data () {
  return {
    languages: [
      { shortCode: 'en', text: 'English' },
      { shortCode: 'pl', text: 'Polski' },
      { shortCode: 'es', text: 'Español' },
      { shortCode: 'pt', text: 'Portugues' }
    ],
    fetchedLocale: '',
    setLocale: null
  }
}, (...)

After some processing, fetchedLocale gets value of some of text properties from the array above, e.g. "Portugues".

Question: how to update the v-select so that it sets fetchedLocale's value, like mentioned before "Portugues", when loading DOM elements, instead of setting default empty value?

like image 695
AbreQueVoy Avatar asked Sep 22 '17 16:09

AbreQueVoy


People also ask

What is V-model in Vue-select?

vue-select takes advantage of the v-model syntax to sync values with a parent. The v-model syntax works with primitives and objects. <v-select v-model="selected" />. Note that when using the multiple prop, the v-model value will always be an array.

Is it possible to set value directly from the controller?

But if you are using a dynamic list and Key Value pair with an iteration to set the values, in that case setting value directly will not work. We have to do some extra efforts with setting the value from controller.

How to set selected value in Vue select?

You can bind the selected value with :value="$store.myValue", and use the input event to trigger a mutation, or dispatch an action – or anything else you might need to do when the selection changes. methods: { setSelected(value) { // trigger a mutation, or dispatch an action } } By default, vue-select supports choosing a single value.

How do I pass an array of variables to a V-select?

If you're using a multiple v-select, you'll want to pass an array. variable in your code using the @input event. The input event is triggered anytime the value state changes, and is emitted with the value state as it's only parameter. The value prop and emit event are very useful when using a state management tool, like Vuex.


1 Answers

As per the documentation, the item-value prop defines the property name to use as the value for each item. The default for this prop is 'value', meaning the value property of each item will be used as each item's value by default. If you set it to text, for example, then the text property of each of your languages will be used as the value of that item. However, this won't actually set the value of the select component.

You've already bound the value of the select component to setLocale via v-model. So if you want to change the select component's value to the fetchedLocale value, just update setLocale with the value of fetchedLocale and the component will update:

this.setLocale = this.fetchedLocale

Here's a working example:

new Vue({
  el: '#app',
  data() {
    return {
      languages: [
        { shortCode: 'en', text: 'English' },
        { shortCode: 'pl', text: 'Polski' },
        { shortCode: 'es', text: 'Español' },
        { shortCode: 'pt', text: 'Portugues' }
      ],
      fetchedLocale: '',
      setLocale: null
    };
  },
  created() {
    setTimeout(() => {
      this.fetchedLocale = 'English';
      this.setLocale = this.fetchedLocale;
    }, 1000);
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.4/vue.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/vuetify.js"></script>
<link href="https://unpkg.com/[email protected]/dist/vuetify.min.css" rel="stylesheet"/>
<link href='https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons' rel="stylesheet" type="text/css">

<div id="app">
  <v-app>
    <v-select
      :items="languages"
      v-model="setLocale"    
      label="Language:"
      auto prepend-icon="map"
      item-value="text"
      hide-details
      id="langSelect"
    ></v-select>
  </v-app>
</div>
like image 160
thanksd Avatar answered Sep 24 '22 03:09

thanksd