Search code examples
vue.jsvuexquasarv-select

Connect v-select with vuex: problem [object object]


I am trying to create a dropdown (v-select/q-select (using quasar)), which allows me to select from an array in my vuex-storage and then eventually save the selected item (content of it) in a variable. Currently I have no problem to access the vuex-storage, but face the problem, that the v-select expects a string and not an object.

My code looks like the following.

// vuex storage:

const state = {
  savedsystems: 
     [
      id: "1",
      system: {...}
     ],
     [
      id: "2",
      system: {...}
     ]

 // example of the vuex storage out of my viewdevtools
 systemsconstant: Object
   savedsystems:Array[2]
     0:Object
       id:"first"
       system:Object
         7a73d702-fc28-4d15-a54c-2bb950f7a51c:Object
           name:"3"
           status:"defined"

         88519419-8a81-48f1-a5e6-5da77291b848:Object
           name:"5"
           status:"not defined"
     1:Object
       id:"second"
       system:Object
         7a73d702-fc28-4d15-a54c-2bb950f7a51c:Object
           name:"3"
           status:"not defined"

         88519419-8a81-48f1-a5e6-5da77291b848:Object
           name:"9"
           status:"defined"
}


// dropdown:

    <q-select 
      outlined 
      dense 
      emit-value
      :value="currentsystem"
      :options="savedsystems" 
      label="selectsystem" />


// computed to get systems from vuex:

computed: {
    savedsystems() {
      return this.$store.getters['systemsconstant/getsavedsystems'] 
    }
  },

I used the following example https://codepen.io/sagalbot/pen/aJQJyp as inspiration and tried a couple of different setups stringifying resulting in nothing really.

If one would try to apply my case to a similar problem (v-select displays object Object), the mentioned formatlabel would be an object instead of a string.

Question: How can I modify the (with a getter) imported array of objects "savedsystems", so it can be used both as label to select it and furthermore then to connect it properly to the values, so I can save the selected as a variable. Or can I change something in my v-select, e.g. varying what comes behind :options/options?

I'd appreciate any help!


Solution

  • You should use the property option-label

        <div id="q-app">
          <div class="q-pa-md" style="max-width: 300px">
            <div class="q-gutter-md">
              <q-badge color="secondary" multi-line>
                Model: "{{ model }}"
              </q-badge>
        
              <q-select filled v-model="model" :options="options" label="Standard" option-label="description"></q-select>
              
              {{ model }}
            </div>
          </div>
        </div>
    

    JS:

    new Vue({
      el: '#q-app',
      data () {
        return {
          model: null,
          options: [
            {
              label: 'Google',
              value: 'Google',
              description: 'Search engine',
              category: '1'
            },
            {
              label: 'Facebook',
              value: 'Facebook',
              description: 'Social media',
              category: '1'
            },
            {
              label: 'Twitter',
              value: 'Twitter',
              description: 'Quick updates',
              category: '2'
            },
          ]
        }
      }
    })
    

    https://codepen.io/reijnemans/pen/bGpqJYx?editors=1010