|
1 |
| -<template> |
2 |
| - <div> |
3 |
| - <h1>Weather forecast</h1> |
4 |
| - |
5 |
| - <p>This component demonstrates fetching data from the server.</p> |
6 |
| - |
7 |
| - <div v-if="!forecasts" class="text-center"> |
8 |
| - <p><em>Loading...</em></p> |
9 |
| - <h1><icon icon="spinner" pulse/></h1> |
10 |
| - </div> |
11 |
| - |
12 |
| - <template v-if="forecasts"> |
13 |
| - <table class="table"> |
14 |
| - <thead class="bg-dark text-white"> |
15 |
| - <tr> |
16 |
| - <th>Date</th> |
17 |
| - <th>Temp. (C)</th> |
18 |
| - <th>Temp. (F)</th> |
19 |
| - <th>Summary</th> |
20 |
| - </tr> |
21 |
| - </thead> |
22 |
| - <tbody> |
23 |
| - <tr :class="index % 2 == 0 ? 'bg-white' : 'bg-light'" v-for="(forecast, index) in forecasts" :key="index"> |
24 |
| - <td>{{ forecast.dateFormatted }}</td> |
25 |
| - <td>{{ forecast.temperatureC }}</td> |
26 |
| - <td>{{ forecast.temperatureF }}</td> |
27 |
| - <td>{{ forecast.summary }}</td> |
28 |
| - </tr> |
29 |
| - </tbody> |
30 |
| - </table> |
31 |
| - <nav aria-label="..."> |
32 |
| - <ul class="pagination justify-content-center"> |
33 |
| - <li :class="'page-item' + (currentPage == 1 ? ' disabled' : '')"> |
34 |
| - <a class="page-link" href="#" tabindex="-1" @click="loadPage(currentPage - 1)">Previous</a> |
35 |
| - </li> |
36 |
| - <li :class="'page-item' + (n == currentPage ? ' active' : '')" v-for="(n, index) in totalPages" :key="index"> |
37 |
| - <a class="page-link" href="#" @click="loadPage(n)">{{n}}</a> |
38 |
| - </li> |
39 |
| - <li :class="'page-item' + (currentPage < totalPages ? '' : ' disabled')"> |
40 |
| - <a class="page-link" href="#" @click="loadPage(currentPage + 1)">Next</a> |
41 |
| - </li> |
42 |
| - </ul> |
43 |
| - </nav> |
44 |
| - </template> |
45 |
| - </div> |
46 |
| -</template> |
47 |
| - |
48 |
| -<script> |
49 |
| -export default { |
50 |
| - computed: { |
51 |
| - totalPages: function () { |
52 |
| - return Math.ceil(this.total / this.pageSize) |
53 |
| - } |
54 |
| - }, |
55 |
| -
|
56 |
| - data () { |
57 |
| - return { |
58 |
| - forecasts: null, |
59 |
| - total: 0, |
60 |
| - pageSize: 5, |
61 |
| - currentPage: 1 |
62 |
| - } |
63 |
| - }, |
64 |
| -
|
65 |
| - methods: { |
66 |
| - async loadPage (page) { |
67 |
| - // ES2017 async/await syntax via babel-plugin-transform-async-to-generator |
68 |
| - // TypeScript can also transpile async/await down to ES5 |
69 |
| - this.currentPage = page |
70 |
| -
|
71 |
| - try { |
72 |
| - var from = (page - 1) * (this.pageSize) |
73 |
| - var to = from + this.pageSize |
74 |
| - let response = await this.$http.get(`/api/weather/forecasts?from=${from}&to=${to}`) |
75 |
| - console.log(response.data.forecasts) |
76 |
| - this.forecasts = response.data.forecasts |
77 |
| - this.total = response.data.total |
78 |
| - } catch (err) { |
79 |
| - window.alert(err) |
80 |
| - console.log(err) |
81 |
| - } |
82 |
| - // Old promise-based approach |
83 |
| - // this.$http |
84 |
| - // .get('/api/SampleData/WeatherForecasts') |
85 |
| - // .then(response => { |
86 |
| - // console.log(response.data) |
87 |
| - // this.forecasts = response.data |
88 |
| - // }) |
89 |
| - // .catch((error) => console.log(error))*/ |
90 |
| - } |
91 |
| - }, |
92 |
| -
|
93 |
| - async created () { |
94 |
| - this.loadPage(1) |
95 |
| - } |
96 |
| -} |
97 |
| -</script> |
98 |
| - |
99 |
| -<style> |
100 |
| -</style> |
| 1 | +<template> |
| 2 | + <div> |
| 3 | + <h1>Weather forecast</h1> |
| 4 | + |
| 5 | + <p>This component demonstrates fetching data from the server.</p> |
| 6 | + |
| 7 | + <div v-if="!forecasts" class="text-center"> |
| 8 | + <p><em>Loading...</em></p> |
| 9 | + <h1><icon icon="spinner" pulse/></h1> |
| 10 | + </div> |
| 11 | + |
| 12 | + <template v-if="forecasts"> |
| 13 | + <table class="table"> |
| 14 | + <thead class="bg-dark text-white"> |
| 15 | + <tr> |
| 16 | + <th>Date</th> |
| 17 | + <th>Temp. (C)</th> |
| 18 | + <th>Temp. (F)</th> |
| 19 | + <th>Summary</th> |
| 20 | + </tr> |
| 21 | + </thead> |
| 22 | + <tbody> |
| 23 | + <tr :class="index % 2 == 0 ? 'bg-white' : 'bg-light'" v-for="(forecast, index) in forecasts" :key="index"> |
| 24 | + <td>{{ forecast.dateFormatted }}</td> |
| 25 | + <td>{{ forecast.temperatureC }}</td> |
| 26 | + <td>{{ forecast.temperatureF }}</td> |
| 27 | + <td>{{ forecast.summary }}</td> |
| 28 | + </tr> |
| 29 | + </tbody> |
| 30 | + </table> |
| 31 | + <nav aria-label="..."> |
| 32 | + <ul class="pagination justify-content-center"> |
| 33 | + <li :class="'page-item' + (currentPage == 1 ? ' disabled' : '')"> |
| 34 | + <a class="page-link" href="#" tabindex="-1" @click="loadPage(currentPage - 1)">Previous</a> |
| 35 | + </li> |
| 36 | + <li :class="'page-item' + (n == currentPage ? ' active' : '')" v-for="(n, index) in totalPages" :key="index"> |
| 37 | + <a class="page-link" href="#" @click="loadPage(n)">{{n}}</a> |
| 38 | + </li> |
| 39 | + <li :class="'page-item' + (currentPage < totalPages ? '' : ' disabled')"> |
| 40 | + <a class="page-link" href="#" @click="loadPage(currentPage + 1)">Next</a> |
| 41 | + </li> |
| 42 | + </ul> |
| 43 | + </nav> |
| 44 | + </template> |
| 45 | + </div> |
| 46 | +</template> |
| 47 | + |
| 48 | +<script> |
| 49 | +export default { |
| 50 | + computed: { |
| 51 | + totalPages: function () { |
| 52 | + return Math.ceil(this.total / this.pageSize) |
| 53 | + } |
| 54 | + }, |
| 55 | +
|
| 56 | + data () { |
| 57 | + return { |
| 58 | + forecasts: null, |
| 59 | + total: 0, |
| 60 | + pageSize: 5, |
| 61 | + currentPage: 1 |
| 62 | + } |
| 63 | + }, |
| 64 | +
|
| 65 | + methods: { |
| 66 | + async loadPage (page) { |
| 67 | + // ES2017 async/await syntax via babel-plugin-transform-async-to-generator |
| 68 | + // TypeScript can also transpile async/await down to ES5 |
| 69 | + this.currentPage = page |
| 70 | +
|
| 71 | + try { |
| 72 | + var from = (page - 1) * (this.pageSize) |
| 73 | + var to = from + this.pageSize |
| 74 | + let response = await this.$http.get(`/api/weather/forecasts?from=${from}&to=${to}`) |
| 75 | + console.log(response.data.forecasts) |
| 76 | + this.forecasts = response.data.forecasts |
| 77 | + this.total = response.data.total |
| 78 | + } catch (err) { |
| 79 | + window.alert(err) |
| 80 | + console.log(err) |
| 81 | + } |
| 82 | + // Old promise-based approach |
| 83 | + // this.$http |
| 84 | + // .get('/api/SampleData/WeatherForecasts') |
| 85 | + // .then(response => { |
| 86 | + // console.log(response.data) |
| 87 | + // this.forecasts = response.data |
| 88 | + // }) |
| 89 | + // .catch((error) => console.log(error))*/ |
| 90 | + } |
| 91 | + }, |
| 92 | +
|
| 93 | + async created () { |
| 94 | + this.loadPage(1) |
| 95 | + } |
| 96 | +} |
| 97 | +</script> |
| 98 | + |
| 99 | +<style> |
| 100 | +</style> |
0 commit comments