Skip to content

Commit 2c11600

Browse files
committed
Updated sample
1 parent d187cb2 commit 2c11600

File tree

2 files changed

+92
-71
lines changed

2 files changed

+92
-71
lines changed
Lines changed: 6 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,14 @@
11
@page "/fetchdata/"
2-
@using BlazorUnitedApp.Data
3-
@inject WeatherForecastService ForecastService
4-
@inject NavigationManager NavigationManager
52
@rendermode InteractiveServer
3+
<FetchDataChildren YayOrNay="_yayOrNay"></FetchDataChildren>
64

7-
<PageTitle>Weather forecast</PageTitle>
8-
9-
<h1>Weather forecast</h1>
10-
11-
<p>This component demonstrates fetching data from a service.</p>
12-
13-
@if (Forecasts == null)
14-
{
15-
<p><em>Loading...</em></p>
16-
}
17-
else
18-
{
19-
<table class="table">
20-
<thead>
21-
<tr>
22-
<th>Date</th>
23-
<th>Temp. (C)</th>
24-
<th>Temp. (F)</th>
25-
<th>Summary</th>
26-
</tr>
27-
</thead>
28-
<tbody>
29-
@foreach (var forecast in Forecasts)
30-
{
31-
<tr>
32-
<td>@forecast.Date.ToShortDateString()</td>
33-
<td>@forecast.TemperatureC</td>
34-
<td>@forecast.TemperatureF</td>
35-
<td>@forecast.Summary</td>
36-
</tr>
37-
}
38-
</tbody>
39-
</table>
40-
}
41-
42-
<p>Current Url = @Page</p>
43-
44-
<ul>
45-
<li>
46-
<a href="fetchdata/?page=0">Page 0</a>
47-
</li>
48-
<li>
49-
<a href="fetchdata/?page=1">Page 1</a>
50-
</li>
51-
<li>
52-
<a href="fetchdata/?page=2">Page 2</a>
53-
</li>
54-
</ul>
5+
<button @onclick="UpdateParameter">Update parameter</button>
556

567
@code {
57-
[PersistentState]
58-
[UpdateStateOnEnhancedNavigation(true)]
59-
public WeatherForecast[]? Forecasts { get; set; }
60-
61-
public string Page { get; set; } = "";
62-
63-
protected override async Task OnInitializedAsync()
64-
{
65-
// Extract the page from the query string
66-
var uri = NavigationManager.ToAbsoluteUri(NavigationManager.Uri);
67-
var query = System.Web.HttpUtility.ParseQueryString(uri.Query);
68-
var pageParam = query["page"];
69-
int pageIndex = 1 + (int.TryParse(pageParam, out int parsedPage) ? parsedPage : 0);
70-
pageIndex *= 7;
71-
72-
Forecasts ??= await ForecastService.GetForecastAsync(DateOnly.FromDateTime(DateTime.Now).AddDays(pageIndex));
73-
}
74-
75-
protected override void OnParametersSet()
8+
private bool _yayOrNay = false;
9+
private void UpdateParameter()
7610
{
77-
Page = NavigationManager.Uri;
11+
// Logic to update the YayOrNay parameter
12+
_yayOrNay = !_yayOrNay;
7813
}
7914
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
@using BlazorUnitedApp.Data
2+
@inject WeatherForecastService ForecastService
3+
@inject NavigationManager NavigationManager
4+
5+
<h1>Weather forecast</h1>
6+
7+
<p>This component demonstrates fetching data from a service.</p>
8+
9+
@if (Forecasts == null)
10+
{
11+
<p><em>Loading...</em></p>
12+
}
13+
else
14+
{
15+
<table class="table">
16+
<thead>
17+
<tr>
18+
<th>Date</th>
19+
<th>Temp. (C)</th>
20+
<th>Temp. (F)</th>
21+
<th>Summary</th>
22+
</tr>
23+
</thead>
24+
<tbody>
25+
@foreach (var forecast in Forecasts)
26+
{
27+
<tr>
28+
<td>@forecast.Date.ToShortDateString()</td>
29+
<td>@forecast.TemperatureC</td>
30+
<td>@forecast.TemperatureF</td>
31+
<td>@forecast.Summary</td>
32+
</tr>
33+
}
34+
</tbody>
35+
</table>
36+
}
37+
38+
<button @onclick="ClearData">Clear data</button>
39+
40+
<p>Current Url = @Page</p>
41+
42+
<p>Yay or Nay: @YayOrNay</p>
43+
44+
<ul>
45+
<li>
46+
<a href="fetchdata/?page=0">Page 0</a>
47+
</li>
48+
<li>
49+
<a href="fetchdata/?page=1">Page 1</a>
50+
</li>
51+
<li>
52+
<a href="fetchdata/?page=2">Page 2</a>
53+
</li>
54+
</ul>
55+
56+
@code {
57+
[PersistentState]
58+
[UpdateStateOnEnhancedNavigation(true)]
59+
public WeatherForecast[]? Forecasts { get; set; }
60+
61+
[Parameter] public bool YayOrNay { get; set; } = false;
62+
63+
public string Page { get; set; } = "";
64+
65+
protected override async Task OnInitializedAsync()
66+
{
67+
// Extract the page from the query string
68+
var uri = NavigationManager.ToAbsoluteUri(NavigationManager.Uri);
69+
var query = System.Web.HttpUtility.ParseQueryString(uri.Query);
70+
var pageParam = query["page"];
71+
int pageIndex = 1 + (int.TryParse(pageParam, out int parsedPage) ? parsedPage : 0);
72+
pageIndex *= 7;
73+
74+
Forecasts ??= await ForecastService.GetForecastAsync(DateOnly.FromDateTime(DateTime.Now).AddDays(pageIndex));
75+
}
76+
77+
protected override void OnParametersSet()
78+
{
79+
Page = NavigationManager.Uri;
80+
}
81+
82+
private void ClearData()
83+
{
84+
Forecasts = [];
85+
}
86+
}

0 commit comments

Comments
 (0)