Greetings everyone,
I am currently working on creating a table with checkboxes. The goal is to add the ID to an array of objects when the checkbox is clicked and remove it when unchecked.
Here is a snippet of my code:
HTML Table
...
<td><input type="checkbox" @click="checkThis(id)" /></td>
...
Vue.js Code Sample
import _ from 'lodash'
export default {
data(){
return{
myArray: []
}
},
methods:{
checkThis(id){
// Remove object from array
_.remove(this.myArray, {myArrayId: id})
// Push new object to array
this.myArray = [...this.myArray, {
myArrayId: id
}]
}
}
}
The above code removes the ID if the user unchecks the specific checkbox.
_.remove(this.myArray, {myArrayId: id})
One issue I am facing is that the script doesn't differentiate between which IDs to remove or whether the checkbox is checked or unchecked for a specific ID.
My aim is to add the ID to the array only if the checkbox is checked, and remove it if unchecked.
Any assistance would be greatly appreciated!