diff --git a/README.md b/README.md index 1b96632..c750670 100644 --- a/README.md +++ b/README.md @@ -21,8 +21,10 @@ GridView component for Blazor ``` +**Version 0.11.0 contains new Triggers feature more info in [wiki](https://github.com/Mewriick/Blazor.FlexGrid/wiki/Triggers)** + # Instalation -[![NuGet Pre Release](https://img.shields.io/badge/nuget-0.10.2-orange.svg)](https://www.nuget.org/packages/Blazor.FlexGrid) +[![NuGet Pre Release](https://img.shields.io/badge/nuget-0.11.0-orange.svg)](https://www.nuget.org/packages/Blazor.FlexGrid) After nuget instalation you must create in Blazor.Client app Linker.xml file because nuget use some features which are not supported in default mono managed interpreter from WebAssembly (https://github.com/mono/mono/issues/8872) diff --git a/demo/Blazor.Components.Demo.FlexGrid/Pages/Grid.razor b/demo/Blazor.Components.Demo.FlexGrid/Pages/Grid.razor index 8c8d96d..1bbe808 100644 --- a/demo/Blazor.Components.Demo.FlexGrid/Pages/Grid.razor +++ b/demo/Blazor.Components.Demo.FlexGrid/Pages/Grid.razor @@ -11,7 +11,9 @@

Weather forecast

@*Test*@ - + + + @*@{ RenderFragment weatherTemp = (weather) => @@weather.Summary; @@ -33,16 +35,27 @@ Collection.AddColumnEditValueRenderer(w => w.Summary, editWeatherSummary); @code { - CollectionTableDataAdapter - dataAdapter; + bool forceReload; + CollectionTableDataAdapter dataAdapter; + + int pageSize = 5; + LazyLoadingOptions op; protected override async Task OnInitializedAsync() { var forecast = await WeatherService.GetForecastAsync(DateTime.Now); - dataAdapter = new CollectionTableDataAdapter - (forecast); + dataAdapter = new CollectionTableDataAdapter(forecast); + } - + async Task SetPageSize() + { + pageSize++; + var forecast = await WeatherService.GetForecastAsync(new DateTime(2020, 3, 19)); + dataAdapter = new CollectionTableDataAdapter(forecast); + } + async Task ReloadCurrentPage() + { + await dataAdapter.ReloadCurrentPage(); } } diff --git a/demo/Blazor.Components.Demo.FlexGrid/Services/WeatherForecastService.cs b/demo/Blazor.Components.Demo.FlexGrid/Services/WeatherForecastService.cs index 010bc1f..168a331 100644 --- a/demo/Blazor.Components.Demo.FlexGrid/Services/WeatherForecastService.cs +++ b/demo/Blazor.Components.Demo.FlexGrid/Services/WeatherForecastService.cs @@ -39,6 +39,7 @@ public Task GetForecastAsync(DateTime startDate) var rng = new Random(); return Task.FromResult(staticRepositoryCollections.Forecasts //.Take(20) + .Where(kv => kv.Value.Date > startDate) .Select(kv => kv.Value) .ToArray() ); diff --git a/demo/Blazor.Components.Demo.FlexGrid/_Imports.razor b/demo/Blazor.Components.Demo.FlexGrid/_Imports.razor index f4a6278..4f206cc 100644 --- a/demo/Blazor.Components.Demo.FlexGrid/_Imports.razor +++ b/demo/Blazor.Components.Demo.FlexGrid/_Imports.razor @@ -12,4 +12,4 @@ @using Blazor.Components.Demo.FlexGrid.Services @using Microsoft.AspNetCore.Mvc.TagHelpers @using Blazor.FlexGrid - +@using Blazor.FlexGrid.DataSet.Options diff --git a/demo/Blazor.FlexGrid.Demo.Client/Pages/LazyLoadedGrid.razor b/demo/Blazor.FlexGrid.Demo.Client/Pages/LazyLoadedGrid.razor index 99f047d..1eed821 100644 --- a/demo/Blazor.FlexGrid.Demo.Client/Pages/LazyLoadedGrid.razor +++ b/demo/Blazor.FlexGrid.Demo.Client/Pages/LazyLoadedGrid.razor @@ -17,19 +17,35 @@

This component demonstrates fetching data from the server.

- + + + @code{ + + ITableDataAdapter dataAdapter; + LazyLoadingOptions options = new LazyLoadingOptions() + { + DataUri = "api/SampleData/WeatherForecasts", + PutDataUri = "api/SampleData/UpdateWeatherForecast", + DeleteUri = "api/SampleData/Delete/{Id}" + }; + + protected override void OnInitialized() + { + base.OnInitialized(); + + dataAdapter = forecastAdapter; + } + public void ItemSavedOperationFinished(SaveResultArgs saveResultArgs) { Console.WriteLine($"Item saved result: {saveResultArgs.ItemSucessfullySaved}"); @@ -44,6 +60,29 @@ { Console.WriteLine($"Item created result: {itemCreatedArgs.CreatedItem}"); } + + void ChangeOptions() + { + options = new LazyLoadingOptions() + { + DataUri = "api/SampleData/WeatherForecasts", + PutDataUri = "api/SampleData/UpdateWeatherForecast", + DeleteUri = "api/SampleData/Delete/{Id}" + }; + + options.RequestParams.Add("test", "value"); + } + + async Task ReloadCurrentPage() + { + await dataAdapter.ReloadCurrentPage(); + } + + async Task ChangeDataAdapter() + { + var forecast = await Http.GetJsonAsync("api/SampleData/WeatherForecastsSimple"); + dataAdapter = new CollectionTableDataAdapter(forecast); + } } diff --git a/src/Blazor.FlexGrid/Blazor.FlexGrid.csproj b/src/Blazor.FlexGrid/Blazor.FlexGrid.csproj index 92b68c2..c019c09 100644 --- a/src/Blazor.FlexGrid/Blazor.FlexGrid.csproj +++ b/src/Blazor.FlexGrid/Blazor.FlexGrid.csproj @@ -9,14 +9,14 @@ false 7.3 Blazor.FlexGrid - 0.10.2 + 0.11.0 Blazor.FlexGrid GridView component for Blazor Jaroslav Surala https://github.com/Mewriick/Blazor.FlexGrid Blazor;GridView;Component - 0.10.2 - Fix saving row issue + 0.11.0 + Add triggers feature https://msdnshared.blob.core.windows.net/media/2018/04/Blazor-300x280.jpg https://dotnet.myget.org/F/aspnetcore-dev/api/v3/index.json; diff --git a/src/Blazor.FlexGrid/Components/GridViewGroup.cs b/src/Blazor.FlexGrid/Components/GridViewGroup.cs index b52f34c..c129053 100644 --- a/src/Blazor.FlexGrid/Components/GridViewGroup.cs +++ b/src/Blazor.FlexGrid/Components/GridViewGroup.cs @@ -24,7 +24,7 @@ protected override Task OnInitializedAsync() protected override async Task OnParametersSetAsync() { - tableDataSet = GetTableDataSet(); + GetTableDataSet(); await tableDataSet.GoToPage(0); } } diff --git a/src/Blazor.FlexGrid/Components/GridViewInternal.cs b/src/Blazor.FlexGrid/Components/GridViewInternal.cs index 94c081c..867e0ac 100644 --- a/src/Blazor.FlexGrid/Components/GridViewInternal.cs +++ b/src/Blazor.FlexGrid/Components/GridViewInternal.cs @@ -9,9 +9,9 @@ using Blazor.FlexGrid.Features; using Blazor.FlexGrid.Filters; using Blazor.FlexGrid.Permission; +using Blazor.FlexGrid.Triggers; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Rendering; -using Microsoft.AspNetCore.Components.RenderTree; using System; using System.Collections.Generic; using System.Linq; @@ -25,9 +25,14 @@ public class GridViewInternal : ComponentBase Enumerable.Empty().AsQueryable(), new FilterExpressionTreeBuilder()); private bool tableDataSetInitialized; + private int pageSize; + private ITableDataAdapter dataAdapter; + private ILazyLoadingOptions lazyLoadingOptions; private FlexGridContext fixedFlexGridContext; private (ImutableGridRendererContext ImutableRendererContext, PermissionContext PermissionContext) gridRenderingContexts; + private IParameterActionTriggerCollection actionTriggerCollection; + protected IEnumerable features; protected ITableDataSet tableDataSet; @@ -39,12 +44,47 @@ public class GridViewInternal : ComponentBase [Inject] ConventionsSet ConventionsSet { get; set; } - [Parameter] public ITableDataAdapter DataAdapter { get; set; } - - [Parameter] public ILazyLoadingOptions LazyLoadingOptions { get; set; } = new LazyLoadingOptions(); + [Parameter] + public ITableDataAdapter DataAdapter + { + get => dataAdapter; + set + { + if (dataAdapter != value) + { + dataAdapter = value; + AddTrigger(() => new RefreshDataAdapterTrigger(GetTableDataSet)); + } + } + } - [Parameter] public int PageSize { get; set; } + [Parameter] + public ILazyLoadingOptions LazyLoadingOptions + { + get => lazyLoadingOptions; + set + { + if (lazyLoadingOptions != value) + { + lazyLoadingOptions = value; + AddTrigger(() => new RefreshLazyLoadingOptionsTrigger(tableDataSet, value)); + } + } + } + [Parameter] + public int PageSize + { + get => pageSize; + set + { + if (pageSize != value) + { + pageSize = value; + AddTrigger(() => new RefreshPageSizeTrigger(tableDataSet.PageableOptions, value)); + } + } + } [Parameter] public Action SaveOperationFinished { get; set; } [Parameter] public Action DeleteOperationFinished { get; set; } @@ -61,6 +101,7 @@ public GridViewInternal() protected GridViewInternal(IEnumerable features) { this.features = features ?? throw new ArgumentNullException(nameof(features)); + this.actionTriggerCollection = new TriggerActionCollection(); } protected override void BuildRenderTree(RenderTreeBuilder builder) @@ -114,7 +155,7 @@ protected override async Task OnInitializedAsync() ConventionsSet.ApplyConventions(DataAdapter.UnderlyingTypeOfItem); } - tableDataSet = GetTableDataSet(); + GetTableDataSet(); await tableDataSet.GoToPage(0); if (DataAdapter != null) @@ -129,11 +170,18 @@ protected override async Task OnParametersSetAsync() DataAdapter != null) { ConventionsSet.ApplyConventions(DataAdapter.UnderlyingTypeOfItem); - tableDataSet = GetTableDataSet(); + GetTableDataSet(); await tableDataSet.GoToPage(0); fixedFlexGridContext.FirstPageLoaded = true; } + + if (fixedFlexGridContext.FirstPageLoaded) + { + await actionTriggerCollection.ExecuteTriggers(() => + tableDataSet.GoToPage(tableDataSet.PageableOptions.CurrentPage) + ); + } } protected virtual FlexGridContext CreateFlexGridContext() @@ -141,7 +189,7 @@ protected virtual FlexGridContext CreateFlexGridContext() protected ITableDataSet GetTableDataSet() { - var tableDataSet = DataAdapter?.GetTableDataSet(conf => + tableDataSet = DataAdapter?.GetTableDataSet(conf => { conf.LazyLoadingOptions = LazyLoadingOptions; conf.PageableOptions.PageSize = PageSize; @@ -160,6 +208,7 @@ protected ITableDataSet GetTableDataSet() } else { + DataAdapter.AfterReloadPage = fixedFlexGridContext.RequestRerenderTableRowsNotification; tableDataSet = MasterDetailTableDataSetFactory.ConvertToMasterTableIfIsRequired(tableDataSet); if (fixedFlexGridContext.IsFeatureActive()) { @@ -190,5 +239,13 @@ private void FilterChanged(object sender, FilterChangedEventArgs e) tableDataSet.ApplyFilters(e.Filters); fixedFlexGridContext.RequestRerenderTableRowsNotification?.Invoke(); } + + private void AddTrigger(Func createTrigger) + { + if (tableDataSetInitialized) + { + actionTriggerCollection.AddTrigger(createTrigger()); + } + } } } diff --git a/src/Blazor.FlexGrid/DataAdapters/BaseTableDataAdapter.cs b/src/Blazor.FlexGrid/DataAdapters/BaseTableDataAdapter.cs index 256c6e7..5fcc096 100644 --- a/src/Blazor.FlexGrid/DataAdapters/BaseTableDataAdapter.cs +++ b/src/Blazor.FlexGrid/DataAdapters/BaseTableDataAdapter.cs @@ -1,16 +1,32 @@ using Blazor.FlexGrid.DataSet; using Blazor.FlexGrid.DataSet.Options; using System; +using System.Threading.Tasks; namespace Blazor.FlexGrid.DataAdapters { public abstract class BaseTableDataAdapter : ITableDataAdapter { + protected ITableDataSet currentTableDataSet; + public abstract Type UnderlyingTypeOfItem { get; } + public Action AfterReloadPage { get; set; } + public void Accept(IDataTableAdapterVisitor dataTableAdapterVisitor) => dataTableAdapterVisitor?.Visit(this); + public async Task ReloadCurrentPage() + { + if (currentTableDataSet is null) + { + return; + } + + await currentTableDataSet.GoToPage(currentTableDataSet.PageableOptions.CurrentPage); + AfterReloadPage?.Invoke(); + } + public abstract ITableDataSet GetTableDataSet(Action configureDataSet); public abstract object Clone(); diff --git a/src/Blazor.FlexGrid/DataAdapters/CollectionTableDataAdapter.cs b/src/Blazor.FlexGrid/DataAdapters/CollectionTableDataAdapter.cs index eecc843..8f460f7 100644 --- a/src/Blazor.FlexGrid/DataAdapters/CollectionTableDataAdapter.cs +++ b/src/Blazor.FlexGrid/DataAdapters/CollectionTableDataAdapter.cs @@ -31,7 +31,7 @@ public override ITableDataSet GetTableDataSet(Action config var tableDataSetOptions = new TableDataSetOptions(); configureDataSet?.Invoke(tableDataSetOptions); - var tableDataSet = new TableDataSet(items.Where(Filter.Compile()).AsQueryable(), new FilterExpressionTreeBuilder()) + currentTableDataSet = new TableDataSet(items.Where(Filter.Compile()).AsQueryable(), new FilterExpressionTreeBuilder()) { PageableOptions = tableDataSetOptions.PageableOptions, SortingOptions = tableDataSetOptions.SortingOptions, @@ -39,7 +39,7 @@ public override ITableDataSet GetTableDataSet(Action config GroupingOptions = tableDataSetOptions.GroupingOptions, }; - return tableDataSet; + return currentTableDataSet; } public override object Clone() diff --git a/src/Blazor.FlexGrid/DataAdapters/IRefreshableDataSource.cs b/src/Blazor.FlexGrid/DataAdapters/IRefreshableDataSource.cs new file mode 100644 index 0000000..a24ce2c --- /dev/null +++ b/src/Blazor.FlexGrid/DataAdapters/IRefreshableDataSource.cs @@ -0,0 +1,12 @@ +using System; +using System.Threading.Tasks; + +namespace Blazor.FlexGrid.DataAdapters +{ + public interface IRefreshableDataSource + { + Action AfterReloadPage { get; set; } + + Task ReloadCurrentPage(); + } +} diff --git a/src/Blazor.FlexGrid/DataAdapters/ITableDataAdapter.cs b/src/Blazor.FlexGrid/DataAdapters/ITableDataAdapter.cs index 9d50b25..878efce 100644 --- a/src/Blazor.FlexGrid/DataAdapters/ITableDataAdapter.cs +++ b/src/Blazor.FlexGrid/DataAdapters/ITableDataAdapter.cs @@ -7,7 +7,7 @@ namespace Blazor.FlexGrid.DataAdapters /// /// Define contract which create and configure for GridComponent /// - public interface ITableDataAdapter : ICloneable + public interface ITableDataAdapter : IRefreshableDataSource, ICloneable { Type UnderlyingTypeOfItem { get; } diff --git a/src/Blazor.FlexGrid/DataAdapters/LazyLoadedTableDataAdapter.cs b/src/Blazor.FlexGrid/DataAdapters/LazyLoadedTableDataAdapter.cs index a6e425a..b865733 100644 --- a/src/Blazor.FlexGrid/DataAdapters/LazyLoadedTableDataAdapter.cs +++ b/src/Blazor.FlexGrid/DataAdapters/LazyLoadedTableDataAdapter.cs @@ -37,7 +37,7 @@ public override ITableDataSet GetTableDataSet(Action config var lazyLoadingOptions = tableDataSetOptions.LazyLoadingOptions; AddRequestParamsAction?.Invoke(lazyLoadingOptions.RequestParams); - var tableDataSet = new LazyTableDataSet(lazyDataSetLoader, lazyGroupableDataSetLoader, lazyDataSetItemSaver) + currentTableDataSet = new LazyTableDataSet(lazyDataSetLoader, lazyGroupableDataSetLoader, lazyDataSetItemSaver) { LazyLoadingOptions = lazyLoadingOptions, PageableOptions = tableDataSetOptions.PageableOptions, @@ -46,7 +46,7 @@ public override ITableDataSet GetTableDataSet(Action config GridViewEvents = tableDataSetOptions.GridViewEvents }; - return tableDataSet; + return currentTableDataSet; } public override object Clone() diff --git a/src/Blazor.FlexGrid/DataSet/Options/ILazyLoadingOptions.cs b/src/Blazor.FlexGrid/DataSet/Options/ILazyLoadingOptions.cs index a9b71c0..6a3d62e 100644 --- a/src/Blazor.FlexGrid/DataSet/Options/ILazyLoadingOptions.cs +++ b/src/Blazor.FlexGrid/DataSet/Options/ILazyLoadingOptions.cs @@ -11,5 +11,7 @@ public interface ILazyLoadingOptions string DeleteUri { get; set; } QueryBuilder RequestParams { get; } + + void Copy(ILazyLoadingOptions lazyLoadingOptions); } } diff --git a/src/Blazor.FlexGrid/DataSet/Options/LazyLoadingOptions.cs b/src/Blazor.FlexGrid/DataSet/Options/LazyLoadingOptions.cs index c0447a0..b20700a 100644 --- a/src/Blazor.FlexGrid/DataSet/Options/LazyLoadingOptions.cs +++ b/src/Blazor.FlexGrid/DataSet/Options/LazyLoadingOptions.cs @@ -10,6 +10,14 @@ public class LazyLoadingOptions : ILazyLoadingOptions public string DeleteUri { get; set; } - public QueryBuilder RequestParams { get; } = new QueryBuilder(); + public QueryBuilder RequestParams { get; private set; } = new QueryBuilder(); + + public void Copy(ILazyLoadingOptions lazyLoadingOptions) + { + DataUri = lazyLoadingOptions.DataUri; + PutDataUri = lazyLoadingOptions.PutDataUri; + DeleteUri = lazyLoadingOptions.DeleteUri; + RequestParams = lazyLoadingOptions.RequestParams; + } } } diff --git a/src/Blazor.FlexGrid/DataSet/Options/PageableOptions.cs b/src/Blazor.FlexGrid/DataSet/Options/PageableOptions.cs index 3ad2b55..06bf905 100644 --- a/src/Blazor.FlexGrid/DataSet/Options/PageableOptions.cs +++ b/src/Blazor.FlexGrid/DataSet/Options/PageableOptions.cs @@ -4,7 +4,7 @@ namespace Blazor.FlexGrid.DataSet.Options { public class PageableOptions : IPagingOptions { - public int PageSize { get; set; } + public int PageSize { get; set; } = 5; public int TotalItemsCount { get; set; } diff --git a/src/Blazor.FlexGrid/Triggers/IParameterActionTriggerCollection.cs b/src/Blazor.FlexGrid/Triggers/IParameterActionTriggerCollection.cs new file mode 100644 index 0000000..00f2778 --- /dev/null +++ b/src/Blazor.FlexGrid/Triggers/IParameterActionTriggerCollection.cs @@ -0,0 +1,12 @@ +using System; +using System.Threading.Tasks; + +namespace Blazor.FlexGrid.Triggers +{ + public interface IParameterActionTriggerCollection : ITriggerCollection + { + bool AddTrigger(IParamterChangedTrigger trigger); + + Task ExecuteTriggers(Func onActionExecuted); + } +} diff --git a/src/Blazor.FlexGrid/Triggers/IParamterChangedTrigger.cs b/src/Blazor.FlexGrid/Triggers/IParamterChangedTrigger.cs new file mode 100644 index 0000000..2e08238 --- /dev/null +++ b/src/Blazor.FlexGrid/Triggers/IParamterChangedTrigger.cs @@ -0,0 +1,10 @@ +namespace Blazor.FlexGrid.Triggers +{ + public interface IParamterChangedTrigger : ITrigger + { + /// + /// Indicates that trigger is for paramter which recreate whole table data set + /// + bool RefreshPage { get; } + } +} diff --git a/src/Blazor.FlexGrid/Triggers/ITrigger.cs b/src/Blazor.FlexGrid/Triggers/ITrigger.cs new file mode 100644 index 0000000..86a4511 --- /dev/null +++ b/src/Blazor.FlexGrid/Triggers/ITrigger.cs @@ -0,0 +1,9 @@ +using System.Threading.Tasks; + +namespace Blazor.FlexGrid.Triggers +{ + public interface ITrigger + { + Task Execute(); + } +} diff --git a/src/Blazor.FlexGrid/Triggers/ITriggerCollection.cs b/src/Blazor.FlexGrid/Triggers/ITriggerCollection.cs new file mode 100644 index 0000000..b1a210e --- /dev/null +++ b/src/Blazor.FlexGrid/Triggers/ITriggerCollection.cs @@ -0,0 +1,9 @@ +using System.Threading.Tasks; + +namespace Blazor.FlexGrid.Triggers +{ + public interface ITriggerCollection + { + Task ExecuteTriggers(); + } +} diff --git a/src/Blazor.FlexGrid/Triggers/RefreshDataAdapterTrigger.cs b/src/Blazor.FlexGrid/Triggers/RefreshDataAdapterTrigger.cs new file mode 100644 index 0000000..ebfc86b --- /dev/null +++ b/src/Blazor.FlexGrid/Triggers/RefreshDataAdapterTrigger.cs @@ -0,0 +1,24 @@ +using Blazor.FlexGrid.DataSet; +using System; +using System.Threading.Tasks; + +namespace Blazor.FlexGrid.Triggers +{ + public class RefreshDataAdapterTrigger : IParamterChangedTrigger + { + private readonly Func createDataSet; + + public bool RefreshPage => true; + + public RefreshDataAdapterTrigger(Func createDataSet) + { + this.createDataSet = createDataSet ?? throw new ArgumentNullException(nameof(createDataSet)); + } + + public async Task Execute() + { + var tableDataSet = createDataSet(); + await tableDataSet.GoToPage(0); + } + } +} diff --git a/src/Blazor.FlexGrid/Triggers/RefreshLazyLoadingOptionsTrigger.cs b/src/Blazor.FlexGrid/Triggers/RefreshLazyLoadingOptionsTrigger.cs new file mode 100644 index 0000000..4478be6 --- /dev/null +++ b/src/Blazor.FlexGrid/Triggers/RefreshLazyLoadingOptionsTrigger.cs @@ -0,0 +1,31 @@ +using Blazor.FlexGrid.DataSet; +using Blazor.FlexGrid.DataSet.Options; +using System; +using System.Threading.Tasks; + +namespace Blazor.FlexGrid.Triggers +{ + class RefreshLazyLoadingOptionsTrigger : IParamterChangedTrigger + { + private readonly ITableDataSet tableDataSet; + private readonly ILazyLoadingOptions newLazyLoadingOptions; + + public bool RefreshPage => false; + + public RefreshLazyLoadingOptionsTrigger(ITableDataSet tableDataSet, ILazyLoadingOptions newLazyLoadingOptions) + { + this.tableDataSet = tableDataSet ?? throw new ArgumentNullException(nameof(tableDataSet)); + this.newLazyLoadingOptions = newLazyLoadingOptions ?? throw new ArgumentNullException(nameof(newLazyLoadingOptions)); + } + + public Task Execute() + { + if (tableDataSet is ILazyTableDataSet lazyTableDataSet) + { + lazyTableDataSet.LazyLoadingOptions.Copy(newLazyLoadingOptions); + } + + return Task.CompletedTask; + } + } +} diff --git a/src/Blazor.FlexGrid/Triggers/RefreshPageSizeTrigger.cs b/src/Blazor.FlexGrid/Triggers/RefreshPageSizeTrigger.cs new file mode 100644 index 0000000..0460c5f --- /dev/null +++ b/src/Blazor.FlexGrid/Triggers/RefreshPageSizeTrigger.cs @@ -0,0 +1,33 @@ +using Blazor.FlexGrid.DataSet.Options; +using System; +using System.Threading.Tasks; + +namespace Blazor.FlexGrid.Triggers +{ + public class RefreshPageSizeTrigger : IParamterChangedTrigger + { + private readonly IPagingOptions pagingOptions; + private readonly int newPageSize; + + public bool RefreshPage => false; + + public RefreshPageSizeTrigger(IPagingOptions pagingOptions, int newPageSize) + { + this.pagingOptions = pagingOptions ?? throw new ArgumentNullException(nameof(pagingOptions)); + this.newPageSize = newPageSize; + } + + public Task Execute() + { + var lastCurrentPage = pagingOptions.CurrentPage; + pagingOptions.PageSize = newPageSize; + + if (lastCurrentPage > pagingOptions.PagesCount) + { + pagingOptions.CurrentPage = pagingOptions.PagesCount - 1; + } + + return Task.CompletedTask; + } + } +} diff --git a/src/Blazor.FlexGrid/Triggers/TriggerActionCollection.cs b/src/Blazor.FlexGrid/Triggers/TriggerActionCollection.cs new file mode 100644 index 0000000..ae70c82 --- /dev/null +++ b/src/Blazor.FlexGrid/Triggers/TriggerActionCollection.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Blazor.FlexGrid.Triggers +{ + public class TriggerActionCollection : IParameterActionTriggerCollection + { + private readonly List paramterChangedTriggers; + + private bool HasActionWithRefresh => paramterChangedTriggers.Any(t => t.RefreshPage); + + public TriggerActionCollection() + { + this.paramterChangedTriggers = new List(); + } + + public bool AddTrigger(IParamterChangedTrigger trigger) + { + if (trigger is null) + { + throw new ArgumentNullException(nameof(trigger)); + } + + if (paramterChangedTriggers.Any(t => t.RefreshPage)) + { + return false; + } + + paramterChangedTriggers.Add(trigger); + + return true; + } + + public async Task ExecuteTriggers() + { + await Task.WhenAll(paramterChangedTriggers.Select(t => t.Execute())); + + return; + } + + public async Task ExecuteTriggers(Func onActionExecuted) + { + if (!paramterChangedTriggers.Any()) + { + return; + } + + await ExecuteTriggers(); + + if (!HasActionWithRefresh) + { + Console.WriteLine("ExecuteTriggers Refresh"); + await onActionExecuted(); + } + + paramterChangedTriggers.Clear(); + } + } +}