0

Interface :

enter image description here

How when the index of the returned record is an even number the Row-reverse class is added ?

Debug Diva
  • 26,058
  • 13
  • 70
  • 123
BinChanhKun
  • 73
  • 2
  • 8
  • It's unknown what your case exactly is. But you can start with checking docs, https://vuejs.org/guide/essentials/class-and-style.html#binding-html-classes – Estus Flask Jul 26 '22 at 06:13
  • When I call the api it returns the records with the index attribute, and I want if the index is even, it will add a class to it. – BinChanhKun Jul 26 '22 at 06:25
  • What is the problem with this? If you need to write a condition for odd/even, this isn't tied to Vue, and its surely have been already discussed https://stackoverflow.com/questions/6211613/testing-whether-a-value-is-odd-or-even – Estus Flask Jul 26 '22 at 06:31

1 Answers1

0

You can simply achieve that by binding a class attribute dynamically in the element based on the JS expression.

:class="index % 2 == 0 ? 'product row-reverse' : 'product'"

Live Demo :

new Vue({
  el: '#app',
  data: {
    items: ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5', 'Item 6']
  }
})
.row-reverse {
  background-color: yellow;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <li :class="index % 2 == 0 ? 'product row-reverse' : 'product'" v-for="(item, index) in items" :key="index">{{ item }}</li>
</div>
Debug Diva
  • 26,058
  • 13
  • 70
  • 123