1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
| <template> <div> <v-data-table :headers="headers" :items="desserts" :options.sync="options" :server-items-length="totalDesserts" :loading="loading" class="elevation-1" ></v-data-table> </div> </template> <script> export default { name: "HelloWorld", data() { return { totalDesserts: 0, desserts: [], loading: true, options: {}, headers: [ { text: "Dessert (100g serving)", align: "start", sortable: false, value: "name", }, { text: "Calories", value: "calories" }, { text: "Fat (g)", value: "fat" }, ], }; }, watch: { options: { async handler() { const { items, total } = await this.getDataFromApi(); this.desserts = items; this.totalDesserts = total; }, deep: true, }, }, async mounted() { const { items, total } = await this.getDataFromApi(); this.desserts = items; this.totalDesserts = total; }, methods: { getDataFromApi() { this.loading = true; return new Promise((resolve) => { const { sortBy, sortDesc, page, itemsPerPage } = this.options;
let items = this.getDesserts(); const total = items.length;
if (sortBy.length === 1 && 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(() => { this.loading = false; resolve({ items, total, }); }, 1000); }); }, getDesserts() { return [ { name: "Frozen Yogurt", calories: 159, fat: 6.0, }, { name: "Ice cream sandwich", calories: 237, fat: 9.0, }, { name: "Eclair", calories: 262, fat: 16.0, }, ]; }, }, }; </script>
|