macenteno 3 years ago
parent e5847bbece
commit 5529457512

@ -0,0 +1,15 @@
namespace ProjetBlaser.Models
{
public class Item
{
public int Id { get; set; }
public string DisplayName { get; set; }
public string Name { get; set; }
public int StackSize { get; set; }
public int MaxDurability { get; set; }
public List<string> EnchantCategories { get; set; }
public List<string> RepairWith { get; set; }
public DateTime CreatedDate { get; set; }
public DateTime? UpdatedDate { get; set; }
}
}

@ -1,3 +1,28 @@
@page "/list"
@using ProjetBlaser.Models
<h3>List</h3>
<h3>List</h3>
<DataGrid TItem="Item"
Data="@items"
ReadData="@OnReadData"
TotalItems="@totalItem"
PageSize="10"
ShowPager
Responsive>
<DataGridColumn TItem="Item" Field="@nameof(Item.Id)" Caption="#" />
<DataGridColumn TItem="Item" Field="@nameof(Item.DisplayName)" Caption="Display name" />
<DataGridColumn TItem="Item" Field="@nameof(Item.StackSize)" Caption="Stack size" />
<DataGridColumn TItem="Item" Field="@nameof(Item.MaxDurability)" Caption="Maximum durability" />
<DataGridColumn TItem="Item" Field="@nameof(Item.EnchantCategories)" Caption="Enchant categories">
<DisplayTemplate>
@(string.Join(", ", ((Item)context).EnchantCategories))
</DisplayTemplate>
</DataGridColumn>
<DataGridColumn TItem="Item" Field="@nameof(Item.RepairWith)" Caption="Repair with">
<DisplayTemplate>
@(string.Join(", ", ((Item)context).RepairWith))
</DisplayTemplate>
</DataGridColumn>
<DataGridColumn TItem="Item" Field="@nameof(Item.CreatedDate)" Caption="Created date" DisplayFormat="{0:d}" DisplayFormatProvider="@System.Globalization.CultureInfo.GetCultureInfo("fr-FR")" />
</DataGrid>

@ -1,6 +1,60 @@
namespace ProjetBlaser.Pages
using Blazored.LocalStorage;
using Blazorise.DataGrid;
using Microsoft.AspNetCore.Components;
using ProjetBlaser.Models;
namespace ProjetBlaser.Pages
{
public partial class List
{
private List<Item> items;
private int totalItem;
[Inject]
public HttpClient Http { get; set; }
[Inject]
public ILocalStorageService LocalStorage { get; set; }
[Inject]
public NavigationManager NavigationManager { get; set; }
protected override async Task OnAfterRenderAsync(bool firstRender)
{
// Do not treat this action if is not the first render
if (!firstRender)
{
return;
}
var currentData = await LocalStorage.GetItemAsync<Item[]>("data");
// Check if data exist in the local storage
if (currentData == null)
{
// this code add in the local storage the fake data (we load the data sync for initialize the data before load the OnReadData method)
var originalData = Http.GetFromJsonAsync<Item[]>($"{NavigationManager.BaseUri}fake-data.json").Result;
await LocalStorage.SetItemAsync("data", originalData);
}
}
private async Task OnReadData(DataGridReadDataEventArgs<Item> e)
{
if (e.CancellationToken.IsCancellationRequested)
{
return;
}
// When you use a real API, we use this follow code
//var response = await Http.GetJsonAsync<Data[]>( $"http://my-api/api/data?page={e.Page}&pageSize={e.PageSize}" );
var response = (await LocalStorage.GetItemAsync<Item[]>("data")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList();
if (!e.CancellationToken.IsCancellationRequested)
{
totalItem = (await LocalStorage.GetItemAsync<List<Item>>("data")).Count;
items = new List<Item>(response); // an actual data for the current page
}
}
}
}

@ -28,5 +28,8 @@
</div>
<script src="_framework/blazor.server.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.4/css/all.css">
<link href="_content/Blazorise/blazorise.css" rel="stylesheet" />
<link href="_content/Blazorise.Bootstrap/blazorise.bootstrap.css" rel="stylesheet" />
</body>
</html>

@ -1,3 +1,7 @@
using Blazored.LocalStorage;
using Blazorise;
using Blazorise.Bootstrap;
using Blazorise.Icons.FontAwesome;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using ProjetBlaser.Data;
@ -8,7 +12,12 @@ var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddSingleton<WeatherForecastService>();
builder.Services.AddHttpClient();
builder.Services
.AddBlazorise()
.AddBootstrapProviders()
.AddFontAwesomeIcons()
.AddBlazoredLocalStorage();
var app = builder.Build();
// Configure the HTTP request pipeline.

@ -6,4 +6,11 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Blazored.LocalStorage" Version="4.2.0" />
<PackageReference Include="Blazorise.Bootstrap" Version="1.1.2" />
<PackageReference Include="Blazorise.DataGrid" Version="1.1.2" />
<PackageReference Include="Blazorise.Icons.FontAwesome" Version="1.1.2" />
</ItemGroup>
</Project>

@ -8,3 +8,4 @@
@using Microsoft.JSInterop
@using ProjetBlaser
@using ProjetBlaser.Shared
@using Blazorise.DataGrid

@ -0,0 +1,35 @@
[
{
"id": 1,
"displayname": "Furnafix",
"name": "furnafix",
"stacksize": 36,
"maxdurability": 32,
"enchantcategories": [ " " ],
"repairwith": [ " " ],
"createddate": "2019-02-16",
"updateddate": "2020-06-15"
},
{
"id": 2,
"displayname": "Deminimum",
"name": "deminimum",
"stacksize": 49,
"maxdurability": 46,
"enchantcategories": [ " " ],
"repairwith": [ " " ],
"createddate": "2020-06-14",
"updateddate": "2020-06-15"
},
{
"id": 3,
"displayname": "Minecraft",
"name": "minecraft",
"stacksize": 50,
"maxdurability": 40,
"enchantcategories": [ "armor" ],
"repairwith": [ "jungle_plants" ],
"createddate": "2020-06-15",
"updateddate": "2020-06-15"
}
]
Loading…
Cancel
Save