|
14 | 14 | class="elevation-1" |
15 | 15 | > |
16 | 16 | <v-progress-linear v-slot:progress color="blue" indeterminate></v-progress-linear> |
17 | | - <template v-slot:items="props"> |
18 | | - <td>{{ props.item.dateFormatted }}</td> |
19 | | - <td>{{ props.item.temperatureC }}</td> |
20 | | - <td>{{ props.item.temperatureF }}</td> |
21 | | - <td>{{ props.item.summary }}</td> |
| 17 | + <template v-slot:item.date="{ item }"> |
| 18 | + <td>{{ item.date | date }}</td> |
| 19 | + </template> |
| 20 | + <template v-slot:item.temperatureC="{ item }"> |
| 21 | + <v-chip :color="getColor(item.temperatureC)" dark>{{ item.temperatureC }}</v-chip> |
22 | 22 | </template> |
23 | 23 | </v-data-table> |
24 | 24 | </v-col> |
@@ -56,27 +56,34 @@ export default class FetchDataView extends Vue { |
56 | 56 | private errorMessage: string = 'Error while loading weather forecast.'; |
57 | 57 | private forecasts: Forecast[] = []; |
58 | 58 | private headers = [ |
59 | | - { text: 'Date', value: 'dateFormatted' }, |
| 59 | + { text: 'Date', value: 'date' }, |
60 | 60 | { text: 'Temp. (C)', value: 'temperatureC' }, |
61 | 61 | { text: 'Temp. (F)', value: 'temperatureF' }, |
62 | 62 | { text: 'Summary', value: 'summary' }, |
63 | 63 | ]; |
64 | 64 |
|
65 | | - private created() { |
66 | | - this.fetchWeatherForecasts(); |
| 65 | + private getColor(temperature: number) { |
| 66 | + if (temperature < 0) { |
| 67 | + return 'blue'; |
| 68 | + } else if (temperature >= 0 && temperature < 30) { |
| 69 | + return 'green'; |
| 70 | + } else { |
| 71 | + return 'red'; |
| 72 | + } |
| 73 | + } |
| 74 | + private async created() { |
| 75 | + await this.fetchWeatherForecasts(); |
67 | 76 | } |
68 | 77 |
|
69 | | - private fetchWeatherForecasts() { |
70 | | - axios |
71 | | - .get<Forecast[]>('api/SampleData/WeatherForecasts') |
72 | | - .then((response) => { |
73 | | - this.forecasts = response.data; |
74 | | - }) |
75 | | - .catch((e) => { |
76 | | - this.showError = true; |
77 | | - this.errorMessage = `Error while loading weather forecast: ${e.message}.`; |
78 | | - }) |
79 | | - .finally(() => (this.loading = false)); |
| 78 | + private async fetchWeatherForecasts() { |
| 79 | + try { |
| 80 | + const response = await axios.get<Forecast[]>('api/WeatherForecast'); |
| 81 | + this.forecasts = response.data; |
| 82 | + } catch (e) { |
| 83 | + this.showError = true; |
| 84 | + this.errorMessage = `Error while loading weather forecast: ${e.message}.`; |
| 85 | + } |
| 86 | + this.loading = false; |
80 | 87 | } |
81 | 88 | } |
82 | 89 | </script> |
0 commit comments