So i am creating a base "TableComponent", with selectable rows etc. This TableComponent takes a prop called "buttons".
The TableComponent expects the buttons to be an array of objects like this:
buttons: [{
label: 'Manage Themes',
click: () => {
if(this.selectedRows.length) {
this.$router.push({ name: 'ManagePageOptions', query: { concernable_type: this.type, concernable_ids: this.selectedRows.map(row => row.id).join(',') }});
} else {
alert('Please select 1 or more company or user');
}
}
}]
And so in the table component, a row of buttons is created based on what is passed like so:
<b-button v-for="button in this.buttons" @click="button.click" :key="button.label"> {{ button.label }} </b-button>
Problem is that in the buttons click function, "this" refers to the parent component, and so i don't have access to the selected rows etc.
EDIT: I have found that just passing "this" to the click function solves it. Not sure if it is wise though.
buttons = [{
label: 'Manage Pages',
click: (vm) => {
if(vm.selectedRows.length) {
vm.$router.push({ name: 'ManagePageOptions', query: { concernable_type: vm.type, concernable_ids: vm.selectedRows.map(row => row.id).join(',') }});
} else {
alert(Lang('Please select 1 or more company or user'))
}
}
}];
TableComponent data:
data() {
return {
selectedRows: [],
m_this: this,
};
},
_
<b-button v-for="button in this.buttons" @click="button.click(m_this)" :key="button.label"> {{ button.label }} </b-button>
@click=button.click($event), and make use of a variable in theclick: (obj) => {}and then check what this gives youclickevent (or any name of your choice). The consuming parent,TableComponent, will then listen to this VueJS event using thev-onor@shorthand, and decide how to handle it thereafter.clickrather than trying to grab them viathis.