This repository was archived by the owner on Jul 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathServerSideData.razor
More file actions
146 lines (127 loc) · 5.56 KB
/
Copy pathServerSideData.razor
File metadata and controls
146 lines (127 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
@page "/ServerSideData"
@inject HttpClient Http
@using BlazorTable
@using BlazorTable.Interfaces
@using BlazorTable.Components.ServerSide
<h1>Server side data</h1>
Selected: @(selectedItems.Any() ? selectedItems.Select(x => x.full_name).Aggregate((c, n) => $"{c},{n}") : "None")
<Table TableItem="PersonData" @ref="Table" DataLoader="_loader" Items="data" PageSize="15" ShowSearchBar="true" SelectionType="SelectionType.Multiple" SelectedItems="selectedItems" RowClickAction="RowClick">
<Column TableItem="PersonData" Title="Id" Field="@(x => x.id)" Sortable="true" Width="10%" DefaultSortColumn="true" Filterable="true" />
<Column TableItem="PersonData" Title="Full Name" Field="@(x => x.full_name)" Sortable="true" Width="20%" Filterable="true">
<CustomIFilters>
<CustomSelect TableItem="PersonData">
<CustomSelectOption Key="Astrix Mariette" Value="@("Astrix Mariette")" />
<CustomSelectOption Key="Marja Mustill" Value="@("Marja Mustill")" />
<CustomSelectOption Key="Creighton Keyzman" Value="@("Creighton Keyzman")" />
<CustomSelectOption Key="Jacobo Linton" Value="@("Jacobo Linton")" />
<CustomSelectOption Key="Michael O'Dyvoy" Value="@("Michael O'Dyvoy")" />
</CustomSelect>
</CustomIFilters>
</Column>
<Column TableItem="PersonData" Title="Email" Field="@(x => x.email)" Sortable="true" Width="20%">
<Template>
<a href="mailto:@context.email">@context.email</a>
</Template>
</Column>
<Column TableItem="PersonData" Title="Paid" Field="@(x => x.paid)" Sortable="true" Width="10%" Filterable="true">
<Template>
@context.paid.ToString()
</Template>
</Column>
<Column TableItem="PersonData" Title="Price" Field="@(x => x.price)" Sortable="true" Width="10%" Format="C" Align="Align.Right" />
<Column TableItem="PersonData" Title="Created Date" Field="@(x => x.created_date)" Sortable="true" Width="10%">
<Template>
@(context.created_date.HasValue ? context.created_date.Value.ToShortDateString() : string.Empty)
</Template>
</Column>
<Pager ShowPageNumber="true" ShowPageNumberSelector="true" ShowPageSizes="true" ShowPageNumberInput="true" />
</Table>
@code
{
[Inject]
private HttpClient httpClient { get; set; }
private ITable<PersonData> Table;
public class PersonData
{
public int? id { get; set; }
public string full_name { get; set; }
public string email { get; set; }
public bool? paid { get; set; }
public decimal? price { get; set; }
public DateTime? created_date { get; set; }
}
private IDataLoader<PersonData> _loader;
private IEnumerable<PersonData> data;
private List<PersonData> selectedItems = new List<PersonData>();
protected override async Task OnInitializedAsync()
{
_loader = new PersonDataLoader(httpClient);
data = (await _loader.LoadDataAsync(null)).Records;
await Table.SetInitialFiltersAsync(new List<FilterString>()
{
//new FilterString()
//{
// Condition = StringCondition.IsEqualTo.ToString(),
// Field = nameof(PersonData.full_name),
// FilterValue = "Marja Mustill"
//},
new FilterString()
{
Condition = BooleanCondition.True.ToString(),
Field = nameof(PersonData.paid)
}
});
}
public void RowClick(PersonData data)
{
StateHasChanged();
}
public class PersonDataLoader : IDataLoader<PersonData>
{
private readonly HttpClient _client;
public PersonDataLoader(HttpClient client)
{
_client = client;
}
public async Task<PaginationResult<PersonData>> LoadDataAsync(FilterData<PersonData> parameters)
{
var data = await _client.GetFromJsonAsync<PersonData[]>("sample-data/MOCK_DATA.json");
IQueryable<PersonData> query = data.AsQueryable();
if (parameters?.Query != null)
{
query = query.Where(
x => x.email.ToLowerInvariant().Contains(parameters.Query.ToLowerInvariant()) ||
x.full_name.ToLowerInvariant().Contains(parameters.Query.ToLowerInvariant()));
}
if (parameters?.OrderBy != null)
{
var orderBy = parameters.OrderBy.Split(" ");
if (orderBy.Length == 2)
{
var isSortDescending = orderBy[1] == "desc";
var prop = typeof(PersonData).GetProperty(orderBy[0]);
query = isSortDescending ? query.OrderByDescending(x => prop.GetValue(x, null))
: query.OrderBy(x => prop.GetValue(x, null));
}
}
if(parameters?.Filters != null)
{
foreach (var filter in parameters?.Filters)
{
query = query.Where(filter);
}
}
var results = parameters?.Top.HasValue ?? false ?
query.Skip(parameters.Skip.GetValueOrDefault())
.Take(parameters.Top.Value).ToArray() :
query.ToArray();
return new PaginationResult<PersonData>
{
Records = results,
Skip = parameters?.Skip ?? 0,
Total = query.ToList().Count,
Top = parameters?.Top ?? 0
};
}
}
}