VDataTable
is driven by the data you feed to it through headers
& items
. If the sources change, the table changes. So, don't do anything else, just update the sources (in a reactive way) & your table is going to update according the new set of data fed to it.
new Vue({
el: '#app',
vuetify: new Vuetify(),
computed: {
headers() {
return this.items.length ? Object.keys(this.items[0]).map(key => ({
value: key,
text: key
})) : []
}
},
data() {
return {
items: []
}
},
methods: {
async fetchData(type) {
const response = await fetch(`https://jsonplaceholder.typicode.com/${type}`)
this.items = await response.json()
console.log(this.items)
},
}
})
<link href="https://fonts.googleapis.com/css?family=Roboto:100,300,400,500,700,900" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/@mdi/[email protected]/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vuetify.js"></script>
<div id="app">
<v-app>
<v-main>
<v-container>
<v-row>
<v-col>
<v-btn-toggle>
<v-btn @click="fetchData('users')">
FETCH USERS
</v-btn>
<v-btn @click="fetchData('posts')">
FETCH POSTS
</v-btn>
</v-btn-toggle>
</v-col>
</v-row>
<v-row>
<v-col>
<v-data-table :headers="headers" :items="items" />
</v-col>
</v-row>
</v-container>
</v-main>
</v-app>
</div>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…