Home > OS >  For loop in vuejs like php
For loop in vuejs like php

Time:06-15

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>

CodePudding user response:

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>

CodePudding user response:

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>
  • Related