I have data that is nested and I want to display it in a tabular format. Here is an example of the data; in the actual application, there could 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>
Please note: the inner values ('CCC', 'F', 'H', 'y') may end up being further nested arrays or objects.
I attempted to create a grid layout using 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>
(Note: the more logical grid-column:1/2
, to span two columns, doesn't seem to work...)
This grid layout does not show the correct structure, so I have included a traditional table layout at the end to demonstrate what I am aiming for.
https://i.sstatic.net/lPKbx.png
Is this issue due to a simple bug? Or is there a better approach to combining vue and grid layouts? Keep in mind that I might need to implement a third v-for
loop as mentioned earlier.
Just for reference, here is the HTML code for the table:
<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>