To populate a table using Vue.js 3 and the Composition API, you can follow these steps:
<template>
<div>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr v-for="(item, index) in items" :key="index">
<td>{{ item.id }}</td>
<td>{{ item.name }}</td>
<td>{{ item.age }}</td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
import { ref } from "vue";
import axios from "axios";
export default {
name: "Table",
setup() {
const items = ref([]);
axios.get("https://api.example.com/data").then((response) => {
items.value = response.data;
});
return { items };
},
};
</script>
ref
for your data and fetch the data using Axios:import { ref } from 'vue'
import axios from 'axios'
export default {
setup() {
const items = ref([])
axios.get('/api/data').then((response) => {
items.value = response.data
})
return { items }
}
}
v-for
to loop through the data and populate the table:<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr v-for="(item, index) in items" :key="index">
<td>{{ item.id }}</td>
<td>{{ item.name }}</td>
<td>{{ item.age }}</td>
</tr>
</tbody>
</table>
That's it! Now you have a table populated with data using Vue.js 3 and the Composition API.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-04-29 07:07:35 +0000
Seen: 14 times
Last updated: Apr 29 '23
What are the steps to utilize a for loop for generating and populating columns?
I created an Api to retrieve information from the header table, but I am encountering an error.
Can a table have horizontal scrollbars located both on the top and bottom?
How can I create a new table by changing the current table type?
What are the recommended best practices and suggestions for using the React Table?
What is the most effective approach to establish foreign keys for three adjacent tables?
Add information to the table that is linked to its own context.