vue-select icon indicating copy to clipboard operation
vue-select copied to clipboard

Selected option setting id value

Open optimalui opened this issue 3 years ago • 10 comments

If there is a default value for vue-select it sets id value. it doesnt set label.

                                       <v-select 
                                            :options="agentList" 
                                            label="full_name" 
                                            v-model="$v.agent_id.$model">
                                           >
                                        </v-select>

image When I inspect from vue dev tools selectedValue appears as 1.

I expect to see full_name at first but it shows id value.

optimalui avatar Mar 09 '21 17:03 optimalui

I was surprised by this as well.

You can do a simple workaround by wrapping it in your own componenthave a computed property like this to trick the component to believe the label is the value

<!-- in wrapper-vue-select -->
  computed: {
    model: {
      get() {
        return this.options.find(x => x.id === this.value).label;
      },
      set(option) {
        this.$emit('input', option.id);
      }
    }

}

jonakirke94 avatar Mar 18 '21 09:03 jonakirke94

Don't understand how to implement the previous method. Why isn't there an update on this? This completely breaks the initial loading view of the component.

HenrijsS avatar Mar 28 '21 16:03 HenrijsS

Same here setting default value programmatically displays the code instead of the label.

neverender24 avatar Apr 13 '21 02:04 neverender24

Same issue If I have 2 selectboxes with one source for options, and I update on the fly this source after first selectbox was selected label is changed by id...This is weird!

7iomka avatar Apr 14 '21 12:04 7iomka

Same issue, any solution???

Quang-Dong avatar Apr 15 '21 10:04 Quang-Dong

Same issue, any solution???

Yes. Vue Multiselect - https://vue-multiselect.js.org/

Works better and has more features.

HenrijsS avatar Apr 15 '21 10:04 HenrijsS

Just use the get-option-label prop: https://vue-select.org/api/props.html#getoptionlabel

<template>
  <vue-select
    :options="options"
    :reduce="item => item.id"
    :get-option-label="getLabel"
  />
</template>

<script>
export default {
  data: () => ({
    options: [
      { id: 1, label: 'Option 1' },
      { id: 2, label: 'Option 2' }
    ]
  }),
  methods: {
    getLabel (option) {
      if (typeof option === 'object') {
        return option.label
      }
      const optionObject = this.options.find(item => item.id.toString() === option.toString())
      if (optionObject) {
        return optionObject.label
      }
      return option
    }
  }
}
</script>

esl51 avatar May 04 '21 06:05 esl51

Adding if ((option && this.label && this.index) || !option || !option.length) return ""; above return option; here seems to resolve the problem.

rgavinc avatar Jun 08 '21 17:06 rgavinc

More codes, bad experience

8debug avatar Jun 19 '21 04:06 8debug

Same here. Tried to solve it with reduce, spent hours to debug this. Well, ended up creating a method like above, but that just unnecessary code for such a basic thing. I liked the simplicity of vue-select, so i really wanted to solve it, but ended switching to vue-multiselect - just works there.

strstr1 avatar Apr 09 '22 15:04 strstr1