I am trying to hide and show a table column based on user selection. But it is not working as expected. Here is the link to the codepen file.
https://codepen.io/nigel1995/pen/XWKbqoe?editors=1111
<div id="app">
<div v-for="item in columnHeaders">
<label>
<input type='checkbox' :value='item.value' v-model="selectedType"> {{ item.text }}
</label>
</div>
<table class="selection-div">
<tr>
<template v-for="item in columnHeaders">
<th v-if="showHeader(item)">{{ item.text }}</th>
</template>
</tr>
<tr v-for="desert in deserts">
<td v-if="!hideCol(desert)">{{ desert.name }}</td>
<td v-if="!hideCol(desert)">{{ desert.fat }}</td>
</tr>
</table>
</div>
new Vue({
el: '#app',
data: {
selectedType: [],
columnHeaders: [
{ text: 'Name', value: 'name', show: true },
{ text: 'Fat (g)', value: 'fat', show:true },
],
deserts: [
{
name: 'Frozen Yogurt',
fat: 6.0,
},
{
name: 'Golden Yogurt',
fat: 6.0,
},
{
name: 'Tasty Yogurt',
fat: 6.0,
}
]
},
methods: {
hideCol(row){
const t = Object.keys(row)[0];
return this.selectedType.includes(t);
},
showHeader(val){
return (val.show && !this.selectedType.includes(val.value));
}
},
});
What I want to do is making the display of table columns dynamic and not check it in each column as table headers will be dynamic in the future. Any direction would be appreciated.