Skip to content

Commit

Permalink
Add AfterChangeValue grid column event
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavnavar committed Apr 5, 2022
1 parent f5cce95 commit a97142e
Show file tree
Hide file tree
Showing 25 changed files with 771 additions and 6 deletions.
8 changes: 8 additions & 0 deletions GridBlazor/Columns/GridColumnBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ public abstract class GridColumnBase<T> : GridStyledColumn, IGridColumn<T>, IExp

public Func<T, CrudHidden> CrudHidden { get; protected set; } = x => GridShared.Columns.CrudHidden.NONE;

public Func<T, GridMode, Task> AfterChangeValue { get; set; }

public Func<T, bool> ReadOnlyOnCreate { get; protected set; } = x => false;

public Func<T, bool> ReadOnlyOnUpdate { get; protected set; } = x => false;
Expand Down Expand Up @@ -524,6 +526,12 @@ public IGridColumn<T> SetCrudHidden(bool create, bool read, bool update, bool de
return this;
}

public IGridColumn<T> SetAfterChangeValue(Func<T, GridMode, Task> afterChangeValue)
{
AfterChangeValue = afterChangeValue;
return this;
}

public IGridColumn<T> SetExcelHidden(bool? excelHidden)
{
ExcelHidden = excelHidden;
Expand Down
2 changes: 1 addition & 1 deletion GridBlazor/GridBlazor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<LangVersion>8.0</LangVersion>
<GenerateEmbeddedFilesManifest>True</GenerateEmbeddedFilesManifest>
<EnableDefaultEmbeddedResourceItems>False</EnableDefaultEmbeddedResourceItems>
<Version>3.2.4</Version>
<Version>3.2.5</Version>
<Title>GridBlazor</Title>
<Description>Grid components for Blazor</Description>
<Summary>Grid components for Blazor</Summary>
Expand Down
5 changes: 4 additions & 1 deletion GridBlazor/Pages/GridCreateComponent.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private void SetValue(object value, IGridColumn column)
pi.SetValue(obj, value, null);
}

private void ChangeValue(ChangeEventArgs e, IGridColumn column, string typeAttr = null)
private async Task ChangeValue(ChangeEventArgs e, IGridColumn column, string typeAttr = null)
{
if (string.IsNullOrWhiteSpace(e.Value.ToString()))
{
Expand Down Expand Up @@ -181,6 +181,9 @@ private void ChangeValue(ChangeEventArgs e, IGridColumn column, string typeAttr
}
}
}

if (((IGridColumn<T>)column).AfterChangeValue != null)
await((IGridColumn<T>)column).AfterChangeValue(Item, GridMode.Create);
}

private void OnFileChange(IGridColumn column, IFileListEntry[] files)
Expand Down
5 changes: 4 additions & 1 deletion GridBlazor/Pages/GridUpdateComponent.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private void SetValue(object value, IGridColumn column)
pi.SetValue(obj, value, null);
}

private void ChangeValue(ChangeEventArgs e, IGridColumn column, string typeAttr = null)
private async Task ChangeValue(ChangeEventArgs e, IGridColumn column, string typeAttr = null)
{
if (string.IsNullOrWhiteSpace(e.Value.ToString()))
{
Expand Down Expand Up @@ -201,6 +201,9 @@ private void ChangeValue(ChangeEventArgs e, IGridColumn column, string typeAttr
}
}
}

if (((IGridColumn<T>)column).AfterChangeValue != null)
await((IGridColumn<T>)column).AfterChangeValue(Item, GridMode.Update);
}

private void OnFileChange(IGridColumn column, IFileListEntry[] files)
Expand Down
66 changes: 66 additions & 0 deletions GridBlazorClientSide.Client/ColumnCollections/ColumnCollections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,72 @@ public class ColumnCollections
.RenderValueAs(o => o.Customer.IsVip ? Strings.BoolTrueLabel : Strings.BoolFalseLabel).SetCrudHidden(true);
};

