I am currently using a v-for loop in Vue.js to create a list of items populated with data from an API. Here is an example of the items array:
items: [
{
foo: 'something',
number: 60
},
{
foo: 'anything',
number: 15
},
{
foo: 'text',
number: 20,
}
]
Template
<div v-for="(item,index) in items" :key="index">
<div :class="{ active: ????}" @click="toggleActive">
{{ item.foo }}
{{ item.number }}
</div>
</div>
JS
methods: {
toggleActive() {
//
}
}
I am looking for a solution where clicking on a div will add the class "active", and if it already has the class then it should be removed (toggled). Additionally, multiple items should be able to be selected.
Is there a way to achieve this without adding a boolean variable to the items array or moving each item into a separate component?