silasmontgomery / vue-dynamic-select

A VueJS plugin that provides a searchable and reactive select list component with no dependencies.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Pre Define selection

amchconsult opened this issue · comments

how do you pre define selected option by id?

When you bring data from server.

In order to predefine/preselect an item you need to provide the v-model prop with an object (which would itself have an id property). If you only have an id and you already have the options array you can find and set the predefined object by it's id. See the example below and let me know if it makes sense.

Sample DynamicSelect instance
<dynamic-select :options="options" option-value="id" option-text="name" v-model="selectedOption"></dynamic-select>

Sample options array (fetched from API or hard coded)
this.options = [ { id: 1, name: 'One' }, { id: 2, name: 'Two' }, { id: 3, name: 'Three' } ]

Selected option (after options array is fetched)
this.selectedOption = this.options.find(o => o.id == this.preDefinedId)