public static Action<IGridColumnCollection<Order>, Func<Order, GridMode, Task>, string> OrderColumnsWithCrudAfterChange
= (c, afterChangeCustomerID, path) =>
{
/* 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, o => path + $"api/SampleData/GetAllCustomers")
.SetAfterChangeValue(afterChangeCustomerID);
/* Adding "EmployeeID" column: */
c.Add(o => o.EmployeeID, true).SetSelectField(true, o => o.Employee.EmployeeID.ToString() + " - " + o.Employee.FirstName + " " + o.Employee.LastName, o => path + $"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, path + $"api/SampleData/GetAllShippers");
/* Adding "OrderDate" column: */
c.Add(o => o.OrderDate, "OrderCustomDate").Titled(SharedResource.OrderCustomDate)
.Format("{0:yyyy-MM-dd}").SetWidth(120)
.SetCrudWidth(3);
/* Adding "CompanyName" column: */
c.Add(o => o.Customer.CompanyName).Titled(SharedResource.CompanyName)
.SetWidth(250).SetCrudHidden(true).SetReadOnlyOnUpdate(true);
/* Adding "ContactName" column: */
c.Add(o => o.Customer.ContactName).Titled(SharedResource.ContactName).SetCrudHidden(true);
/* Adding "Freight" column: */
c.Add(o => o.Freight)
.Titled(SharedResource.Freight)
.SetWidth(150)
.Format("{0:#,##0.00}");
/* Adding "Vip customer" column: */
c.Add(o => o.Customer.IsVip).Titled(SharedResource.IsVip).SetWidth(90).Css("hidden-xs") //hide on phones
.RenderValueAs(o => o.Customer.IsVip ? Strings.BoolTrueLabel : Strings.BoolFalseLabel).SetCrudHidden(true);
/* Adding hidden "RequiredDate" column: */
c.Add(o => o.RequiredDate, true).Format("{0:yyyy-MM-dd}").SetCrudWidth(3);
/* Adding hidden "ShippedDate" column: */
c.Add(o => o.ShippedDate, true).Format("{0:yyyy-MM-dd}").SetCrudWidth(3);
/* Adding hidden "ShipName" column: */
c.Add(o => o.ShipName, true);
/* Adding hidden "ShipAddress" column: */
c.Add(o => o.ShipAddress, true);
/* Adding hidden "ShipCity" column: */
c.Add(o => o.ShipCity, true);
/* Adding hidden "ShipPostalCode" column: */
c.Add(o => o.ShipPostalCode, true);
/* Adding hidden "ShipRegion" column: */
c.Add(o => o.ShipRegion, true);
/* Adding hidden "ShipCountry" column: */
c.Add(o => o.ShipCountry, true);
/* Adding not mapped column, that renders a component */
c.Add(true).Titled("Images").RenderCrudComponentAs<Carousel, Carousel, Carousel, NullComponent>();
};

public static Action<IGridColumnCollection<Order>> OrderColumnsWithSubgrids = c =>
{
/* Adding "OrderID" column: */
Expand Down
71 changes: 71 additions & 0 deletions GridBlazorClientSide.Client/Pages/AfterChangeValue.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
@page "/afterchangevalue"
@using GridBlazorClientSide.Client.ColumnCollections
@using GridBlazorClientSide.Shared.Models
@using Microsoft.Extensions.Primitives
@using System.Globalization
@using System.Threading.Tasks
@inject NavigationManager NavigationManager
@inject HttpClient HttpClient
@inject ICrudDataService<Order> orderService

<h1>Grid Sample</h1>

<p>
This page contains a CRUD grid implementing an event to modify the OrderDate after the CustomerID is changed on the Create form
</p>

<p>
This component demonstrates a GridBlazor client-side grid. For more information, please see: <a href="https://github.com/gustavnavar/Grid.Blazor">https://github.com/gustavnavar/Grid.Blazor</a>
</p>

@if (_task.IsCompleted)
{
<div class="row">
<div class="col-md-12">
<GridComponent T="Order" Grid="@_grid"></GridComponent>
</div>
</div>
}
else
{
<p><em>Loading...</em></p>
}

@code
{
private CGrid<Order> _grid;
private Task _task;

protected override async Task OnParametersSetAsync()
{
var locale = CultureInfo.CurrentCulture;

var query = new QueryDictionary<StringValues>();
string url = NavigationManager.BaseUri + "api/SampleData/OrderColumnsWithCrud";

Func<Order, GridMode, Task> afterChangeCustomerID = async (order, mode) =>
{
if (mode == GridMode.Create)
{
order.OrderDate = DateTime.Now;
StateHasChanged();
await Task.CompletedTask;
}
};

var client = new GridClient<Order>(HttpClient, url, query, false, "ordersGrid", c =>
ColumnCollections.OrderColumnsWithCrudAfterChange(c, afterChangeCustomerID, NavigationManager.BaseUri), locale)
.Sortable()
.Filterable()
.SetStriped(true)
.Crud(true, orderService)
.WithMultipleFilters()
.WithGridItemsCount();

_grid = client.Grid;

// Set new items to grid
_task = client.UpdateGrid();
await _task;
}
}
5 changes: 5 additions & 0 deletions GridBlazorClientSide.Client/Shared/NavMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@
<span class="oi oi-list-rich" aria-hidden="true"></span>Header CRUD
</NavLink>
</li>
<li class="nav-item px-3">
<NavLink class="nav-link" href="afterchangevalue">
<span class="oi oi-list-rich" aria-hidden="true"></span>Change event
</NavLink>
</li>
<li class="nav-item px-3">
<NavLink class="nav-link" href="changepagesize">
<span class="oi oi-list-rich" aria-hidden="true"></span>Page Size
Expand Down
142 changes: 142 additions & 0 deletions GridBlazorOData.Client/Pages/AfterChangeValue.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
@page "/afterchangevalue"
@using GridBlazor.Resources
@using GridShared.Columns
@using GridBlazorOData.Client.Resources
@using GridBlazorOData.Shared.Models
@using Microsoft.Extensions.Primitives
@using System.Globalization
@using System.Threading.Tasks
@inject NavigationManager NavigationManager
@inject HttpClient HttpClient

<h1>Grid Sample</h1>

<p>
This page contains a CRUD grid implementing an event to modify the OrderDate after the CustomerID is changed on the Create form
</p>

<p>
This component demonstrates a GridBlazor client-side grid with an OData back-end. For more information, please see: <a href="https://github.com/gustavnavar/Grid.Blazor">https://github.com/gustavnavar/Grid.Blazor</a>
</p>

@if (_task.IsCompleted)
{
<div class="row">
<div class="col-md-12">
<GridComponent T="Order" Grid="@_grid"></GridComponent>
</div>
</div>
}
else
{
<p><em>Loading...</em></p>
}

@code
{
private CGrid<Order> _grid;
private Task _task;

protected override async Task OnParametersSetAsync()
{
var locale = CultureInfo.CurrentCulture;

var query = new QueryDictionary<StringValues>();

Action<IGridColumnCollection<Order>, Func<Order, GridMode, Task>, string> columns = (c, afterChangeCustomerID, baseUri) =>
{
c.Add(o => o.OrderID).SetPrimaryKey(true).Titled(SharedResource.Number).SetWidth(100);
c.Add(o => o.CustomerID, true).SetSelectField(true, o => o.Customer.CustomerID + " - " + o.Customer.CompanyName, o => GetAllCustomers(baseUri)).SetAfterChangeValue(afterChangeCustomerID);
c.Add(o => o.EmployeeID, true).SetSelectField(true, o => o.Employee.EmployeeID.ToString() + " - " + o.Employee.FirstName + " " + o.Employee.LastName, o => GetAllEmployees(baseUri));
c.Add(o => o.ShipVia, true).SetSelectField(true, o => o.Shipper == null ? "" : o.Shipper.ShipperID.ToString() + " - " + o.Shipper.CompanyName, () => GetAllShippers(baseUri));
c.Add(o => o.OrderDate, "OrderCustomDate").Titled(SharedResource.OrderCustomDate).Format("{0:yyyy-MM-dd}").SetWidth(120).SetCrudWidth(3);
c.Add(o => o.Customer.CompanyName).Titled(SharedResource.CompanyName).SetWidth(250).SetCrudHidden(true).SetReadOnlyOnUpdate(true);
c.Add(o => o.Customer.ContactName).Titled(SharedResource.ContactName).SetCrudHidden(true);
c.Add(o => o.Freight).Titled(SharedResource.Freight).SetWidth(150).Format("{0:#,##0.00}");
c.Add(o => o.Customer.IsVip).Titled(SharedResource.IsVip).SetWidth(90).Css("hidden-xs").RenderValueAs(o => o.Customer.IsVip ? Strings.BoolTrueLabel : Strings.BoolFalseLabel).SetCrudHidden(true);
c.Add(o => o.RequiredDate, true).Format("{0:yyyy-MM-dd}").SetCrudWidth(3);
c.Add(o => o.ShippedDate, true).Format("{0:yyyy-MM-dd}").SetCrudWidth(3);
c.Add(o => o.ShipName, true);
c.Add(o => o.ShipAddress, true);
c.Add(o => o.ShipCity, true);
c.Add(o => o.ShipPostalCode, true);
c.Add(o => o.ShipRegion, true);
c.Add(o => o.ShipCountry, true);
c.Add(true).Titled("Images").RenderCrudComponentAs<Carousel, Carousel, NullComponent, NullComponent>();
};

Func<Order, GridMode, Task> afterChangeCustomerID = async (order, mode) =>
{
if (mode == GridMode.Create)
{
order.OrderDate = DateTime.Now;
StateHasChanged();
await Task.CompletedTask;
}
};

string url = NavigationManager.BaseUri + "odata/Orders";
var client = new GridODataClient<Order>(HttpClient, url, query, false, "ordersGrid",
c => columns(c, afterChangeCustomerID, NavigationManager.BaseUri), 10, locale, new List<string> { "Employee", "Shipper" })
.Sortable()
.Filterable()
.SetStriped(true)
.ODataCrud(true)
.WithMultipleFilters()
.WithGridItemsCount();

_grid = client.Grid;

// Set new items to grid
_task = client.UpdateGrid();
await _task;
}

Func<string, Task<IEnumerable<SelectItem>>> GetAllCustomers = async (baseUri) =>
{
string url = baseUri + $"odata/Customers?$select=CustomerID,CompanyName";
ODataDTO<Customer> response = await new HttpClient().GetFromJsonAsync<ODataDTO<Customer>>(url);
if (response == null || response.Value == null)
{
return new List<SelectItem>();
}
else
{
return response.Value
.Select(r => new SelectItem(r.CustomerID, r.CustomerID + " - " + r.CompanyName))
.ToList();
}
};

Func<string, Task<IEnumerable<SelectItem>>> GetAllEmployees = async (baseUri) =>
{
string url = baseUri + $"odata/Employees?$select=EmployeeID,FirstName,LastName";
ODataDTO<Employee> response = await new HttpClient().GetFromJsonAsync<ODataDTO<Employee>>(url);
if (response == null || response.Value == null)
{
return new List<SelectItem>();
}
else
{
return response.Value
.Select(r => new SelectItem(r.EmployeeID.ToString(), r.EmployeeID.ToString() + " - " + r.FirstName + " " + r.LastName))
.ToList();
}
};

Func<string, Task<IEnumerable<SelectItem>>> GetAllShippers = async (baseUri) =>
{
string url = baseUri + $"odata/Shippers?$select=ShipperID,CompanyName";
ODataDTO<Shipper> response = await new HttpClient().GetFromJsonAsync<ODataDTO<Shipper>>(url);
if (response == null || response.Value == null)
{
return new List<SelectItem>();
}
else
{
return response.Value
.Select(r => new SelectItem(r.ShipperID.ToString(), r.ShipperID.ToString() + " - " + r.CompanyName))
.ToList();
}
};
}
5 changes: 5 additions & 0 deletions GridBlazorOData.Client/Shared/NavMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@
<span class="oi oi-list-rich" aria-hidden="true"></span>Header CRUD
</NavLink>
</li>
<li class="nav-item px-3">
<NavLink class="nav-link" href="afterchangevalue">
<span class="oi oi-list-rich" aria-hidden="true"></span>Change event
</NavLink>
</li>
<li class="nav-item px-3">
<NavLink class="nav-link" href="changepagesize">
<span class="oi oi-list-rich" aria-hidden="true"></span>Page Size
Expand Down
Loading

0 comments on commit a97142e

Please sign in to comment.