I'm struggling to figure out how to effectively utilize computed properties when presenting data in columns within a table format. You can view the complete code example at jsfiddle, here's a condensed version with an explanation. My goal is to display this data in a table:
var vueData = {
objects: [
{
name: "objectName",
objectData: [
{prop1: "val1", prop2: "val2"}
]
}
]
}
In this vueData
, each element of the objectData
array should be shown as a column (representing data for one month or day). The properties in the elements of objectData
should not be displayed as they are, but as calculated values. And these displayed values should update based on changes in vueData
.
So, I crafted this vue template:
<table>
<tr>
<th>Object Name</th>
<th>Data Piece 1</th>
<th>Data Piece 2</th>
</tr>
<template v-for="obj in objects">
<tr>
<th rowspan="2">{{ obj.name }}</th>
<td v-for="dataPiece in obj.objectData">{{ compute(dataPiece.prop1) }}</td>
</tr>
<tr><td v-for="dataPiece in obj.objectData">{{ compute(dataPiece.prop2) }}</td></tr>
</template>
</table>
Everything functions correctly except that I used methods instead of Vue's computed properties. The issue with methods is that their results aren't cached, so after changing just one property, ALL cell values are recalculated.
I could opt for a Vue component for each cell using a computed property instead of using td
s, but it seems like overkill since the table could be large.
Is there another solution from a Vue perspective? (Without altering the data structure or appearance of the table, etc).