3

I am trying to pass parameter to @select event function in 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>

My Question: How can I pass billItem to onSelect_selectedGood so I can access it inside the function.

Something like @select="onSelect_selectedGood(billItem)" then implement the function like this onSelect_selectedGood( billItem, option, id)

Let me know how I can achieve it.

2
  • What library are you using here? Commented May 6, 2019 at 20:41
  • @Ohgodwhy am using this library vue-multiselect ( vue-multiselect.js.org ) Commented May 6, 2019 at 20:54

2 Answers 2

8

You could do it simply like :

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

and in your methods :

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

   },
}

the passed parameters are the $event which is the selectedOption and billItem.

Sign up to request clarification or add additional context in comments.

Comments

0

If you want to access all billItem, option and id, you can create a custom input component:

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>

and then using it in your html:

<custom-input 
  :billItem="billItem"
/>

Because you passed billItem as a prop, you can access it by this.billItem in custom component.

2 Comments

thanks for the help. am getting this error [Vue warn]: Unknown custom element: <custom-input> - did you register the component correctly? For recursive components, make sure to provide the "name" option.
You need to register custom component vuejs.org/v2/guide/components-registration.html. But @Boussadjra Brahim solution is simpler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.