|
| 1 | +@page "/ServerSideData" |
| 2 | +@inject HttpClient Http |
| 3 | +@using BlazorTable |
| 4 | +@using BlazorTable.Interfaces |
| 5 | +@using BlazorTable.Components.ServerSide |
| 6 | + |
| 7 | +<h1>Server side data</h1> |
| 8 | + |
| 9 | +<Table TableItem="PersonData" DataLoader="_loader" Items="data" PageSize="15" ShowSearchBar="true"> |
| 10 | + <Column TableItem="PersonData" Title="Id" Field="@(x => x.id)" Sortable="true" Width="10%" DefaultSortColumn="true" /> |
| 11 | + <Column TableItem="PersonData" Title="Full Name" Field="@(x => x.full_name)" Sortable="true" Width="20%" /> |
| 12 | + <Column TableItem="PersonData" Title="Email" Field="@(x => x.email)" Sortable="true" Width="20%"> |
| 13 | + <Template> |
| 14 | + <a href="mailto:@context.email">@context.email</a> |
| 15 | + </Template> |
| 16 | + </Column> |
| 17 | + <Column TableItem="PersonData" Title="Paid" Field="@(x => x.paid)" Sortable="true" Width="10%"> |
| 18 | + <Template> |
| 19 | + @context.paid.ToString() |
| 20 | + </Template> |
| 21 | + </Column> |
| 22 | + <Column TableItem="PersonData" Title="Price" Field="@(x => x.price)" Sortable="true" Width="10%" Format="C" Align="Align.Right" /> |
| 23 | + <Column TableItem="PersonData" Title="Created Date" Field="@(x => x.created_date)" Sortable="true" Width="10%"> |
| 24 | + <Template> |
| 25 | + @(context.created_date.HasValue ? context.created_date.Value.ToShortDateString() : string.Empty) |
| 26 | + </Template> |
| 27 | + </Column> |
| 28 | + <Pager ShowPageNumber="true" ShowTotalCount="true" /> |
| 29 | +</Table> |
| 30 | + |
| 31 | +@code |
| 32 | +{ |
| 33 | + [Inject] |
| 34 | + private HttpClient httpClient { get; set; } |
| 35 | + |
| 36 | + public class PersonData |
| 37 | + { |
| 38 | + public int? id { get; set; } |
| 39 | + public string full_name { get; set; } |
| 40 | + public string email { get; set; } |
| 41 | + public bool? paid { get; set; } |
| 42 | + public decimal? price { get; set; } |
| 43 | + public DateTime? created_date { get; set; } |
| 44 | + } |
| 45 | + |
| 46 | + private IDataLoader<PersonData> _loader; |
| 47 | + |
| 48 | + private IEnumerable<PersonData> data; |
| 49 | + |
| 50 | + protected override async Task OnInitializedAsync() |
| 51 | + { |
| 52 | + _loader = new PersonDataLoader(httpClient); |
| 53 | + data = (await _loader.LoadDataAsync(null)).Records; |
| 54 | + } |
| 55 | + |
| 56 | + public class PersonDataLoader : IDataLoader<PersonData> |
| 57 | + { |
| 58 | + private readonly HttpClient _client; |
| 59 | + public PersonDataLoader(HttpClient client) |
| 60 | + { |
| 61 | + _client = client; |
| 62 | + } |
| 63 | + public async Task<PaginationResult<PersonData>> LoadDataAsync(FilterData parameters) |
| 64 | + { |
| 65 | + |
| 66 | + var data = await _client.GetFromJsonAsync<PersonData[]>("sample-data/MOCK_DATA.json"); |
| 67 | + IQueryable<PersonData> query = data.AsQueryable(); |
| 68 | + if (parameters?.Query != null) |
| 69 | + { |
| 70 | + query = query.Where( |
| 71 | + x => x.email.ToLowerInvariant().Contains(parameters.Query.ToLowerInvariant()) || |
| 72 | + x.full_name.ToLowerInvariant().Contains(parameters.Query.ToLowerInvariant())); |
| 73 | + } |
| 74 | + if (parameters?.OrderBy != null) |
| 75 | + { |
| 76 | + var orderBy = parameters.OrderBy.Split(" "); |
| 77 | + if (orderBy.Length == 2) |
| 78 | + { |
| 79 | + var isSortDescending = orderBy[1] == "desc"; |
| 80 | + var prop = typeof(PersonData).GetProperty(orderBy[0]); |
| 81 | + query = isSortDescending ? query.OrderByDescending(x => prop.GetValue(x, null)) |
| 82 | + : query.OrderBy(x => prop.GetValue(x, null)); |
| 83 | + } |
| 84 | + } |
| 85 | + var results = parameters?.Top.HasValue ?? false ? |
| 86 | + query.Skip(parameters.Skip.GetValueOrDefault()) |
| 87 | + .Take(parameters.Top.Value).ToArray() : |
| 88 | + query.ToArray(); |
| 89 | + |
| 90 | + return new PaginationResult<PersonData> |
| 91 | + { |
| 92 | + Records = results, |
| 93 | + Skip = parameters?.Skip ?? 0, |
| 94 | + Total = query.ToList().Count, |
| 95 | + Top = parameters?.Top ?? 0 |
| 96 | + }; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | +} |
0 commit comments