vue.jsHow do I create a table using Vue.js?
Creating a table using Vue.js is a straightforward process. To do this, you will need to create a template, create a script, and use the v-for directive to iterate through the data.
Example code
<template>
<table>
<thead>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr v-for="person in people" :key="person.name">
<td>{{person.name}}</td>
<td>{{person.age}}</td>
</tr>
</tbody>
</table>
</template>
<script>
export default {
data() {
return {
people: [
{ name: 'John', age: 25 },
{ name: 'Jane', age: 32 },
{ name: 'Bob', age: 21 }
]
}
}
}
</script>
The code above will render the following table:
Name Age
John 25
Jane 32
Bob 21
The template contains the HTML elements that will be used to create the table. The script contains the data that will be used to populate the table. The v-for directive is used to iterate through the data and create the table rows.
Code explanation
<template>
- This is the HTML element that contains the table elements.<thead>
- This is the table head element, which contains the column headers.<tr>
- This is the table row element, which contains the individual cells.<th>
- This is the table header element, which contains the column headers.<tbody>
- This is the table body element, which contains the table rows.v-for
- This is the Vue directive that is used to iterate through the data and create the table rows.data()
- This is the function that contains the data that will be used to populate the table.
Helpful links
More of Vue.js
- How do I download a zip file using Vue.js?
- How do I set a z-index in Vue.js?
- How do I get the z-index to work in Vue.js?
- How do I determine which version of Vue.js I am using?
- How do I install Yarn with Vue.js?
- How do I use the v-model in Vue.js?
- How can I use Vue and Chart.js to add zoom functionality to my chart?
- How can I use Vue.js to implement a zoomable image?
- How do I use Yup with Vue.js?
- How can I use Vue.js to implement image zooming on my website?
See more codes...