1

I have a set of data which has a city name and a value attributed to the city. When someone selects the city it should show the value. I know that I can get the selection by having {{selected.value}}. However, as I want to extract the value to later use it in a calculation (i.e. sum for example 30 to it), I try to calculate it using a computed value. My problem is that I can't seem to return the value or a part of the array. Whenever I have return this.selected I can see the entire selected array, but when I have return this.selected.value or return.this.selected.name I get nothing.

I'm quite new to vuejs so I'm not sure what am I missing.

    <template>
    <v-container fluid>
        <v-layout wrap align_center>
          <v-flex xs12 sm6>
            <v-select
              v-model="selected"
              :items="items"
              item-text= "city"
              item-value="value"
              :menu-props="{ maxHeight: '400'}"
              label="Select"
              hint="Pick your favorite states"
              multiple
              persistent-hint
              return-object
              single-line
            ></v-select>
          </v-flex>
          <v-flex>
                  {{getselection}}

          </v-flex>
        </v-layout>
        </v-container>

    </template>

    <script>

    export default {

        data () {
          return {
            text:'',
          selected: [],
          newsel: [],
          items:[
    {city: 'NY', value: 32},
    {city: 'Gent', value: 80},
    {city: 'Coimbra', value: 41}
     ]}
        },
  computed:{
    getselection(){
    return this.selected.value
    }
      },
      }
    </script>
2
  • You want only select one city ? Commented Sep 2, 2019 at 14:11
  • @BoussadjraBrahim for now it doesn't really matter. What matters is that what's returned isn't the full array but either just city or just value. Commented Sep 2, 2019 at 14:12

1 Answer 1

2

Try to remove multiple prop from v-select component in order to return one item:

 <v-flex xs12 sm6>
            <v-select
              v-model="selected"
              :items="items"
              item-text= "city"
              item-value="value"
              :menu-props="{ maxHeight: '400'}"
              label="Select"
              hint="Pick your favorite states"
             
              persistent-hint
              return-object
              single-line
            ></v-select>
See the Pen Vuetify Example Pen by boussadjra (@boussadjra) on CodePen.

If you want to keep multiple prop the computed property could be like :

    getselection(){
    return !!this.selected.length ?this.selected[0].value:0;
    }
Sign up to request clarification or add additional context in comments.

9 Comments

This works indeed, but how would it work for multiple selections?
but that returns multiple cities, you should access array via indexes or use one of its methods in order to make some logic
I'm sorry but I don't understand your answer. If you remove the multiple prop from the v-select, you can't select then multiple options in the dropdown list.
if you keep multiple you should do something like return this.selected[0].value
That doesn't work. Actually even if you try it on your CodePen, the dropdown list disappears.
|

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.