I'm beginner with Vue.js and I would like to use component. I have a (dynamic) table with machines (SN = serial number) and their money for each source so I want to use rowspan. Each row will be instance of component because has own checkbox and I would like to work with it later. Problem is that I don't know how to use loop with creating component which is a bit complicated inside table. Here is example:
<div id="container">
<table border="1">
<template v-for="row in storage.rows">
<tr>
<td rowspan="2"><input type="checkbox"></td>
<td rowspan="2">{{row.sn}}</td>
<td>{{row.source}}</td>
</tr>
<tr>
<td>{{row.pair.source}}</td>
</tr>
</template>
</table>
</div>
<script>
new Vue({
el: '#container',
data: {
storage: {
rows: [
{sn: 111, source: 'EK', pair: {source: 'VLT', in: 100}},
{sn: 222, source: 'EK', pair: {source: 'VLT', in: 200}}
]
}
},
});
</script>
This works fine but I don't know how to transform template to component. With Vue.js I'm using Laravel and its template engine Blade so I have workaround like this:
<div id="container">
<table border="1">
@foreach($differences as $difference)
<tr is="ek" diff="{{ json_encode($difference) }}"></tr>
<tr is="vlt" diff="{{ json_encode($difference->pair) }}"></tr>
@endforeach
</table>
</div>
<template id="ek-template">
<tr>
<td rowspan="2"><input type="checkbox"></td>
<td rowspan="2">@{{ difference.sn }}</td>
<td>@{{ difference.source }}</td>
</tr>
</template>
<template id="source-template">
<tr>
<td>@{{ difference.source }}</td>
</tr>
</template>
<script>
new Vue({
el: '#container',
data: {
storage: {
differences: {!! json_encode($differences) !!}
}
},
components: {
ek: {
template: '#ek-template',
props: ['diff'],
computed: {
difference: function () {
return JSON.parse(this.diff);
},
},
},
vlt: {
template: '#source-template',
props: ['diff'],
computed: {
difference: function () {
return JSON.parse(this.diff);
},
},
}
},
});
</script>
And I want to use Vue.js loop instead Laravel's foreach because I have already data in Vue.js object. Is there any solution how to do it?