From 9535315dda7c5f1bd7a1b9110c1c81cf976d28f0 Mon Sep 17 00:00:00 2001 From: Peace Date: Fri, 28 Jun 2024 10:53:11 +0900 Subject: [PATCH] datagrid rest --- .../Components/Layout/NavMenu.razor | 6 ++ .../Components/Pages/DataGridREST.razor | 71 +++++++++++++++++++ .../ComponentPractice/Models/Customer.cs | 17 +++++ .../ComponentPractice/Program.cs | 2 + .../Services/NorthwindDataService.cs | 27 +++++++ 5 files changed, 123 insertions(+) create mode 100644 ComponentPractice/ComponentPractice/Components/Pages/DataGridREST.razor create mode 100644 ComponentPractice/ComponentPractice/Models/Customer.cs create mode 100644 ComponentPractice/ComponentPractice/Services/NorthwindDataService.cs diff --git a/ComponentPractice/ComponentPractice/Components/Layout/NavMenu.razor b/ComponentPractice/ComponentPractice/Components/Layout/NavMenu.razor index a9f1401..5ba6723 100644 --- a/ComponentPractice/ComponentPractice/Components/Layout/NavMenu.razor +++ b/ComponentPractice/ComponentPractice/Components/Layout/NavMenu.razor @@ -61,6 +61,12 @@   DataGrid Adv. + + diff --git a/ComponentPractice/ComponentPractice/Components/Pages/DataGridREST.razor b/ComponentPractice/ComponentPractice/Components/Pages/DataGridREST.razor new file mode 100644 index 0000000..65fa325 --- /dev/null +++ b/ComponentPractice/ComponentPractice/Components/Pages/DataGridREST.razor @@ -0,0 +1,71 @@ +@page "/datagridrest" +@using ComponentPractice.Services +@using System.Linq.Dynamic.Core + +@inject NorthwindDataService service; + +@rendermode RenderMode.InteractiveServer + + + DataGrid REST + + + + + + + + + + + + + + + + + + + +@code { + readonly int PAGE_SIZE = 10; + + RadzenDataGrid grid; + IEnumerable customers; + + int count; + bool isLoading = false; + + async Task Reset() + { + grid.Reset(true); + await grid.FirstPage(true); + } + + async Task LoadData(LoadDataArgs args) + { + isLoading = true; + + await Task.Yield(); + + var result = await service.GetCustomer(); + + customers = result.Value; + var query = customers.AsQueryable(); + + if (!string.IsNullOrEmpty(args.Filter)) + query = query.Where(args.Filter); + + if (!string.IsNullOrEmpty(args.OrderBy)) + query = query.OrderBy(args.OrderBy); + + count = query.Count(); + + customers = query.Skip(args.Skip.Value).Take(args.Top.Value).ToList(); + + isLoading = false; + } +} diff --git a/ComponentPractice/ComponentPractice/Models/Customer.cs b/ComponentPractice/ComponentPractice/Models/Customer.cs new file mode 100644 index 0000000..823ae3c --- /dev/null +++ b/ComponentPractice/ComponentPractice/Models/Customer.cs @@ -0,0 +1,17 @@ +namespace ComponentPractice.Models +{ + public class Customer + { + public string CustomerID { get; set; } + public string CompanyName { get; set; } + public string ContactName { get; set; } + public string ContactTitle { get; set; } + public string Address { get; set; } + public string City { get; set; } + public string Region { get; set; } + public string PostalCode { get; set; } + public string Country { get; set; } + public string Phone { get; set; } + public string Fax { get; set; } + } +} diff --git a/ComponentPractice/ComponentPractice/Program.cs b/ComponentPractice/ComponentPractice/Program.cs index 7f2c5ac..57c2d42 100644 --- a/ComponentPractice/ComponentPractice/Program.cs +++ b/ComponentPractice/ComponentPractice/Program.cs @@ -1,5 +1,6 @@ using ComponentPractice.Components; using ComponentPractice.Data; +using ComponentPractice.Services; using Radzen; namespace ComponentPractice @@ -17,6 +18,7 @@ namespace ComponentPractice builder.Services.AddRadzenComponents(); builder.Services.AddScoped(); + builder.Services.AddScoped(); var app = builder.Build(); diff --git a/ComponentPractice/ComponentPractice/Services/NorthwindDataService.cs b/ComponentPractice/ComponentPractice/Services/NorthwindDataService.cs new file mode 100644 index 0000000..0018549 --- /dev/null +++ b/ComponentPractice/ComponentPractice/Services/NorthwindDataService.cs @@ -0,0 +1,27 @@ +using ComponentPractice.Models; +using Radzen; + +namespace ComponentPractice.Services +{ + public class NorthwindDataService + { + private readonly HttpClient httpClient; + private readonly Uri baseUri; + + public NorthwindDataService(string url = "https://services.radzen.com/odata/Northwind/") + { + baseUri = new Uri(url); + httpClient = new HttpClient(); + } + + public async Task> GetCustomer() + { + var uri = new Uri(baseUri, "Customers"); + var httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, uri); + + var response = await httpClient.SendAsync(httpRequestMessage); + + return await response.ReadAsync>(); + } + } +}