From 6bf353c8e292fe3eeef33964bac0da9053004874 Mon Sep 17 00:00:00 2001 From: Gustavo Navarro Date: Thu, 21 Nov 2019 22:01:04 -0600 Subject: [PATCH] Blazor WebAssembly support --- GridBlazor.Tests/GridBlazor.Tests.csproj | 2 +- GridBlazor/CGrid.cs | 6 +- GridBlazor/Columns/GridColumnBase.cs | 14 +- GridBlazor/GridBlazor.csproj | 4 +- GridBlazor/GridComponentBase.cs | 40 ++++- GridBlazor/GridCreateComponent.razor | 140 ++++++++---------- GridBlazor/GridUpdateComponent.razor | 138 ++++++++--------- .../ColumnCollections/ColumnCollections.cs | 126 ++++++++-------- GridBlazorClientSide.Client/Pages/Crud.razor | 7 +- .../Pages/CustomCrud.razor | 7 +- .../Services/OrderService.cs | 54 +------ GridBlazorClientSide.Client/Startup.cs | 4 +- .../Controllers/SampleDataController.cs | 9 +- .../GridBlazorClientSide.Server.csproj | 4 +- .../ColumnCollections/ColumnCollections.cs | 55 ++++--- .../GridBlazorServerSide.csproj | 4 +- .../Services/CustomerService.cs | 6 +- .../Services/EmployeeService.cs | 8 +- .../Services/ShipperService.cs | 8 +- GridMvc.Demo/GridMvc.Demo.csproj | 4 +- GridMvc.Tests/GridMvc.Tests.csproj | 2 +- GridMvc/Columns/GridColumnBase.cs | 13 +- GridShared/Columns/IGridColumn.cs | 13 +- GridShared/GridShared.csproj | 2 +- GridShared/SelectItem.cs | 18 +++ 25 files changed, 346 insertions(+), 342 deletions(-) create mode 100644 GridShared/SelectItem.cs diff --git a/GridBlazor.Tests/GridBlazor.Tests.csproj b/GridBlazor.Tests/GridBlazor.Tests.csproj index 3d243af1..4838562d 100644 --- a/GridBlazor.Tests/GridBlazor.Tests.csproj +++ b/GridBlazor.Tests/GridBlazor.Tests.csproj @@ -6,7 +6,7 @@ - + diff --git a/GridBlazor/CGrid.cs b/GridBlazor/CGrid.cs index fb0cfc57..16dbd3cb 100644 --- a/GridBlazor/CGrid.cs +++ b/GridBlazor/CGrid.cs @@ -697,8 +697,10 @@ public async Task UpdateGrid() string urlParameters = ((GridPager)_pager).GetLink(); if (Url.Contains("?")) urlParameters = urlParameters.Replace("?", "&"); - HttpClient httpClient = new HttpClient(); - response = await httpClient.GetJsonAsync>(Url + urlParameters); + using (HttpClient httpClient = new HttpClient()) + { + response = await httpClient.GetJsonAsync>(Url + urlParameters); + } } else { diff --git a/GridBlazor/Columns/GridColumnBase.cs b/GridBlazor/Columns/GridColumnBase.cs index f49cb184..f92a0ed7 100644 --- a/GridBlazor/Columns/GridColumnBase.cs +++ b/GridBlazor/Columns/GridColumnBase.cs @@ -47,7 +47,9 @@ public abstract class GridColumnBase : GridStyledColumn, IGridColumn, ICGr public bool IsPrimaryKey { get; protected set; } = false; - public (bool IsSelectKey, Func Expression, Func>> KeyValues) IsSelectField { get; protected set; } = (false, null, null); + public (bool IsSelectKey, Func Expression, string Url, Func> SelectItemExpr) IsSelectField { get; protected set; } = (false, null, null, null); + + public IEnumerable SelectItems { get; internal set; } public bool IsSumEnabled { get; internal set; } = false; @@ -191,9 +193,15 @@ public IGridColumn SetPrimaryKey(bool enabled) return this; } - public IGridColumn SetSelectField(bool enabled, Func expression, Func>> keyValues) + public IGridColumn SetSelectField(bool enabled, Func expression, Func> selectItemExpr) + { + IsSelectField = (enabled, expression, null, selectItemExpr); + return this; + } + + public IGridColumn SetSelectField(bool enabled, Func expression, string url) { - IsSelectField = (enabled, expression, keyValues); + IsSelectField = (enabled, expression, url, null); return this; } diff --git a/GridBlazor/GridBlazor.csproj b/GridBlazor/GridBlazor.csproj index b0b7562a..c6f4c06e 100644 --- a/GridBlazor/GridBlazor.csproj +++ b/GridBlazor/GridBlazor.csproj @@ -27,8 +27,8 @@ - - + + diff --git a/GridBlazor/GridComponentBase.cs b/GridBlazor/GridComponentBase.cs index fabaf469..4c1dc8f0 100644 --- a/GridBlazor/GridComponentBase.cs +++ b/GridBlazor/GridComponentBase.cs @@ -8,7 +8,9 @@ using GridShared.Utility; using Microsoft.AspNetCore.Components; using System; +using System.Collections.Generic; using System.Linq; +using System.Net.Http; using System.Threading.Tasks; namespace GridBlazor @@ -31,6 +33,9 @@ public class GridComponentBase : ComponentBase protected RenderFragment CrudRender { get; set; } + [Inject] + public NavigationManager NavigationManager { get; set; } + [Parameter] public ICGrid Grid { get; set; } @@ -173,8 +178,9 @@ public async Task RemoveExtSorting(ColumnOrderValue column) await UpdateGrid(); } - public void CreateHandler() + public async Task CreateHandler() { + await SetSelectFields(); _item = (T)Activator.CreateInstance(typeof(T)); ((CGrid)Grid).Mode = GridMode.Create; if (Grid.CreateComponent != null) @@ -197,6 +203,7 @@ public void ReadHandler(object item) public async Task UpdateHandler(object item) { + await SetSelectFields(); var keys = Grid.GetPrimaryKeyValues(item); try { @@ -226,6 +233,37 @@ public void DeleteHandler(object item) StateHasChanged(); } + protected async Task SetSelectFields() + { + foreach (var column in Grid.Columns) + { + var isSelectField = ((IGridColumn)column).IsSelectField; + if (isSelectField.IsSelectKey) + { + try + { + if (isSelectField.SelectItemExpr == null) + { + using (HttpClient httpClient = new HttpClient()) + { + var selectItems = await httpClient.GetJsonAsync(NavigationManager.BaseUri + isSelectField.Url); + ((GridColumnBase)column).SelectItems = selectItems.ToList(); + } + } + else + { + ((GridColumnBase)column).SelectItems = isSelectField.SelectItemExpr.Invoke(); + } + } + catch (Exception e) + { + Console.WriteLine(e.Message); + ((GridColumnBase)column).SelectItems = new List(); + } + } + } + } + protected RenderFragment CreateCrudComponent() => builder => { var componentType = Grid.CreateComponent; diff --git a/GridBlazor/GridCreateComponent.razor b/GridBlazor/GridCreateComponent.razor index d046dae3..022402b8 100644 --- a/GridBlazor/GridCreateComponent.razor +++ b/GridBlazor/GridCreateComponent.razor @@ -17,31 +17,21 @@
@if (((IGridColumn)column).IsSelectField.IsSelectKey) - { - IEnumerable> keyValues; - try - { - keyValues = ((IGridColumn)column).IsSelectField.KeyValues.Invoke(); - } - catch (Exception e) - { - Console.WriteLine(e.Message); - keyValues = new List>(); - } + { var selectedValue = column.GetFormatedValue(value); if (type == typeof(string)) { @@ -50,15 +40,15 @@ { @@ -67,15 +57,15 @@ { @@ -84,15 +74,15 @@ { @@ -101,15 +91,15 @@ { @@ -118,15 +108,15 @@ { @@ -135,15 +125,15 @@ { @@ -152,15 +142,15 @@ { @@ -169,15 +159,15 @@ { @@ -186,15 +176,15 @@ { @@ -203,15 +193,15 @@ { @@ -220,15 +210,15 @@ { @@ -237,15 +227,15 @@ { @@ -254,15 +244,15 @@ { @@ -273,15 +263,15 @@ { @@ -290,15 +280,15 @@ { diff --git a/GridBlazor/GridUpdateComponent.razor b/GridBlazor/GridUpdateComponent.razor index 87066696..86b0aa9a 100644 --- a/GridBlazor/GridUpdateComponent.razor +++ b/GridBlazor/GridUpdateComponent.razor @@ -18,30 +18,20 @@
@if (((IGridColumn)column).IsSelectField.IsSelectKey) { - IEnumerable> keyValues; - try - { - keyValues = ((IGridColumn)column).IsSelectField.KeyValues.Invoke(); - } - catch (Exception e) - { - Console.WriteLine(e.Message); - keyValues = new List>(); - } var selectedValue = column.GetFormatedValue(value); if (type == typeof(string)) { @@ -50,15 +40,15 @@ { @@ -67,15 +57,15 @@ { @@ -84,15 +74,15 @@ { @@ -101,15 +91,15 @@ { @@ -118,15 +108,15 @@ { @@ -135,15 +125,15 @@ { @@ -152,15 +142,15 @@ { @@ -169,15 +159,15 @@ { @@ -186,15 +176,15 @@ { @@ -203,15 +193,15 @@ { @@ -220,15 +210,15 @@ { @@ -237,15 +227,15 @@ { @@ -254,15 +244,15 @@ { @@ -273,15 +263,15 @@ { @@ -290,15 +280,15 @@ { diff --git a/GridBlazorClientSide.Client/ColumnCollections/ColumnCollections.cs b/GridBlazorClientSide.Client/ColumnCollections/ColumnCollections.cs index 749cd476..14e0718b 100644 --- a/GridBlazorClientSide.Client/ColumnCollections/ColumnCollections.cs +++ b/GridBlazorClientSide.Client/ColumnCollections/ColumnCollections.cs @@ -152,93 +152,91 @@ public class ColumnCollections .RenderValueAs(o => o.Customer.IsVip ? "Yes" : "No"); }; - public static Action, Func>>, - Func>>, Func>>> - OrderColumnsWithCrud = (c, f, g, h) => - { - /* Adding "OrderID" column: */ - c.Add(o => o.OrderID).SetPrimaryKey(true).Titled(SharedResource.Number).SetWidth(100); + public static Action> OrderColumnsWithCrud = c => + { + /* Adding "OrderID" column: */ + c.Add(o => o.OrderID).SetPrimaryKey(true).Titled(SharedResource.Number).SetWidth(100); - /* Adding "CustomerID" column: */ - c.Add(o => o.CustomerID, true).SetSelectField(true, o => o.Customer.CustomerID + " - " + o.Customer.CompanyName, f); + /* Adding "CustomerID" column: */ + c.Add(o => o.CustomerID, true).SetSelectField(true, o => o.Customer.CustomerID + " - " + o.Customer.CompanyName, $"api/SampleData/GetAllCustomers"); - /* Adding "EmployeeID" column: */ - c.Add(o => o.EmployeeID, true).SetSelectField(true, o => o.Employee.EmployeeID.ToString() + " - " + o.Employee.FirstName + " " + o.Employee.LastName, g); + /* Adding "EmployeeID" column: */ + c.Add(o => o.EmployeeID, true).SetSelectField(true, o => o.Employee.EmployeeID.ToString() + " - " + o.Employee.FirstName + " " + o.Employee.LastName, $"api/SampleData/GetAllEmployees"); - /* Adding "ShipVia" column: */ - c.Add(o => o.ShipVia, true).SetSelectField(true, o => o.Shipper == null ? "" : o.Shipper.ShipperID.ToString() + " - " + o.Shipper.CompanyName, h); + /* Adding "ShipVia" column: */ + c.Add(o => o.ShipVia, true).SetSelectField(true, o => o.Shipper == null ? "" : o.Shipper.ShipperID.ToString() + " - " + o.Shipper.CompanyName, $"api/SampleData/GetAllShippers"); - /* Adding "OrderDate" column: */ - c.Add(o => o.OrderDate, "OrderCustomDate").Titled(SharedResource.OrderCustomDate) - .Format("{0:yyyy-MM-dd}").SetWidth(120); + /* Adding "OrderDate" column: */ + c.Add(o => o.OrderDate, "OrderCustomDate").Titled(SharedResource.OrderCustomDate) + .Format("{0:yyyy-MM-dd}").SetWidth(120); - /* Adding "CompanyName" column: */ - c.Add(o => o.Customer.CompanyName).Titled(SharedResource.CompanyName) - .SetWidth(250).SetCrudHidden(true); + /* Adding "CompanyName" column: */ + c.Add(o => o.Customer.CompanyName).Titled(SharedResource.CompanyName) + .SetWidth(250).SetCrudHidden(true); - /* Adding "ContactName" column: */ - c.Add(o => o.Customer.ContactName).Titled(SharedResource.ContactName).SetWidth(250).SetCrudHidden(true); + /* Adding "ContactName" column: */ + c.Add(o => o.Customer.ContactName).Titled(SharedResource.ContactName).SetWidth(250).SetCrudHidden(true); - /* Adding "Freight" column: */ - c.Add(o => o.Freight) - .Titled(SharedResource.Freight) - .Format("{0:F}"); + /* Adding "Freight" column: */ + c.Add(o => o.Freight) + .Titled(SharedResource.Freight) + .Format("{0:F}"); - /* Adding "Vip customer" column: */ - c.Add(o => o.Customer.IsVip).Titled(SharedResource.IsVip).SetWidth(70).Css("hidden-xs") //hide on phones - .RenderValueAs(o => o.Customer.IsVip ? "Yes" : "No").SetCrudHidden(true); + /* Adding "Vip customer" column: */ + c.Add(o => o.Customer.IsVip).Titled(SharedResource.IsVip).SetWidth(70).Css("hidden-xs") //hide on phones + .RenderValueAs(o => o.Customer.IsVip ? "Yes" : "No").SetCrudHidden(true); - /* Adding hidden "RequiredDate" column: */ - c.Add(o => o.RequiredDate, true).Format("{0:yyyy-MM-dd}"); + /* Adding hidden "RequiredDate" column: */ + c.Add(o => o.RequiredDate, true).Format("{0:yyyy-MM-dd}"); - /* Adding hidden "ShippedDate" column: */ - c.Add(o => o.ShippedDate, true).Format("{0:yyyy-MM-dd}"); + /* Adding hidden "ShippedDate" column: */ + c.Add(o => o.ShippedDate, true).Format("{0:yyyy-MM-dd}"); - /* Adding hidden "ShipName" column: */ - c.Add(o => o.ShipName, true); + /* Adding hidden "ShipName" column: */ + c.Add(o => o.ShipName, true); - /* Adding hidden "ShipAddress" column: */ - c.Add(o => o.ShipAddress, true); + /* Adding hidden "ShipAddress" column: */ + c.Add(o => o.ShipAddress, true); - /* Adding hidden "ShipCity" column: */ - c.Add(o => o.ShipCity, true); + /* Adding hidden "ShipCity" column: */ + c.Add(o => o.ShipCity, true); - /* Adding hidden "ShipPostalCode" column: */ - c.Add(o => o.ShipPostalCode, true); + /* Adding hidden "ShipPostalCode" column: */ + c.Add(o => o.ShipPostalCode, true); - /* Adding hidden "ShipRegion" column: */ - c.Add(o => o.ShipRegion, true); + /* Adding hidden "ShipRegion" column: */ + c.Add(o => o.ShipRegion, true); - /* Adding hidden "ShipCountry" column: */ - c.Add(o => o.ShipCountry, true); - }; + /* Adding hidden "ShipCountry" column: */ + c.Add(o => o.ShipCountry, true); + }; - public static Action> OrderColumnsWithCustomCrud = (c) => - { - /* Adding "OrderID" column: */ - c.Add(o => o.OrderID).SetPrimaryKey(true).Titled(SharedResource.Number).SetWidth(100); + public static Action> OrderColumnsWithCustomCrud = c => + { + /* Adding "OrderID" column: */ + c.Add(o => o.OrderID).SetPrimaryKey(true).Titled(SharedResource.Number).SetWidth(100); - /* Adding "OrderDate" column: */ - c.Add(o => o.OrderDate, "OrderCustomDate").Titled(SharedResource.OrderCustomDate) - .Format("{0:yyyy-MM-dd}").SetWidth(120); + /* Adding "OrderDate" column: */ + c.Add(o => o.OrderDate, "OrderCustomDate").Titled(SharedResource.OrderCustomDate) + .Format("{0:yyyy-MM-dd}").SetWidth(120); - /* Adding "CompanyName" column: */ - c.Add(o => o.Customer.CompanyName).Titled(SharedResource.CompanyName) - .SetWidth(250).SetCrudHidden(true); + /* Adding "CompanyName" column: */ + c.Add(o => o.Customer.CompanyName).Titled(SharedResource.CompanyName) + .SetWidth(250).SetCrudHidden(true); - /* Adding "ContactName" column: */ - c.Add(o => o.Customer.ContactName).Titled(SharedResource.ContactName).SetWidth(250).SetCrudHidden(true); + /* Adding "ContactName" column: */ + c.Add(o => o.Customer.ContactName).Titled(SharedResource.ContactName).SetWidth(250).SetCrudHidden(true); - /* Adding "Freight" column: */ - c.Add(o => o.Freight) - .Titled(SharedResource.Freight) - .Format("{0:F}"); + /* Adding "Freight" column: */ + c.Add(o => o.Freight) + .Titled(SharedResource.Freight) + .Format("{0:F}"); - /* Adding "Vip customer" column: */ - c.Add(o => o.Customer.IsVip).Titled(SharedResource.IsVip).SetWidth(70).Css("hidden-xs") //hide on phones - .RenderValueAs(o => o.Customer.IsVip ? "Yes" : "No").SetCrudHidden(true); - }; + /* Adding "Vip customer" column: */ + c.Add(o => o.Customer.IsVip).Titled(SharedResource.IsVip).SetWidth(70).Css("hidden-xs") //hide on phones + .RenderValueAs(o => o.Customer.IsVip ? "Yes" : "No").SetCrudHidden(true); + }; public static Action> OrderColumnsWithSubgrids = c => { diff --git a/GridBlazorClientSide.Client/Pages/Crud.razor b/GridBlazorClientSide.Client/Pages/Crud.razor index cf838418..941c8f39 100644 --- a/GridBlazorClientSide.Client/Pages/Crud.razor +++ b/GridBlazorClientSide.Client/Pages/Crud.razor @@ -1,7 +1,6 @@ @page "/crud" @using GridBlazor @using GridBlazorClientSide.Client.ColumnCollections -@using GridBlazorClientSide.Client.Services @using GridBlazorClientSide.Shared.Models @using GridShared @using GridShared.Utility @@ -9,7 +8,7 @@ @using System.Globalization @using System.Threading.Tasks @inject NavigationManager NavigationManager -@inject IOrderService orderService +@inject ICrudDataService orderService

Grid Sample

@@ -46,9 +45,7 @@ else var query = new QueryDictionary(); string url = NavigationManager.BaseUri + "api/SampleData/OrderColumnsWithCrud"; - Action> columns = c => ColumnCollections.OrderColumnsWithCrud(c, - orderService.GetAllCustomers, orderService.GetAllEmployees, orderService.GetAllShippers); - var client = new GridClient(url, query, false, "ordersGrid", columns, locale) + var client = new GridClient(url, query, false, "ordersGrid", ColumnCollections.OrderColumnsWithCrud, locale) .Sortable() .Filterable() .Crud(true, orderService) diff --git a/GridBlazorClientSide.Client/Pages/CustomCrud.razor b/GridBlazorClientSide.Client/Pages/CustomCrud.razor index 5eb9d41a..d6e1e9fc 100644 --- a/GridBlazorClientSide.Client/Pages/CustomCrud.razor +++ b/GridBlazorClientSide.Client/Pages/CustomCrud.razor @@ -1,7 +1,6 @@ @page "/customcrud" @using GridBlazor @using GridBlazorClientSide.Client.ColumnCollections -@using GridBlazorClientSide.Client.Services @using GridBlazorClientSide.Shared.Models @using GridShared @using GridShared.Utility @@ -9,7 +8,7 @@ @using System.Globalization @using System.Threading.Tasks @inject NavigationManager NavigationManager -@inject IOrderService orderService +@inject ICrudDataService orderService

Grid Sample

@@ -46,8 +45,8 @@ else var query = new QueryDictionary(); string url = NavigationManager.BaseUri + "api/SampleData/OrderColumnsWithCrud"; - Action> columns = c => ColumnCollections.OrderColumnsWithCustomCrud(c); - var client = new GridClient(url, query, false, "ordersGrid", columns, locale) + var client = new GridClient(url, query, false, "ordersGrid", ColumnCollections.OrderColumnsWithCustomCrud, + locale) .Sortable() .Filterable() .Crud(true, orderService) diff --git a/GridBlazorClientSide.Client/Services/OrderService.cs b/GridBlazorClientSide.Client/Services/OrderService.cs index 3ed2e5ce..5012c4ad 100644 --- a/GridBlazorClientSide.Client/Services/OrderService.cs +++ b/GridBlazorClientSide.Client/Services/OrderService.cs @@ -1,15 +1,12 @@ using GridBlazorClientSide.Shared.Models; using GridShared; using Microsoft.AspNetCore.Components; -using System; -using System.Collections.Generic; -using System.Linq; using System.Net.Http; using System.Threading.Tasks; namespace GridBlazorClientSide.Client.Services { - public class OrderService : IOrderService + public class OrderService : ICrudDataService { private readonly HttpClient _httpClient; private readonly string _baseUri; @@ -43,54 +40,5 @@ public async Task Delete(params object[] keys) int.TryParse(keys[0].ToString(), out orderId); await _httpClient.SendJsonAsync(HttpMethod.Delete, _baseUri + $"api/Order/{orderId}", null); } - - public IList> GetAllCustomers() - { - try - { - var customers = _httpClient.GetJsonAsync[]>(_baseUri + $"api/SampleData/GetAllCustomers").Result; - return customers.ToList(); - } - catch (Exception e) - { - Console.WriteLine(e.Message); - return new List>(); - } - } - - public IList> GetAllEmployees() - { - try - { - var employees = _httpClient.GetJsonAsync[]>(_baseUri + $"api/SampleData/GetAllEmployees").Result; - return employees.ToList(); - } - catch (Exception e) - { - Console.WriteLine(e.Message); - return new List>(); - } - } - - public IList> GetAllShippers() - { - try - { - var shippers = _httpClient.GetJsonAsync[]>(_baseUri + $"api/SampleData/GetAllShippers").Result; - return shippers.ToList(); - } - catch (Exception e) - { - Console.WriteLine(e.Message); - return new List>(); - } - } - } - - public interface IOrderService : ICrudDataService - { - IList> GetAllCustomers(); - IList> GetAllEmployees(); - IList> GetAllShippers(); } } diff --git a/GridBlazorClientSide.Client/Startup.cs b/GridBlazorClientSide.Client/Startup.cs index a8be01bd..c7d773b6 100644 --- a/GridBlazorClientSide.Client/Startup.cs +++ b/GridBlazorClientSide.Client/Startup.cs @@ -1,4 +1,6 @@ using GridBlazorClientSide.Client.Services; +using GridBlazorClientSide.Shared.Models; +using GridShared; using Microsoft.AspNetCore.Components.Builder; using Microsoft.Extensions.DependencyInjection; @@ -11,7 +13,7 @@ public class Startup public void ConfigureServices(IServiceCollection services) { - services.AddScoped(); + services.AddScoped, OrderService>(); } public void Configure(IComponentsApplicationBuilder app) diff --git a/GridBlazorClientSide.Server/Controllers/SampleDataController.cs b/GridBlazorClientSide.Server/Controllers/SampleDataController.cs index a487999e..94fbab56 100644 --- a/GridBlazorClientSide.Server/Controllers/SampleDataController.cs +++ b/GridBlazorClientSide.Server/Controllers/SampleDataController.cs @@ -2,6 +2,7 @@ using GridBlazorClientSide.Server.Models; using GridBlazorClientSide.Shared.Models; using GridMvc.Server; +using GridShared; using Microsoft.AspNetCore.Mvc; using System; using System.Linq; @@ -121,7 +122,7 @@ public ActionResult OrderColumnsWithCrud() { var repository = new OrdersRepository(_context); IGridServer server = new GridServer(repository.GetAll(), Request.Query, - true, "ordersGrid", c => ColumnCollections.OrderColumnsWithCrud(c, null, null, null)) + true, "ordersGrid", ColumnCollections.OrderColumnsWithCrud) .WithPaging(10) .Sortable() .Filterable() @@ -161,7 +162,7 @@ public ActionResult GetAllCustomers() { var repository = new CustomersRepository(_context); return Ok(repository.GetAll() - .Select(r => new Tuple(r.CustomerID, r.CustomerID + " - " + r.CompanyName)) + .Select(r => new SelectItem(r.CustomerID, r.CustomerID + " - " + r.CompanyName)) .ToList()); } @@ -170,7 +171,7 @@ public ActionResult GetAllEmployees() { var repository = new EmployeeRepository(_context); return Ok(repository.GetAll() - .Select(r => new Tuple(r.EmployeeID.ToString(), r.EmployeeID.ToString() + " - " + .Select(r => new SelectItem(r.EmployeeID.ToString(), r.EmployeeID.ToString() + " - " + r.FirstName + " " + r.LastName)) .ToList()); } @@ -180,7 +181,7 @@ public ActionResult GetAllShippers() { var repository = new ShipperRepository(_context); return Ok(repository.GetAll() - .Select(r => new Tuple(r.ShipperID.ToString(), r.ShipperID.ToString() + " - " + .Select(r => new SelectItem(r.ShipperID.ToString(), r.ShipperID.ToString() + " - " + r.CompanyName)) .ToList()); } diff --git a/GridBlazorClientSide.Server/GridBlazorClientSide.Server.csproj b/GridBlazorClientSide.Server/GridBlazorClientSide.Server.csproj index d9ca33f9..6be936eb 100644 --- a/GridBlazorClientSide.Server/GridBlazorClientSide.Server.csproj +++ b/GridBlazorClientSide.Server/GridBlazorClientSide.Server.csproj @@ -9,8 +9,8 @@ - - + + diff --git a/GridBlazorServerSide/ColumnCollections/ColumnCollections.cs b/GridBlazorServerSide/ColumnCollections/ColumnCollections.cs index 13605838..735f2746 100644 --- a/GridBlazorServerSide/ColumnCollections/ColumnCollections.cs +++ b/GridBlazorServerSide/ColumnCollections/ColumnCollections.cs @@ -152,8 +152,8 @@ public class ColumnCollections .RenderValueAs(o => o.Customer.IsVip ? "Yes" : "No"); }; - public static Action, Func>>, - Func>>, Func>>> + public static Action, Func>, + Func>, Func>> OrderColumnsWithCrud = (c, f, g, h) => { /* Adding "OrderID" column: */ @@ -214,32 +214,31 @@ public class ColumnCollections c.Add(o => o.ShipCountry, true); }; - public static Action> - OrderColumnsWithCustomCrud = (c) => - { - /* Adding "OrderID" column: */ - c.Add(o => o.OrderID).SetPrimaryKey(true).Titled(SharedResource.Number).SetWidth(100); - - /* Adding "OrderDate" column: */ - c.Add(o => o.OrderDate, "OrderCustomDate").Titled(SharedResource.OrderCustomDate) - .Format("{0:yyyy-MM-dd}").SetWidth(120); - - /* Adding "CompanyName" column: */ - c.Add(o => o.Customer.CompanyName).Titled(SharedResource.CompanyName) - .SetWidth(250).SetCrudHidden(true); - - /* Adding "ContactName" column: */ - c.Add(o => o.Customer.ContactName).Titled(SharedResource.ContactName).SetWidth(250).SetCrudHidden(true); - - /* Adding "Freight" column: */ - c.Add(o => o.Freight) - .Titled(SharedResource.Freight) - .Format("{0:F}"); - - /* Adding "Vip customer" column: */ - c.Add(o => o.Customer.IsVip).Titled(SharedResource.IsVip).SetWidth(70).Css("hidden-xs") //hide on phones - .RenderValueAs(o => o.Customer.IsVip ? "Yes" : "No").SetCrudHidden(true); - }; + public static Action> OrderColumnsWithCustomCrud = c => + { + /* Adding "OrderID" column: */ + c.Add(o => o.OrderID).SetPrimaryKey(true).Titled(SharedResource.Number).SetWidth(100); + + /* Adding "OrderDate" column: */ + c.Add(o => o.OrderDate, "OrderCustomDate").Titled(SharedResource.OrderCustomDate) + .Format("{0:yyyy-MM-dd}").SetWidth(120); + + /* Adding "CompanyName" column: */ + c.Add(o => o.Customer.CompanyName).Titled(SharedResource.CompanyName) + .SetWidth(250).SetCrudHidden(true); + + /* Adding "ContactName" column: */ + c.Add(o => o.Customer.ContactName).Titled(SharedResource.ContactName).SetWidth(250).SetCrudHidden(true); + + /* Adding "Freight" column: */ + c.Add(o => o.Freight) + .Titled(SharedResource.Freight) + .Format("{0:F}"); + + /* Adding "Vip customer" column: */ + c.Add(o => o.Customer.IsVip).Titled(SharedResource.IsVip).SetWidth(70).Css("hidden-xs") //hide on phones + .RenderValueAs(o => o.Customer.IsVip ? "Yes" : "No").SetCrudHidden(true); + }; public static Action> OrderColumnsWithSubgrids = c => { diff --git a/GridBlazorServerSide/GridBlazorServerSide.csproj b/GridBlazorServerSide/GridBlazorServerSide.csproj index 794d308e..3c4acbb8 100644 --- a/GridBlazorServerSide/GridBlazorServerSide.csproj +++ b/GridBlazorServerSide/GridBlazorServerSide.csproj @@ -8,8 +8,8 @@ - - + + diff --git a/GridBlazorServerSide/Services/CustomerService.cs b/GridBlazorServerSide/Services/CustomerService.cs index 8f18b9ba..d81f5525 100644 --- a/GridBlazorServerSide/Services/CustomerService.cs +++ b/GridBlazorServerSide/Services/CustomerService.cs @@ -30,13 +30,13 @@ public IEnumerable GetCustomersNames() } } - public IEnumerable> GetAllCustomers() + public IEnumerable GetAllCustomers() { using (var context = new NorthwindDbContext(_options)) { CustomersRepository repository = new CustomersRepository(context); return repository.GetAll() - .Select(r => new Tuple(r.CustomerID, r.CustomerID + " - " + r.CompanyName)) + .Select(r => new SelectItem(r.CustomerID, r.CustomerID + " - " + r.CompanyName)) .ToList(); } } @@ -65,7 +65,7 @@ public IEnumerable GetCustomersNames() public interface ICustomerService { IEnumerable GetCustomersNames(); - IEnumerable> GetAllCustomers(); + IEnumerable GetAllCustomers(); ItemsDTO GetCustomersGridRows(Action> columns, QueryDictionary query); } } diff --git a/GridBlazorServerSide/Services/EmployeeService.cs b/GridBlazorServerSide/Services/EmployeeService.cs index bca139a0..a07e1313 100644 --- a/GridBlazorServerSide/Services/EmployeeService.cs +++ b/GridBlazorServerSide/Services/EmployeeService.cs @@ -1,6 +1,6 @@ using GridBlazorServerSide.Data; +using GridShared; using Microsoft.EntityFrameworkCore; -using System; using System.Collections.Generic; using System.Linq; @@ -15,13 +15,13 @@ public EmployeeService(DbContextOptions options) _options = options; } - public IEnumerable> GetAllEmployees() + public IEnumerable GetAllEmployees() { using (var context = new NorthwindDbContext(_options)) { EmployeeRepository repository = new EmployeeRepository(context); return repository.GetAll() - .Select(r => new Tuple(r.EmployeeID.ToString(), r.EmployeeID.ToString() + " - " + .Select(r => new SelectItem(r.EmployeeID.ToString(), r.EmployeeID.ToString() + " - " + r.FirstName + " " + r.LastName)) .ToList(); } @@ -30,6 +30,6 @@ public EmployeeService(DbContextOptions options) public interface IEmployeeService { - IEnumerable> GetAllEmployees(); + IEnumerable GetAllEmployees(); } } diff --git a/GridBlazorServerSide/Services/ShipperService.cs b/GridBlazorServerSide/Services/ShipperService.cs index 2f8fbdcd..c4ff8bef 100644 --- a/GridBlazorServerSide/Services/ShipperService.cs +++ b/GridBlazorServerSide/Services/ShipperService.cs @@ -1,6 +1,6 @@ using GridBlazorServerSide.Data; +using GridShared; using Microsoft.EntityFrameworkCore; -using System; using System.Collections.Generic; using System.Linq; @@ -15,13 +15,13 @@ public ShipperService(DbContextOptions options) _options = options; } - public IEnumerable> GetAllShippers() + public IEnumerable GetAllShippers() { using (var context = new NorthwindDbContext(_options)) { ShipperRepository repository = new ShipperRepository(context); return repository.GetAll() - .Select(r => new Tuple(r.ShipperID.ToString(), r.ShipperID.ToString() + " - " + .Select(r => new SelectItem(r.ShipperID.ToString(), r.ShipperID.ToString() + " - " + r.CompanyName)) .ToList(); } @@ -30,6 +30,6 @@ public ShipperService(DbContextOptions options) public interface IShipperService { - IEnumerable> GetAllShippers(); + IEnumerable GetAllShippers(); } } diff --git a/GridMvc.Demo/GridMvc.Demo.csproj b/GridMvc.Demo/GridMvc.Demo.csproj index 90a0beae..c7718494 100644 --- a/GridMvc.Demo/GridMvc.Demo.csproj +++ b/GridMvc.Demo/GridMvc.Demo.csproj @@ -10,8 +10,8 @@ - - + + diff --git a/GridMvc.Tests/GridMvc.Tests.csproj b/GridMvc.Tests/GridMvc.Tests.csproj index 3d7420b6..4bf59b25 100644 --- a/GridMvc.Tests/GridMvc.Tests.csproj +++ b/GridMvc.Tests/GridMvc.Tests.csproj @@ -6,7 +6,7 @@ - + diff --git a/GridMvc/Columns/GridColumnBase.cs b/GridMvc/Columns/GridColumnBase.cs index 661c5f99..52ea3529 100644 --- a/GridMvc/Columns/GridColumnBase.cs +++ b/GridMvc/Columns/GridColumnBase.cs @@ -47,7 +47,9 @@ public abstract class GridColumnBase : GridStyledColumn, IGridColumn, ISGr public bool IsPrimaryKey { get; protected set; } = false; - public (bool IsSelectKey, Func Expression, Func>> KeyValues) IsSelectField { get; protected set; } = (false, null, null); + public (bool IsSelectKey, Func Expression, string Url, Func> SelectItemExpr) IsSelectField { get; protected set; } = (false, null, null, null); + + public IEnumerable SelectItems { get; internal set; } public bool IsSumEnabled { get; internal set; } = false; @@ -197,12 +199,17 @@ public IGridColumn SetPrimaryKey(bool enabled) return this; } - public IGridColumn SetSelectField(bool enabled, Func expression, Func>> keyValues) + public IGridColumn SetSelectField(bool enabled, Func expression, Func> selectItemExpr) { - IsSelectField = (enabled, expression, keyValues); + IsSelectField = (enabled, expression, null, selectItemExpr); return this; } + public IGridColumn SetSelectField(bool enabled, Func expression, string url) + { + IsSelectField = (enabled, expression, url, null); + return this; + } public abstract IGrid ParentGrid { get; } public virtual IGridColumn Sanitized(bool sanitize) diff --git a/GridShared/Columns/IGridColumn.cs b/GridShared/Columns/IGridColumn.cs index 5b3d355a..a792d349 100644 --- a/GridShared/Columns/IGridColumn.cs +++ b/GridShared/Columns/IGridColumn.cs @@ -1,4 +1,5 @@ -using GridShared.Filtering; +using GridShared; +using GridShared.Filtering; using GridShared.Grouping; using GridShared.Searching; using GridShared.Sorting; @@ -14,7 +15,8 @@ public interface IGridColumn : IGridColumn, IColumn, ISortableColumn, { IGridCell GetValue(T instance); (Type Type, object Value) GetTypeAndValue(T item); - (bool IsSelectKey, Func Expression, Func>> KeyValues) IsSelectField { get; } + (bool IsSelectKey, Func Expression, string Url, Func> SelectItemExpr) IsSelectField { get; } + IEnumerable SelectItems { get; } } public interface IGridColumn : ISortableColumn, IFilterableColumn @@ -155,7 +157,12 @@ public interface IColumn /// /// Sets the column as select for CRUD components /// - IGridColumn SetSelectField(bool enabled, Func expression, Func>> keyValues); + IGridColumn SetSelectField(bool enabled, Func expression, Func> selectItemExpr); + + /// + /// Sets the column as select for CRUD components + /// + IGridColumn SetSelectField(bool enabled, Func expression, string url); } public interface IColumn diff --git a/GridShared/GridShared.csproj b/GridShared/GridShared.csproj index ebebc90e..a262f86c 100644 --- a/GridShared/GridShared.csproj +++ b/GridShared/GridShared.csproj @@ -14,7 +14,7 @@ - + diff --git a/GridShared/SelectItem.cs b/GridShared/SelectItem.cs new file mode 100644 index 00000000..0b95806a --- /dev/null +++ b/GridShared/SelectItem.cs @@ -0,0 +1,18 @@ +namespace GridShared +{ + public class SelectItem + { + public string Value { get; set; } + + public string Title { get; set; } + + public SelectItem() + { } + + public SelectItem(string value, string title) + { + Value = value; + Title = title; + } + } +}