I've nested data I'd like to display in tabular format. Here is some example data; in the real application there may sometimes be dozens of entries in rows.
<script>
export default {
name: 'Example',
data () {
return {
results: [
{ a: 'AAA', rows: { BBB: 'CCC' } },
{ a: 'D', rows: { E: 'F', G: 'H' } },
{ a: 'Tom, Dick & Harry', rows: { x: 'y' } },
],
}
},
}
</script>
Note: the inner value ('CCC', 'F', 'H', 'y') might end up being a further nested array or object.
I tried this grid layout, with v-for loops:
<template>
<div style="width:100%">
<div v-for="(res,key) in results" :key="key" class="results">
<div class="A">{{res.a}}</div>
<div v-for="(c,b) in res.rows" :key="b">
<div class="B">{{b}}</div>
<div class="C">{{c}}</div>
</div>
</div>
</div>
</template>
<style scoped>
.results {display:grid;grid-template-columns: 1fr 1fr;}
.A {font-weight:bold;grid-column:1/3;}
.B {grid-column:1;padding:0.5rem;}
.C {grid-column:2;padding:0.5rem;}
</style>
(Aside: the more logical grid-column:1/2, to get a span of two columns, seems to do nothing...)
This gives me the wrong layout; I've appended a good old-fashioned table layout to show what I am trying to achieve (HTML for that at end of question).
Do I have a simple bug? Or, is there a whole better approach to using vue and grid together? (Bearing in mind, as mentioned above, that I may end up with a 3rd v-for loop.)
By the way, last time I tried using grid I gave up, and used <table> tags; I got around the problem of v-for creating html tags by putting it on a <tbody>, i.e. having a table body for each section. But that won't scale to a 3rd v-for loop. And I'd rather use CSS grid, if I can, as it gives more power for responsive layouts.
The table HTML, for reference:
<table border="1" cellspacing=0 style="width:100%;">
<tr>
<td colspan="2"><b>AAA</b></td>
</tr>
<tr>
<td>BBB</td>
<td>CCC</td>
</tr>
<tr>
<td colspan="2"><b>D</b></td>
</tr>
<tr>
<td>E</td>
<td>F</td>
</tr>
<tr>
<td>G</td>
<td>H</td>
</tr>
<tr>
<td colspan="2"><b>Tom, Dick & Harry</b></td>
</tr>
<tr>
<td>x</td>
<td>y</td>
</tr>
</table>
