Vue-multiselect: How to only get id back when selecting item

Created on 21 Oct 2016  路  4Comments  路  Source: shentao/vue-multiselect

Hy,

having trouble with vue-multiselect. I only want to receive back to id of the selected item, not the whole object. How can I handle this? Also if I pass only an id to the selectbox as selected value, it doesn't select the value with this id, it only sets the label to the passed id.

I tried to prepare a jsfiddle here, but I don't get it working, it's complaining about prop "key" is a boolean instead of a string (https://jsfiddle.net/j8574gm4/1/)

question

Most helpful comment

If anyone need this:

<multiselect @input="opt => data.interval = opt.id" :value="data.interval" :options="intervals"></multiselect>

All 4 comments

In jsfiddle you鈥檙e trying to use the 1.x version of Multiselect with Vue 2.0. That won鈥檛 work. Please use the beta version.

In the beta you will also have to change the key="id" to track-by="id" due to conflicts with Vue.

This happens because you pass a primitive, so it doesn鈥檛 look for a label, because there is none. Just the string/number you passed.

You could however do something like this to fake a selected object.

update ({ id, label }) {
  this.selected = { id: id, label: label }
}

but I don鈥檛 see a reason to do this. Why do you need to recieve just the id?

If you need to preselect the multiselect鈥檚 value based on just the id, use:

this.selected = this.options.find(o => o.id === preselectedId)

somewhere in the mounted/ready lifecycle hook.

If anyone need this:

<multiselect @input="opt => data.interval = opt.id" :value="data.interval" :options="intervals"></multiselect>

This worked for me:

:options="listaEstado"
:multiple="false"
:close-on-select="true"
:clear-on-select="false"
:preserve-search="true"
placeholder="Selecione o Estado"
label="descricao"
track-by="descricao"
:preselect-first="false"
@input="listaCidades(estadoSelecionado.id)"

Is there anyway to do this, i respect that we didn't think this was a reasonable question. But it would actually be quite useful... If you could straight up get an array of ids i can send a request to api and parse id's into query string, this just adds another layer of confusion. We all know how to find a item in a list by its id?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hskrishna29 picture hskrishna29  路  3Comments

MaxHalford picture MaxHalford  路  4Comments

Uninen picture Uninen  路  4Comments

wujekbogdan picture wujekbogdan  路  4Comments

bushcode picture bushcode  路  3Comments