Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing index of a v-for loop in a sibling? (VueJS)

I'm trying to produce the following (rendering div elements, broken up by a clearfix after every four elements):

<div class="col-md-3 form-group"></div>
<div class="col-md-3 form-group"></div>
<div class="col-md-3 form-group"></div>
<div class="col-md-3 form-group"></div>
<div class="clearfix></div>
....

And wondering if anything as the code below is possible in VueJS or can someone help me out with an alternative?.

<div v-for="(sku, n) in sku_pattern" :key="n"  class="col-md-3 form-group"></div>
<div v-if="!((n+1)%4)" class="clearfix"> <!-- I know this won't work outside the scope of the previous element. -->
like image 418
SagunKho Avatar asked Nov 30 '25 01:11

SagunKho


1 Answers

You can use <template> tag with v-for to render a block of multiple elements:

<template v-for="(sku, n) in sku_pattern">
    <div class="col-md-3 form-group">
    </div>
    <div v-if="!((n+1)%4)" class="clearfix">
</template>

Reference document

like image 161
ittus Avatar answered Dec 03 '25 08:12

ittus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!