1 | initial version |
To update tables using axios in Vue/Vuetify 3 while using imported data-returning methods, you would follow these steps:
import axios from 'axios'
export default {
methods: {
async getTableData(query) {
const response = await axios.get(`/api/search?q=${query}`)
this.tableData = response.data.results
}
}
}
<template>
<div>
<v-text-field v-model="searchQuery" label="Search"></v-text-field>
<v-btn @click="getTableData(searchQuery)">Search</v-btn>
</div>
</template>
mounted
lifecycle hook to populate your table initially. You can also use this method to update the table with new data when the input method is triggered.<template>
<div>
<v-data-table :items="tableData" :headers="tableHeaders"></v-data-table>
</div>
</template>
<script>
import { getData } from "@/api/dataMethods"
export default {
data() {
return {
tableHeaders: ["name", "age", "email"],
tableData: []
}
},
methods: {
async getTableData(query) {
const response = await axios.get(`/api/search?q=${query}`)
this.tableData = response.data.results
}
},
mounted() {
this.tableData = getData()
}
}
</script>
In this example, we are importing a getData()
method from @/api/dataMethods
and calling it in the mounted
hook to initially populate the table with returned data. When the user performs a search, the getTableData()
method is called with the search query, and the table is updated with the resulting data from the API call.