[Vuejs]-VueJS and Laravel Blade – incrementing counter in nested v-for loops

0👍

Here you go: https://jsfiddle.net/mimani/7o4k0gf2/

JS

new Vue({
  el: '#app',
  mounted () {
    var sum = 0
    for (const key of Object.keys(this.agegroups)) {
      sum += this.agegroups[key]
        this.offset.push(sum)
        }
  },
  data: {
    agegroups: {
      adult: 3,
      child: 1,
      infant: 2
    },
    offset: [0]
  }
});

HTML

  <ul>
    <template v-for="(num, agegroup, idx) in agegroups" v-init="getOffset(num)">
      <li v-for="index in num">
         {{offset[idx] + index}}
      </li>
    </template>
  </ul>

I had to create a variable which is pre populated based on agegroups to keep track of earlier indexes, and I add this to index of nested for loop.

Leave a comment