I'm a bit of a novice coder and I'm struggling to get some data to display in a table on a web page. This feels like it should be basic, but I just can't quite get the display right.
The data in the DB has the following fields, with each row containing data that will need to become a column heading (round) and a row heading (Ate) in the final rendered table.
Code,Ate,Round,Value,Active
ZOM3,Ears,16,18,True
ZOM4,Ears,16,22,True
ZOM4,Knees,16,24,True
ZOM4,Arms,16,90,True
ZOM4,Brains,16,940,True
ZOM4,Ears,17,29,True
ZOM4,Arms,17,114,True
ZOM4,Brains,17,746,True
ZOM4,Ears,18,23,True
ZOM4,Shins,18,1930,True
ZOM4,Arms,18,135,True
ZOM4,Brains,18,952,True
ZOM4,Ears,19,22,True
ZOM4,Shins,19,580,True
ZOM4,Knees,19,32,True
ZOM4,Arms,19,139,True
ZOM5,Shins,18,14,True
For character ZOM4, the way I need this to present on a web page is in a table like this. I will display one of the characters at a time (ZOM4):
| 16 | 17 | 18 | 19 | |
|---|---|---|---|---|
| Arms | 90 | 114 | 135 | 139 |
| Brains | 940 | 746 | 952 | |
| Ears | 22 | 29 | 23 | 22 |
| Knees | 24 | 32 | ||
| Shins | 1930 | 580 |
Sadly, my code does not. In the .vue file I have the following code, which ignores the null/missing Value values so that ZOM4 displays 1930 Shins in round 16 instead of round 18.
const props = defineProps<{
data?: CharacterDetail;
}>();
const eats = computed(() => {
if (!props.data) return {};
const d = {} as { [key: string]: { rounds: { [key: string]: any } } };
for (let x of props.data.roundlyData) {
if (!d[x.ate]) {
d[x.ate] = { rounds: {} };
}
d[x.ate].rounds[x.round] = x.value;
}
return d;
});
const rounds = computed(() => {
const t = Object.keys(eats.value)[0];
if (!t) return [];
return Object.keys(eats.value[t].rounds);
});
</script>
<template>
<table class="triage-table"> <!-- might need to sort the above data by ate and/or by round before presenting it. Test with Co. WPL -->
<colgroup>
<col style="width: 60px" />
</colgroup>
<thead>
<tr v-if="rounds">
<th></th>
<th v-for="round of rounds" :key="round">{{ round }}</th>
</tr>
</thead>
<tbody>
<tr v-for="(t, ate) of eats" :key="ate">
<td>{{ ate }}</td>
<td v-for="r in t.rounds" :key="r">{{ r }}</td>
</tr>
</tbody>
</table>
</template>
This is driving me crazy. I understand what's going wrong, but not how to make it right. I'm deliberately trying not to hard code the row or column headings as those details may change or expand later as I play more with this idea.
Can someone please help me get the table to display in a way that respects the nulls/missing data. Thanks
props.data?props.dataadded