Created
January 12, 2019 21:21
-
-
Save Mirch/92f07db384ebf5b9ce847d6539a955f6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@inject HttpClient Http | |
<div> | |
@if (posts == null) | |
{ | |
<p>Loading...</p> | |
} | |
else | |
{ | |
foreach (var post in posts) | |
{ | |
<Post Title="@post.Title"> | |
@post.Content | |
</Post> | |
} | |
} | |
</div> | |
@functions { | |
[Parameter] | |
private string Filter { get; set; } | |
IEnumerable<FeedElement> posts; | |
protected override async Task OnInitAsync() | |
{ | |
await Load(); | |
} | |
protected override async void OnParametersSet() | |
{ | |
await Load(); | |
this.StateHasChanged(); | |
} | |
private async Task Load() | |
{ | |
posts = await GetData(); | |
if (!string.IsNullOrEmpty(Filter)) | |
{ | |
string filter = Filter.ToLower(); | |
posts = posts.Where(p => p.Title.ToLower().Contains(filter) || p.Content.ToLower().Contains(filter)); | |
} | |
} | |
private async Task<IEnumerable<FeedElement>> GetData() | |
{ | |
var data = await Http.GetJsonAsync<IEnumerable<FeedElement>>("sample-data/feed.json"); | |
return data; | |
} | |
public class FeedElement | |
{ | |
public string Title { get; set; } | |
public string Content { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment