Search code examples
vue.jsvuexvuetify.js

How to load Array Data into Vuetify Select Input


I'm trying to show "locations" in a vuetify select component, but my current code renders "[object Object]" instead of Location 1, Location 2, etc.

My select component:

<v-select
:items="locations"
v-model="location"
label="Choose Location"
bottom
autocomplete
></v-select>

Locations:

locations () {
  return this.$store.getters.getLocationsForEvent(this.event.id)
}

Vuex Getter:

getLocationsForEvent: (state) => (id) => {
  return state.loadedLocations.filter(function (location) { 
    return location.eventId === id; 
  });
}

Here is a screenshot of what the location data looks like:

enter image description here

Thanks!


Solution

  • For custom objects you have to specify the item-text. The item-text is what each option will display.

    From your screenshot, for instance, title is a possible property:

    <v-select
    :items="locations"
    v-model="location"
    label="Choose Location"
    item-text="title"
    bottom
    autocomplete
    ></v-select>
    

    Demos below.

    Without item-text:

    new Vue({
      el: '#app',
      data () {
        return {
          location: null,
          locations: [
            { id: "1111", manager: 'Alice',  title: 'Water Cart 1' },
            { id: "2222", manager: 'Bob',    title: 'Water Cart 2' },
            { id: "3333", manager: 'Neysa',  title: 'Water Cart 3' }
          ]
        }
      }
    })
    <link rel='stylesheet' href='https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons'>
    <link rel='stylesheet' href='https://unpkg.com/[email protected]/dist/vuetify.min.css'>
    <script src='https://unpkg.com/vue/dist/vue.js'></script>
    <script src='https://unpkg.com/[email protected]/dist/vuetify.min.js'></script>
    
    <div id="app">
      <v-app>
        <v-container>
          <v-select
            :items="locations"
            v-model="location"
            label="Choose Location"
            bottom
            autocomplete
            >
          </v-select>
        </v-container>
      </v-app>
    </div>

    With item-text:

    new Vue({
      el: '#app',
      data () {
        return {
          location: null,
          locations: [
            { id: "1111", manager: 'Alice',  title: 'Water Cart 1' },
            { id: "2222", manager: 'Bob',    title: 'Water Cart 2' },
            { id: "3333", manager: 'Neysa',  title: 'Water Cart 3' }
          ]
        }
      }
    })
    <link rel='stylesheet' href='https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons'>
    <link rel='stylesheet' href='https://unpkg.com/[email protected]/dist/vuetify.min.css'>
    <script src='https://unpkg.com/vue/dist/vue.js'></script>
    <script src='https://unpkg.com/[email protected]/dist/vuetify.min.js'></script>
    
    <div id="app">
      <v-app>
        <v-container>
          <v-select
            :items="locations"
            v-model="location"
            label="Choose Location"
            item-text="title"
            bottom
            autocomplete
            >
          </v-select>
        </v-container>
      </v-app>
    </div>