Please see the code
<div v-for="msg in leftMsg">
div v-if="msg.last_sender" @click.prevent="loadMsg(msg)">
<tr :class="['active',{ 'seens' : !msg.seen, 'selected':msg.isActive}]">
// some html
</tr>
</div>
</div>
loadMsg(obj){
obj.isActive = !obj.isActive;
}
The problem is, it is adding selected class properly but when I click another item it adds selected but doesn't remove the old one. How can I keep only the most recent clicked item selected?
Thank you.
I have solved this issue using a for loop. I thought it may help some other. In order to remove the all other previous active classes all you need to run a for loop and make them false and then assign active=true to the newest one.
Here is the code that may help
Add a data property outside of the
msg
objects and use that to track the active message.Then in your template, set the
activeMessage
.The key parts I changed here are
@click.prevent="activeMessage = msg"
and'selected': msg === activeMessage
. This will setactiveMessage
to the clicked message, and then theselected
class will be applied to theactiveMessage
and will only apply to theactiveMessage
.I would also note that it's strange that you have a
tr
element nested insidediv
. I assume it was just because of your example, but that's not technically valid HTML.