I use v-skeleton-loader and v-if and finally use watch to check the data status.
<div id="app">
<v-app id="inspire">
<div>
{{options}}
<v-skeleton-loader v-if="firstLoad" :loading="loading" type="table">.
</v-skeleton-loader>
<v-data-table
:headers="headers"
v-show="!firstLoad"
:items="desserts"
:options.sync="options"
:server-items-length="totalDesserts"
:loading="loading"
class="elevation-1"
></v-data-table>
</div>
</v-app>
</div>
script:
new Vue({
el: '#app',
vuetify: new Vuetify(),
data() {
return {
totalDesserts: 0,
desserts: [],
loading: true,
firstLoad: true,
options: {},
headers: [
{
text: 'Dessert (100g serving)',
align: 'left',
sortable: false,
value: 'name',
},
{ text: 'Calories', value: 'calories' },
{ text: 'Fat (g)', value: 'fat' },
{ text: 'Carbs (g)', value: 'carbs' },
{ text: 'Protein (g)', value: 'protein' },
{ text: 'Iron (%)', value: 'iron' },
],
};
},
watch: {
options: {
handler() {
console.log('options watcher called');
this.getDataFromApi()
.then(data => {
this.desserts = data.items;
this.totalDesserts = data.total;
});
},
deep: true,
},
},
methods: {
getDataFromApi() {
this.loading = true;
return new Promise((resolve, reject) => {
const { sortBy, sortDesc, page, itemsPerPage } = this.options;
let items = this.getDesserts();
const total = items.length;
if ((sortBy && sortBy.length === 1) && (sortDesc && sortDesc.length === 1)) {
items = items.sort((a, b) => {
const sortA = a[sortBy[0]];
const sortB = b[sortBy[0]];
if (sortDesc[0]) {
if (sortA < sortB) return 1;
if (sortA > sortB) return -1;
return 0;
} else {
if (sortA < sortB) return -1;
if (sortA > sortB) return 1;
return 0;
}
});
}
if (itemsPerPage > 0) {
items = items.slice((page - 1) * itemsPerPage, page * itemsPerPage);
}
setTimeout(() => {
if (this.firstLoad) this.firstLoad = false
console.log('data is loaded');
this.loading = false;
resolve({
items,
total,
});
}, 1000);
});
},
getDesserts() {
return [
{
name: 'Frozen Yogurt',
calories: 159,
fat: 6.0,
carbs: 24,
protein: 4.0,
iron: '1%',
},
{
name: 'Ice cream sandwich',
calories: 237,
fat: 9.0,
carbs: 37,
protein: 4.3,
iron: '1%',
},
{
name: 'Eclair',
calories: 262,
fat: 16.0,
carbs: 23,
protein: 6.0,
iron: '7%',
},
];
},
},
});