将参数传递给Vue.js中的multiselect的@select-vue-multiselect

时间:2019-05-06 20:31:37

标签: javascript vue.js v-for vue-multiselect

我正在尝试将参数传递给 Vue.js 中的 @select 事件函数,请参见下面的代码。

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)

让我知道如何实现。

2 个答案:

答案 0 :(得分:1)

您可以像这样简单地完成它:

 @select="onSelect_selectedGood($event,billItem)"

以及您的方法中:

 methods : {
   onSelect_selectedGood( selectedOption,billItem) {
      console.log(  selectedOption,billItem);

   },
}

传递的参数是$eventselectedOption

答案 1 :(得分:0)

如果要访问所有billItem, optionid,则可以创建一个自定义输入组件:

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作为道具,因此可以通过this.billItem在自定义组件中进行访问。