When using Vuejs3, I am currently iterating through an array of objects:
<div
v-for="ligne in lignes"
:key="ligne.id"
:class="{ 'border-b-2':isSelected }"
:id="`ligne_${ligne.id}`"
>
<span @click="select(ligne.id)">X</span>
</div>
I am trying to apply the class 'border-b-2' only on the selected line, but I am facing difficulties in doing it dynamically. Setting isSelected
to true in the vue devtools is applying this style to all lines instead of just the selected one.
As a temporary solution, I have wrapped this code inside a function (select(id)) and changed the html class accordingly.
document.getElementById(`ligne_${id}`).classList.add('border-b-2')
This approach seems overly verbose. Is there a way to accomplish this by utilizing the :key
or the v-for
loop more effectively?