-1

I have a v-for in a v-for. After clicking on my selected dropdown-item I pass my data to my methods - this works well, of course.

But now I need the exact itemDropdown.ID in another function in my methods...

Do I have to pass data from method to method or how can I solve that?

Thank You!

<div v-for="(item, index) in json" :key="index">
  <b-dropdown text="Selection" right>
    <b-dropdown-item v-for="(itemDropdown, indexDropdown) in json2" :key="indexDropdown" @click="inputDropdown(itemDropdown.ID)">{{itemDropdown.Name}}</b-dropdown-item>
  </b-dropdown> 
</div>
methods: {
  inputDropdown(ID) {
    //Some code
  },

  anotherFunction(here I need this itemDropdown.ID as well!)
  //Do some code too
  }  
}
3
  • Can you call anotherFunction() in inputDropdown() ? Is it mess your logic ? Commented Oct 20, 2021 at 7:09
  • Does this answer your question? How to call multiple functions with @click in vue? Commented Oct 20, 2021 at 7:10
  • Not clear what's your issue... How are you invoking anotherFuntion()? If you're calling it from inputDropdown() the most obvious way is to pass ID since it's already in scope Commented Oct 20, 2021 at 7:11

2 Answers 2

1

I think it would be better to write some handler method like that and you can extend the usage with other methods too.

<div v-for="(item, index) in json" :key="index">
  <b-dropdown text="Selection" right>
    <b-dropdown-item v-for="(itemDropdown, indexDropdown) in json2" :key="indexDropdown" @click="dropdownClickHandler(itemDropdown.ID)">{{itemDropdown.Name}}</b-dropdown-item>
  </b-dropdown> 
</div>

I think that it would be more readable in a usage like that.

methods: {
  dropdownClickHandler(ID) {
    this.inputDropdown(ID);
    this.anotherFunction(ID);
  },

  inputDropdown(ID) {
    //Some code
  },

  anotherFunction(ID)
   // Some code
  }  
}
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, that seems to work. But just for additional info if anotherFunction is a current function in my template with parameters like anotherFunction(value, value, value) is it possible too to pass the ID into this function too?
yes, but you need to modify with additional parameters like dropdownClickHandler(itemDropdown.ID, itemDropdown.value). Or better version just passing dropdownClickHandler(itemDropdown) and getting parameters in the inputDropdown and anotherFunction by using object dessturcturing here developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
0

You can call multiple methods in the @click="fnA(var); fnB(var);"

<b-dropdown-item v-for="(itemDropdown, indexDropdown) in json2"
                 :key="indexDropdown"
                 @click="inputDropdown(itemDropdown.ID); anotherFunction(itemDropdownID)"
>
    {{itemDropdown.Name}}
</b-dropdown-item>

Comments

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.