I would like to use Vue to render a table with rows but have the cells as a component.
I have made a working example of what I would like to see as the end result and have some code relating to how I think that I could go about this:
HTML
<div id="app">
<table>
<thead>
<tr>
<th>Row</th>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr v-for="(row, rindex) in people">
<td>{{ rindex }}</td>
<cell v-for="(value, vindex, rindex) in row" :value="value" :vindex="vindex" :rindex="rindex"></cell>
</tr>
</tbody>
</table>
<template id="template-cell">
<td>{{ value }}</td>
</template>
</div>
JS
// Register component
Vue.component('cell', {
template: '#template-cell',
name: 'row-value',
props: ['value', 'vindex', 'rindex']
// In here I would like to access value, vindex and rindex
});
// Start application
new Vue({
el: '#app',
data: {
people: [
{id: 3, name: 'Bob', age: 27},
{id: 4, name: 'Frank', age: 32},
{id: 5, name: 'Joe', age: 38}
]
}
});
This is because, in the Vue documentation on v-for they show the example:
And another for the index:
<div v-for="(value, key, index) in object">
{{ index }}. {{ key }} : {{ value }}
</div>
So logically thinking, I should be able to get the rows index in the cell component without too much issue but this is not so as it comes up with a bunch of errors indicating that the values are undefined.
If anyone could point me in the right direction here then I would be greatly appreciate it as I'm out of ideas with this problem but would really like to understand how to implement this correctly.
(Note: the reason for me wanting to render the cells in the component is to register the change of values, see the answer to another question that I asked here if you're interested)