将参数传递给@select of multiselect in Vue.js - vue-multiselect
Pass parameters to @select of multiselect in Vue.js - vue-multiselect
我正在尝试将参数传递给 @select 事件函数 Vue.js
HTML
<template>
<div class="container">
<div v-for="(billItem, k) in billItems" :key="k" >
<div class="form-group row">
<label class="col-form-label col-sm-3" for=""> Products</label>
<div class="col-sm-3">
<div class="form-group">
<label for="">Product</label>
<multiselect
v-model="billItem.billItem_selectedGood"
:options="productOptions"
:close-on-select="true"
:clear-on-select="false"
:hide-selected="true"
:preserve-search="true"
placeholder="Select Product"
label="name"
track-by="name"
:preselect-first="false"
id="example"
@select="onSelect_selectedGood"
>
</multiselect>
</div>
</div>
</div>
</div>
</template>
JS
<script>
export default {
data(){
return {
form: new Form({
})
}
},
methods : {
onSelect_selectedGood( option, id) {
console.log("onSelect_selectedGood");
console.log(option);
}
},
mounted() {
}
}
</script>
我的问题: 我怎样才能将 billItem 传递给 onSelect_selectedGood 所以我可以在函数内部访问它。
有点像
@select="onSelect_selectedGood(billItem)"
然后像这样实现函数 onSelect_selectedGood( billItem, option, id)
告诉我如何实现它。
你可以像这样简单地做:
@select="onSelect_selectedGood($event,billItem)"
在你的方法中:
methods : {
onSelect_selectedGood( selectedOption,billItem) {
console.log( selectedOption,billItem);
},
}
传递的参数是$event
,也就是selectedOption
和billItem
。
如果你想访问所有billItem, option
和id
,你可以创建一个自定义输入组件:
CustomInput.vue
<template>
<multiselect
v-model="billItem.billItem_selectedGood"
:options="productOptions"
:close-on-select="true"
:clear-on-select="false"
:hide-selected="true"
:preserve-search="true"
placeholder="Select Product"
label="name"
track-by="name"
:preselect-first="false"
id="example"
@select="onSelect_selectedGood"
>
</multiselect>
</template>
<script>
export default {
props: ['billItem'],
methods: {
onSelect_selectedGood( option, id) {
console.log("onSelect_selectedGood");
console.log(option);
console.log(this.billItem)
}
}
}
</script>
然后在您的 html:
中使用它
<custom-input
:billItem="billItem"
/>
因为您将 billItem
作为 prop 传递,您可以在自定义组件中通过 this.billItem
访问它。
我正在尝试将参数传递给 @select 事件函数 Vue.js
HTML
<template>
<div class="container">
<div v-for="(billItem, k) in billItems" :key="k" >
<div class="form-group row">
<label class="col-form-label col-sm-3" for=""> Products</label>
<div class="col-sm-3">
<div class="form-group">
<label for="">Product</label>
<multiselect
v-model="billItem.billItem_selectedGood"
:options="productOptions"
:close-on-select="true"
:clear-on-select="false"
:hide-selected="true"
:preserve-search="true"
placeholder="Select Product"
label="name"
track-by="name"
:preselect-first="false"
id="example"
@select="onSelect_selectedGood"
>
</multiselect>
</div>
</div>
</div>
</div>
</template>
JS
<script>
export default {
data(){
return {
form: new Form({
})
}
},
methods : {
onSelect_selectedGood( option, id) {
console.log("onSelect_selectedGood");
console.log(option);
}
},
mounted() {
}
}
</script>
我的问题: 我怎样才能将 billItem 传递给 onSelect_selectedGood 所以我可以在函数内部访问它。
有点像
@select="onSelect_selectedGood(billItem)"
然后像这样实现函数 onSelect_selectedGood( billItem, option, id)
告诉我如何实现它。
你可以像这样简单地做:
@select="onSelect_selectedGood($event,billItem)"
在你的方法中:
methods : {
onSelect_selectedGood( selectedOption,billItem) {
console.log( selectedOption,billItem);
},
}
传递的参数是$event
,也就是selectedOption
和billItem
。
如果你想访问所有billItem, option
和id
,你可以创建一个自定义输入组件:
CustomInput.vue
<template>
<multiselect
v-model="billItem.billItem_selectedGood"
:options="productOptions"
:close-on-select="true"
:clear-on-select="false"
:hide-selected="true"
:preserve-search="true"
placeholder="Select Product"
label="name"
track-by="name"
:preselect-first="false"
id="example"
@select="onSelect_selectedGood"
>
</multiselect>
</template>
<script>
export default {
props: ['billItem'],
methods: {
onSelect_selectedGood( option, id) {
console.log("onSelect_selectedGood");
console.log(option);
console.log(this.billItem)
}
}
}
</script>
然后在您的 html:
中使用它<custom-input
:billItem="billItem"
/>
因为您将 billItem
作为 prop 传递,您可以在自定义组件中通过 this.billItem
访问它。