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 can I implement pinch zoom functionality in a Vue.js project?
- How do I determine which version of Vue.js I am using?
- How do I use the v-if directive in Vue.js?
- How do I use Yup with Vue.js?
- How can I use Vue and Chart.js to add zoom functionality to my chart?
- How to use a YAML editor with Vue.js?
- How can I convert XML data to JSON using Vue.js?
- How can I use Vue.js to create a XSS payload?
- How do I use the Vue.js Wiki?
- How do I use the v-model in Vue.js?
See more codes...