This repository has been archived by the owner on May 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathTodos.razor
81 lines (72 loc) · 2.28 KB
/
Todos.razor
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
@page "/todos"
@inject TodoContext Context
<h1>Todos</h1>
<BSForm>
<BSInput InputType="InputType.Text" @bind-Value="@Filter" placeholder="Type to filter" />
<BSButton Color="Color.Primary" @onclick="@onFilter">Filter</BSButton>
</BSForm>
<BSTable IsBordered="true" IsHovarable="true">
<thead>
<tr><th>Id</th><th>Is Done</th><th>Text</th></tr>
</thead>
<tbody>
@foreach (var todo in Filtered)
{
var color = todo.IsDone ? Color.Success : Color.Danger;
<BSTableRow Color="@color">
<td><BSButton Color="Color.Secondary" @onclick="@(() => onClickItem(todo.Id))">@todo.Id</BSButton></td>
<td>@todo.IsDone</td>
<td>@todo.Text</td>
</BSTableRow>
}
</tbody>
</BSTable>
<BSButton Color="Color.Primary" @onclick="@addNewClick">Add New</BSButton>
<h2>Selected Item</h2>
<TodoItemForm SelectedId="@SelectedId" NewTodoAdded="@NewTodoAdded" />
@functions {
string Filter { get; set; }
List<TodoItem> Filtered { get; set; } = new List<TodoItem>();
int SelectedId { get; set; } = 1;
protected async override Task OnInitAsync()
{
await Context.Initialize();
if (Context.Todos.Count == 0)
{
//Insert some initial data
Context.Todos.Add(new TodoItem { IsDone = true, Text = "Create initial BlazorDB project" });
Context.Todos.Add(new TodoItem { IsDone = true, Text = "Make a todo app" });
Context.Todos.Add(new TodoItem { IsDone = false, Text = "Make BlazorDB really awesome!" });
await Context.SaveChanges();
}
Filtered = Context.Todos.ToList<TodoItem>();
}
void onFilter()
{
if(Filter == String.Empty)
{
Filtered = Context.Todos.ToList<TodoItem>();
}
else
{
var query = from todo in Context.Todos
where todo.Text.ToLower().Contains(Filter.ToLower())
select todo;
Filtered = query.ToList();
}
}
void onClickItem(int i)
{
SelectedId = i;
StateHasChanged();
}
void addNewClick()
{
SelectedId = 0;
StateHasChanged();
}
void NewTodoAdded()
{
StateHasChanged();
}
}