I have an array that looks like this:
var arr = [{
text: '1'
}, {
text: '2'
children: [{
text: '2.1'
}]
}, {
text: '3',
children: [{
text: '3.1'
}, {
text: '3.2',
children: [{
text: '3.2.1'
}, {
text: '3.2.2'
}]
}]
}];
I want to display only the "leafs" of this array in a table, while showing the parents of the leafs. I want to generate the following HTML:
<table>
<tr>
<td>1</td>
</tr>
<tr>
<td>2 | 2.1</td>
</tr>
<tr>
<td>3 | 3.1</td>
</tr>
<tr>
<td>3 | 3.2 | 3.2.1</td>
</tr>
<tr>
<td>3 | 3.2 | 3.2.2</td>
</tr>
</table>
I tried the following, but it doesn't work because the <section>s are interspersed with the <tr>s.
<section v-for="parent in arr">
<tr v-if="!parent.children">
<td>{{ parent.text }}</td>
</tr>
<section v-if="parent.children" v-for="child in parent.children">
<tr v-if="!child.children">
<td>{{ parent.text + ' | ' + child.text }}</td>
</tr>
<tr v-if="child.children" v-for="grandchild in child.children">
<td>{{ parent.text + ' | ' + child.text + ' | ' + grandchild.text }}</td>
</tr>
</section>
</section>
computedthat recursively descends the tree and yields a simple array. It will be easy to render that.