You could assign a type of an empty array to the fields prop then pass in the array of fields that you need to loop through in your vue template.
dynamic table
Hello,
i build a little data-table to output different data with one vue-component. the problem is, the object keys are not the same for each table. There are always 2 columns but it can be "ID, Name" or "Extension, Name".
I tried for 2 days but couldnt find a solution to use prop-values as object keys in v-for. this is what it actually looks:
<template>
<div style="overflow: auto; height: 500px;">
<table class="table">
<thead>
<tr><th class="w-25" v-for="field in fields">{{field}}</th></tr>
</thead>
<tbody>
<tr v-for="item in data"><td class="w-25">{{item.id}}</td><td>{{item.id}} </td></tr>
</tbody>
</table>
</div>
</template>
<script>
export default {
props: ['data','fields']
}
</script>
I would need something like item.field[0] and item field[1], but i didnt knew how to get this working. Is it possible or has someone a hint how to get this working?
Thanks in Advance.
Simon
So your row objects can have a bunch of fields, it's that value function you define in the column object that pulls the correct one:
columns = [
{
heading: 'Id',
value: row => row.id
},
{
heading: 'Name',
value: row => row.name
}
]
It's admittedly a kinda tough thing to wrap your head around but it makes for a pretty sweet re-usable table component when you get it set up, I'd show you my table but there's a lot more going on with it that would make it harder to digest... I think you should probably go back through all the vue related videos on here, it will pay off in the long run, I watched them all a few times before I started building my components.
Please or to participate in this conversation.