I am using a computed value to dynamically filter an array ("orders").
The computed .filter() function allows the user to dynamically search by order number, name or reference:
data() {
return {
orders: [],
search: "" // search string from a text input
};
},
computed: {
filtered:
return this.orders.filter(order => {
const s =
order.order_number + order.reference + order.name;
const su = s.toUpperCase();
return su.match(this.search.toUpperCase());
});
}
I am using a v-for loop to render the search results as follows:
<tbody v-for="(order, index) in filtered" :key="order.id">
<tr>
<td @click="add_events(order, index)>{{order.order_number}}</td>
<td>{{order.reference}}</td>
<td>{{order.name}}</td>
...
</tr>
</tbody>
I want to use the @click to target a specific component (an object) in the filtered array and use $set to append a value ("objEvents") to that object:
methods: {
add_events (order, index) {
const objEvents= [ external data from an API ]
this.$set(this.orders[index], "events", objEvents)
}
}
However the index of the component in the filtered array ("filtered") is not the same as its index in the original array ("orders") and so the add_events method targets the wrong component.
Can I use key to target the correct component? or is there some other way to identify the target component in the filtered array?