1 Star 0 Fork 0

XFly-Studio / Grid.Blazor

Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
Clone or download
Events.md 10.19 KB
Copy Edit Web IDE Raw Blame History

GridBlazor for ASP.NET Core MVC

Events, exceptions and CRUD validation

Index

Events and CRUD validation

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 changed
  • Func<object, SortEventArgs, Task> SortChanged: it's fired when sorting is changed
  • Func<object, ExtSortEventArgs, Task> ExtSortChanged: it's fired when extended sorting or grouping are changed
  • Func<object, FilterEventArgs, Task<bool>> BeforeFilterChanged: it's fired before a filter is created, changed or removed
  • Func<object, FilterEventArgs, Task> FilterChanged: it's fired when a filter is created, changed or removed
  • Func<object, SearchEventArgs, Task> SearchChanged: it's fired when the a new word is searched or search has been cleared

These 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 inserted
  • Func<GridCreateComponent<T>, T, Task<bool>> BeforeUpdate: it's fired before an item is updated
  • Func<GridCreateComponent<T>, T, Task<bool>> BeforeDelete: it's fired before an item is deleted
  • Func<GridCreateComponent<T>, T, Task> AfterInsert: it's fired after an item is inserted
  • Func<GridCreateComponent<T>, T, Task> AfterUpdate: it's fired after an item is updated
  • Func<GridCreateComponent<T>, T, Task> AfterDelete: it's fired after an item is deleted

These events are provided to allow running tasks on changing Checkbox columns :

  • Func<CheckboxEventArgs<T>, Task> HeaderCheckboxChanged: it's fired when a header checkbox is changed
  • Func<CheckboxEventArgs<T>, Task> RowCheckboxChanged: it's fired when a row checkbox is changed

And 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 refreshed
  • Func<Task> AfterRefreshGrid: it's fired after the grid is refreshed

If 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;
        ...

        protected override void OnAfterRender(bool firstRender)
        {
            if (firstRender)
            {
                _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;
            }
        }

        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.

Exceptions and messages on the CRUD forms

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:

<- Nested CRUD | Button components on the grid ->

Comment ( 0 )

Sign in for post a comment

1
https://gitee.com/fan0217/Grid.Blazor.git
git@gitee.com:fan0217/Grid.Blazor.git
fan0217
Grid.Blazor
Grid.Blazor
master

Search