admin管理员组文章数量:1406060
I am iterating through a list of products and displaying them in cards. I'd like to display a promotion after every 18th product listed:
<div
v-for="(prod, index) in products"
:key="index"
class="w-full md:w-1/3 lg:w-1/4 xl:w-1/4 mb-8 px-2
>
<promotion v-if="(index % 18 == 0) && index != 0" ></promotion>
<product-card v-else :product="prod"></product-card>
</div>
The way I have this written now, the promotion is displayed but it is inserted in place of the item with the matching index. How do I place the promotion before or after the nth item without replacing it?
I am iterating through a list of products and displaying them in cards. I'd like to display a promotion after every 18th product listed:
<div
v-for="(prod, index) in products"
:key="index"
class="w-full md:w-1/3 lg:w-1/4 xl:w-1/4 mb-8 px-2
>
<promotion v-if="(index % 18 == 0) && index != 0" ></promotion>
<product-card v-else :product="prod"></product-card>
</div>
The way I have this written now, the promotion is displayed but it is inserted in place of the item with the matching index. How do I place the promotion before or after the nth item without replacing it?
Share Improve this question asked Feb 11, 2019 at 23:55 retrograderetrograde 2,9796 gold badges31 silver badges54 bronze badges2 Answers
Reset to default 6To preserve grid css, you can put them under a template
and use v-for
<template v-for="(prod, index) in products">
<div class="w-full md:w-1/3 lg:w-1/4 xl:w-1/4 mb-8 px-2"
v-if="(index % 18 == 0) && index != 0"
:key="'promotion-${index}'">
<promotion></promotion>
</div>
<div class="w-full md:w-1/3 lg:w-1/4 xl:w-1/4 mb-8 px-2"
:key="'product-${index}'">
<product-card :product="prod"></product-card>
</div>
</template>
I think you just need to remove the v-else here inside the product-card element
<product-card :product="prod"></product-card>
本文标签: javascriptInsert item after every nth item in vfor loopStack Overflow
版权声明:本文标题:javascript - Insert item after every nth item in v-for loop - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744959349a2634556.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论