In my application I have multiple tables on same page (also multiple pages). Each table has "Select all" option and checkboxes in each row. Code reuse would be really helpfull but I cant get this working. Currently I have following methods, but I always get Error in render: "TypeError: Cannot read property 'includes' of undefined". Right now this is code is inside one component, but should be available to another one. How can I properly extra this to standalone component and then use it in others?
In mounted method there is a field selected : {}.
Vue HTML template:
<input type="checkbox" v-bind:checked="isSelected(sts.id, 'country')"
@click="toggleSelected(sts.id, 'country')">
Vue methods:
isSelected(id, group) {
return this.selected[group].includes(id);
},
toggleAll(event, group, items) {
let state = $(event.target).prop("checked");
for (let st of items) {
if (state === true) {
this.addSelected(st, group);
} else {
this.removeSelected(st, group);
}
}
},
addSelected(id, group) {
if (!this.isSelected(id, group)) {
this.selected[group].push(id);
}
},
removeSelected(id, group) {
this.selected[group] = this.selected[group].filter(item => item !== id);
},
toggleSelected(id, group) {
if (this.isSelected(id, group)) {
this.removeSelected(id, group);
} else {
this.addSelected(id, group);
}
},