2

It is possible in vuejs to v-for a loop like php?

I want this output:

<select>
    <option>2</option>
    <option>1</option>
    <option>0</option>
</select>

My code (ticket.quantity = 2):

<select>
    <option v-for="index in ticket.quantity" :value="index" :key="index">{{index}}</option>
</select>

My output:

<select>
    <option>2</option>
</select>

2 Answers 2

1

You can try to loop ticket.quantity + 1 :

new Vue({
  el: '#demo',
  data() {
    return {
      ticket: {quantity: 2}
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo">
  <select>
    <option v-for="(q, index) in ticket.quantity + 1" :value="index" :key="index">{{index}}</option>
  </select>
</div>

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

2 Comments

this also works with vue2
@Ol D. Castor hey mate, sure, take a look at snippet again now it's vue2 :)
0

The answer of Nikola didn't worked for me completely but I managed to solve it like this:

<select>
    <option value="0" selected>0</option>
    <option v-for="index in parseInt(ticket.quantity)" :value="index" :key="index">{{index}}</option>
</select>

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.