admin管理员组文章数量:1427333
I am new to VueJS, and i am using Vuejs-Multiselect, enabling the multiple option.
I need to pass the selected value to the toggleSelected
method .
In this pen, there is a simulation :
I see the values are concatenated, eg.: when i choose the first value i can see using console.log : [object Object], choosing the second [object Object],[object Object] and so on.
How can i get just the selected value and so the code value from the options array?
In the html i do use :
<div id="app">
<multiselect
v-model="value"
:options="options"
:multiple="true"
track-by="name"
:hide-selected="true"
:close-on-select="false"
:custom-label="customLabel"
:searchable="false"
placeholder=""
:show-labels="false"
@input="toggleSelected(value)"
>
</multiselect>
<pre>{{ value }}</pre>
</div>
In the script i do use :
new Vue({
ponents: {
Multiselect: VueMultiselect.default
},
data: {
value: [],
options: [
{ name: 'Agriculture', code: '1' },
{ name: 'Police', code: '2' },
{ name: 'Medical', code: '3' }
]
},
methods: {
customLabel (option) {
return `${option.name}`
},
toggleSelected(value) {
alert( `${value.name}` )
console.log(' >> '+ `${value}` )
}
}
}).$mount('#app')
I am new to VueJS, and i am using Vuejs-Multiselect, enabling the multiple option.
I need to pass the selected value to the toggleSelected
method .
In this pen, there is a simulation : https://codepen.io/amrigo/pen/arvadE
I see the values are concatenated, eg.: when i choose the first value i can see using console.log : [object Object], choosing the second [object Object],[object Object] and so on.
How can i get just the selected value and so the code value from the options array?
In the html i do use :
<div id="app">
<multiselect
v-model="value"
:options="options"
:multiple="true"
track-by="name"
:hide-selected="true"
:close-on-select="false"
:custom-label="customLabel"
:searchable="false"
placeholder=""
:show-labels="false"
@input="toggleSelected(value)"
>
</multiselect>
<pre>{{ value }}</pre>
</div>
In the script i do use :
new Vue({
ponents: {
Multiselect: VueMultiselect.default
},
data: {
value: [],
options: [
{ name: 'Agriculture', code: '1' },
{ name: 'Police', code: '2' },
{ name: 'Medical', code: '3' }
]
},
methods: {
customLabel (option) {
return `${option.name}`
},
toggleSelected(value) {
alert( `${value.name}` )
console.log(' >> '+ `${value}` )
}
}
}).$mount('#app')
Share
Improve this question
edited May 8, 2019 at 20:01
Ângelo Rigo
asked May 8, 2019 at 19:43
Ângelo RigoÂngelo Rigo
2,16710 gold badges40 silver badges73 bronze badges
5
-
1
do not change
value
because it will pareoptions
to determin show / hide options in view. make a puted is more better. – 王仁宏 Commented May 8, 2019 at 19:58 - 1 codepen.io/anon/pen/PvPxoQ – 王仁宏 Commented May 8, 2019 at 20:01
- Thank you @王仁宏 your solution is very clear, did not know i could access the value in this way using the javascript map – Ângelo Rigo Commented May 8, 2019 at 20:11
- How can i reset the array so i can have just one value at every time i select one option from the select box ? – Ângelo Rigo Commented May 8, 2019 at 20:45
-
1
if you mean what value changed, vue-multiselect.js/#sub-events add @select attr and give a function to access
selectedOption
– 王仁宏 Commented May 8, 2019 at 20:51
2 Answers
Reset to default 2From the documentation, to get the selected option you use the @select event. Example:
<multiselect
// ... all your props*
@select="optionSelected">
</multiselect>
// dont put the brackets (),
//js will automatically pass the values to toggleSelected
//In your method:
optionSelected(option, id) {
console.log(option)
}
You can use a puted property for that. Do something like this:
puted: {
valueIds() {
return this.value.map(v => v.id);
}
}
See this working example:
https://jsfiddle/ebbishop/7eku4vf0/
本文标签:
版权声明:本文标题:javascript - Vuejs-Multiselect how to get just the selected option when enabling the multiple option - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745495326a2660780.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论