Skip to content

Instantly share code, notes, and snippets.

@talkingdotnet
Created April 4, 2018 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save talkingdotnet/fc15f7eefb8e677da7395b51d9341016 to your computer and use it in GitHub Desktop.
Save talkingdotnet/fc15f7eefb8e677da7395b51d9341016 to your computer and use it in GitHub Desktop.
@page "/todo"
@using ASPNETBlazorCRUDApp.Shared
@using Microsoft.AspNetCore.Blazor.Browser.Interop
@inject HttpClient Http
<h1>ToDo List</h1>
<div>
<div class="row">
<div class="col-sm-1">
<p>Item:</p>
</div>
<div class="col-sm-4">
<input id="todoName" placeholder="Item Name" @bind(itemName)>
</div>
</div>
<br />
<div class="row">
<div class="col-sm-1">
<button class="btn btn-info" id="btnAdd" @onclick(async () => await AddToDo())>Add</button>
</div>
<div class="col-sm-2">
@if (todos != null && todos.Count > 0)
{
<button class="btn btn-danger" @onclick(async () => await DeleteAllToDos())>Delete All</button>
}
</div>
</div>
</div>
<br /><br />
@if (todos == null)
{
<p><em>Loading...</em></p>
}
else
{
@if (todos.Count > 0)
{
<table class='table table-striped table-bordered table-hover table-condensed' style="width:80%;">
<thead>
<tr>
<th style="width: 40%">Name</th>
<th style="width: 40%">Edit</th>
<th style="width: 20%">Delete</th>
</tr>
</thead>
<tbody>
@foreach (var todo in todos)
{
<tr>
<td>
<span id="spn_@todo.ID">@todo.Item</span>
<input id="txt_@todo.ID" @bind(UpdateItemName) style="display:none;">
</td>
<td>
<button id="btnEdit_@todo.ID" class="btn btn-primary" @onclick(() => EditToDo(todo.ID, todo.Item))>Edit</button>
<button id="btnUpdate_@todo.ID" style="display:none;" class="btn btn-success" @onclick(async () => await UpdateToDo(todo.ID))>Update</button>
<button id="btnCancel_@todo.ID" style="display:none;" class="btn btn-primary" @onclick(() => CancelToDo(todo.ID))>Cancel</button>
</td>
<td><button class="btn btn-danger" @onclick(async () => await DeleteToDo(todo.ID))>Delete</button></td>
</tr>
}
</tbody>
</table>
}
}
<script>
Blazor.registerFunction('ShowControl', (id, item, bShow) => {
if (bShow) {
var txtInput = document.getElementById("txt_" + id);
document.getElementById("spn_" + id).style.display = "none";
txtInput.style.display = "";
txtInput.value = item;
txtInput.focus();
document.getElementById("btnEdit_" + id).style.display = "none";
document.getElementById("btnUpdate_" + id).style.display = "";
document.getElementById("btnCancel_" + id).style.display = "";
}
else {
document.getElementById("spn_" + id).style.display = "";
document.getElementById("txt_" + id).style.display = "none";
document.getElementById("btnEdit_" + id).style.display = "";
document.getElementById("btnUpdate_" + id).style.display = "none";
document.getElementById("btnCancel_" + id).style.display = "none";
}
return true;
});
</script>
@functions {
string itemName;
string UpdateItemName;
IList<ToDoList> todos = new List<ToDoList>();
protected override async Task OnInitAsync()
{
await Refresh();
}
private async Task Refresh()
{
todos = await Http.GetJsonAsync<ToDoList[]>("/api/ToDo");
StateHasChanged();
}
private async Task AddToDo()
{
if (!string.IsNullOrEmpty(itemName))
{
await Http.SendJsonAsync(HttpMethod.Post, "/api/ToDo", new ToDoList
{
Item = itemName,
});
itemName = "";
await Refresh();
}
}
private async Task UpdateToDo(int id)
{
if (!string.IsNullOrEmpty(UpdateItemName))
{
await Http.SendJsonAsync(HttpMethod.Put, $"/api/ToDo/{id}", new ToDoList
{
ID = id,
Item = UpdateItemName,
});
await Refresh();
//UpdateItemName = "";
RegisteredFunction.Invoke<bool>("ShowControl", id.ToString(), "", false);
}
}
private async Task DeleteToDo(int id)
{
await Http.DeleteAsync($"/api/ToDo/{id}");
await Refresh();
}
private void EditToDo(int id, string itemName)
{
RegisteredFunction.Invoke<bool>("ShowControl", id.ToString(), itemName, true);
}
private void CancelToDo(int id)
{
RegisteredFunction.Invoke<bool>("ShowControl", id.ToString(), "", false);
}
private async Task DeleteAllToDos()
{
foreach (var c in todos)
{
await Http.DeleteAsync($"/api/ToDo/{c.ID}");
}
await Refresh();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment