GridBlazor component provides some events to notify other classes or objects when the grid has changed. The supported events are:
Func<object, PagerEventArgs, Task> PagerChanged
: it's fired when the page number and/or page size are changedFunc<object, SortEventArgs, Task> SortChanged
: it's fired when sorting is changedFunc<object, ExtSortEventArgs, Task> ExtSortChanged
: it's fired when extended sorting or grouping are changedFunc<object, FilterEventArgs, Task<bool>> BeforeFilterChanged
: it's fired before a filter is created, changed or removedFunc<object, FilterEventArgs, Task> FilterChanged
: it's fired when a filter is created, changed or removedFunc<object, SearchEventArgs, Task> SearchChanged
: it's fired when the a new word is searched or search has been clearedThese events are provided to allow running tasks on changing grid items:
Func<GridCreateComponent<T>, T, Task<bool>> BeforeInsert
: it's fired before an item is insertedFunc<GridCreateComponent<T>, T, Task<bool>> BeforeUpdate
: it's fired before an item is updatedFunc<GridCreateComponent<T>, T, Task<bool>> BeforeDelete
: it's fired before an item is deletedFunc<GridCreateComponent<T>, T, Task> AfterInsert
: it's fired after an item is insertedFunc<GridCreateComponent<T>, T, Task> AfterUpdate
: it's fired after an item is updatedFunc<GridCreateComponent<T>, T, Task> AfterDelete
: it's fired after an item is deletedThese events are provided to allow running tasks on changing Checkbox columns :
Func<CheckboxEventArgs<T>, Task> HeaderCheckboxChanged
: it's fired when a header checkbox is changedFunc<CheckboxEventArgs<T>, Task> RowCheckboxChanged
: it's fired when a row checkbox is changedAnd these events are provided to allow running tasks before and after grid is refreshed:
Func<Task> BeforeRefreshGrid
: it's fired before the grid will be refreshedFunc<Task> AfterRefreshGrid
: it's fired after the grid is refreshedIf you want to handle an event you have to create a reference to the GridCompoment
.
The GridCompoment
object has an attribute named Error
that can be set to show an error on the CRUD form.
There is also another property named ColumnErrors
to show errors specific to each each form field. ColumnErrors
is a QueryDictionary
to store the error message for each field.
Then you have to add the events that you want to handle in the OnAfterRender
method.
And finally you have to write the handlers for each event.
You can see here an example handling all component events that writes some grid changes on the console and validates a grid item before being inserted, updated and deleted:
<GridComponent @ref="_gridComponent" T="Order" Grid="@_grid"></GridComponent>
@code
{
private GridComponent<Order> _gridComponent;
private bool _areEventsLoaded = false;
...
protected override void OnAfterRender(bool firstRender)
{
if (!_areEventsLoaded && _gridComponent != null)
{
_gridComponent.PagerChanged += PagerChanged;
_gridComponent.SortChanged += SortChanged;
_gridComponent.ExtSortChanged += ExtSortChanged;
_gridComponent.BeforeFilterChanged += BeforeFilterChanged;
_gridComponent.FilterChanged += FilterChanged;
_gridComponent.SearchChanged += SearchChanged;
_gridComponent.BeforeInsert += BeforeInsert;
_gridComponent.BeforeUpdate += BeforeUpdate;
_gridComponent.BeforeDelete += BeforeDelete;
_gridComponent.BeforeRefreshGrid += BeforeRefreshGrid;
_gridComponent.AfterRefreshGrid += AfterRefreshGrid;
_areEventsLoaded = true;
}
}
private async Task PagerChanged(object sender, PagerEventArgs e)
{
Console.WriteLine("The pager has changed: EnablePaging: {0}, CurrentPage: {1}, ItemsCount: {2}, PageSize: {3}.",
e.Pager.EnablePaging, e.Pager.CurrentPage, e.Pager.ItemsCount, e.Pager.PageSize);
await Task.CompletedTask;
}
private async Task SortChanged(object sender, SortEventArgs e)
{
Console.WriteLine("Sorting has changed: ColumnName: {0}, Direction: {1}.",
e.ColumnName, e.Direction);
await Task.CompletedTask;
}
private async Task ExtSortChanged(object sender, ExtSortEventArgs e)
{
Console.WriteLine("Extended sorting has changed:");
foreach (var sortValues in e.SortValues)
{
Console.WriteLine(" - ColumnName: {0}, Direction: {1}, Id: {2}.",
sortValues.ColumnName, sortValues.Direction, sortValues.Id);
}
await Task.CompletedTask;
}
private async Task<bool> BeforeFilterChanged(object sender, FilterEventArgs e)
{
Console.WriteLine("Filters can be changed:");
foreach (var filteredColumn in e.FilteredColumns)
{
Console.WriteLine(" - ColumnName: {0}, FilterType: {1}, FilterValue: {2}.",
filteredColumn.ColumnName, filteredColumn.FilterType, filteredColumn.FilterValue);
}
await Task.CompletedTask;
var rnd = new Random();
if (rnd.Next(100) % 2 == 0)
{
Console.WriteLine("Filters will be changed");
return true;
}
else
{
Console.WriteLine("Filters won't be changed");
return false;
}
}
private async Task FilterChanged(object sender, FilterEventArgs e)
{
Console.WriteLine("Filters have changed:");
foreach (var filteredColumn in e.FilteredColumns)
{
Console.WriteLine(" - ColumnName: {0}, FilterType: {1}, FilterValue: {2}.",
filteredColumn.ColumnName, filteredColumn.FilterType, filteredColumn.FilterValue);
}
await Task.CompletedTask;
}
private async Task SearchChanged(object sender, SearchEventArgs e)
{
Console.WriteLine("Search has changed: SearchValue: {0}.", e.SearchValue);
await Task.CompletedTask;
}
private async Task<bool> BeforeInsert(GridCreateComponent<Order> component, Order item)
{
var orderValidator = new OrderValidator();
var valid = await orderValidator.ValidateAsync(item);
if (!valid.IsValid)
{
component.Error = "Insert operation returned one or more errors";
foreach (var error in valid.Errors)
{
component.ColumnErrors.AddParameter(error.PropertyName, error.ErrorMessage);
}
}
return valid.IsValid;
}
private async Task<bool> BeforeUpdate(GridUpdateComponent<Order> component, Order item)
{
var orderValidator = new OrderValidator();
var valid = await orderValidator.ValidateAsync(item);
if (!valid.IsValid)
{
component.Error = "Update operation returned one or more errors";
foreach (var error in valid.Errors)
{
component.ColumnErrors.AddParameter(error.PropertyName, error.ErrorMessage);
}
}
return valid.IsValid;
}
private async Task<bool> BeforeDelete(GridDeleteComponent<Order> component, Order item)
{
var orderValidator = new OrderValidator();
var valid = await orderValidator.ValidateAsync(item);
if (!valid.IsValid)
{
component.Error = valid.ToString();
}
return valid.IsValid;
}
private async Task<bool> BeforeRefreshGrid()
{
Console.WriteLine("Grid will start refreshing");
await Task.CompletedTask;
return true;
}
private async Task AfterRefreshGrid()
{
Console.WriteLine("Grid has been refreshed");
await Task.CompletedTask;
}
}
Notice that all handlers must be async.
In this sample OrderValidator
is a class that validates the Order
object to be modified. If it's a valid item the event returns true
. If the item is not valid the event writes an error on the form and returns false
.
GridException
is provided to throw exceptions from the ICrudDataService<T>
services used for data persistence. The GridException
message will be shown on the CRUD forms.
You can see here an example catching any exception issued during database insert and throwing a new GridException
with a custom message:
public async Task Insert(OrderDetail item)
{
using (var context = new NorthwindDbContext(_options))
{
try
{
var repository = new OrderDetailsRepository(context);
await repository.Insert(item);
repository.Save();
}
catch (Exception e)
{
throw new GridException("There was an error during the order detail record creation");
}
}
}
You can also throw a GridException
that will show the most inner exception's message on the CRUD form:
public async Task Insert(OrderDetail item)
{
using (var context = new NorthwindDbContext(_options))
{
try
{
var repository = new OrderDetailsRepository(context);
await repository.Insert(item);
repository.Save();
}
catch (Exception e)
{
throw new GridException(e);
}
}
}
This is an example of a CRUD form error:
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。