Skip to content

Commit

Permalink
Merge pull request #92 from Mewriick/feature/Triggers
Browse files Browse the repository at this point in the history
Feature/triggers
  • Loading branch information
Mewriick authored Jan 12, 2020
2 parents 0d8d684 + f9160c4 commit dcbe863
Show file tree
Hide file tree
Showing 24 changed files with 371 additions and 32 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ GridView component for Blazor
<script src="_content/Blazor.FlexGrid/flexGridIntereop.js"></script>
```

**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)
Expand Down
25 changes: 19 additions & 6 deletions demo/Blazor.Components.Demo.FlexGrid/Pages/Grid.razor
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
<h1>Weather forecast</h1>

@*<BlazorButton>Test</BlazorButton>*@
<GridView DataAdapter="@dataAdapter" PageSize="5"></GridView>
<GridView DataAdapter="@dataAdapter" PageSize="@pageSize" LazyLoadingOptions="@op"></GridView>
<button class="btn btn-primary" @onclick="@SetPageSize">Click me</button>
<button class="btn btn-primary" @onclick="@ReloadCurrentPage">Reload data</button>

@*@{
RenderFragment<WeatherForecast> weatherTemp = (weather) => @<MatButton Raised="true" Icon="favorite">@weather.Summary</MatButton>;
Expand All @@ -33,16 +35,27 @@ Collection.AddColumnEditValueRenderer(w => w.Summary, editWeatherSummary);


@code {
CollectionTableDataAdapter<WeatherForecast>
dataAdapter;
bool forceReload;
CollectionTableDataAdapter<WeatherForecast> dataAdapter;

int pageSize = 5;
LazyLoadingOptions op;

protected override async Task OnInitializedAsync()
{
var forecast = await WeatherService.GetForecastAsync(DateTime.Now);
dataAdapter = new CollectionTableDataAdapter<WeatherForecast>
(forecast);
dataAdapter = new CollectionTableDataAdapter<WeatherForecast>(forecast);
}


async Task SetPageSize()
{
pageSize++;
var forecast = await WeatherService.GetForecastAsync(new DateTime(2020, 3, 19));
dataAdapter = new CollectionTableDataAdapter<WeatherForecast>(forecast);
}

async Task ReloadCurrentPage()
{
await dataAdapter.ReloadCurrentPage();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public Task<WeatherForecast[]> 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()
);
Expand Down
2 changes: 1 addition & 1 deletion demo/Blazor.Components.Demo.FlexGrid/_Imports.razor
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
@using Blazor.Components.Demo.FlexGrid.Services
@using Microsoft.AspNetCore.Mvc.TagHelpers
@using Blazor.FlexGrid

@using Blazor.FlexGrid.DataSet.Options
49 changes: 44 additions & 5 deletions demo/Blazor.FlexGrid.Demo.Client/Pages/LazyLoadedGrid.razor
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,35 @@
<p>This component demonstrates fetching data from the server.</p>


<GridView DataAdapter="@forecastAdapter"
LazyLoadingOptions="@(new LazyLoadingOptions() {
DataUri = "api/SampleData/WeatherForecasts",
PutDataUri = "api/SampleData/UpdateWeatherForecast",
DeleteUri = "api/SampleData/Delete/{Id}" })"
<GridView DataAdapter="@dataAdapter"
LazyLoadingOptions="@options"
PageSize="10"
SaveOperationFinished="@ItemSavedOperationFinished"
DeleteOperationFinished="@ItemDeletedOperationFinished"
NewItemCreated="@ItemCreated">
</GridView>

<button class="btn btn-primary" @onclick="@ChangeOptions">Change options</button>
<button class="btn btn-primary" @onclick="@ReloadCurrentPage">Reload data</button>
<button class="btn btn-primary" @onclick="@ChangeDataAdapter">Change data adapter</button>

@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}");
Expand All @@ -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<WeatherForecast[]>("api/SampleData/WeatherForecastsSimple");
dataAdapter = new CollectionTableDataAdapter<WeatherForecast>(forecast);
}
}


Expand Down
6 changes: 3 additions & 3 deletions src/Blazor.FlexGrid/Blazor.FlexGrid.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
<BlazorLinkOnBuild>false</BlazorLinkOnBuild>
<LangVersion>7.3</LangVersion>
<PackageId>Blazor.FlexGrid</PackageId>
<PackageVersion>0.10.2</PackageVersion>
<PackageVersion>0.11.0</PackageVersion>
<Title>Blazor.FlexGrid</Title>
<Description>GridView component for Blazor</Description>
<Authors>Jaroslav Surala</Authors>
<PackageProjectUrl>https://github.com/Mewriick/Blazor.FlexGrid</PackageProjectUrl>
<PackageTags>Blazor;GridView;Component</PackageTags>
<Version>0.10.2</Version>
<PackageReleaseNotes>Fix saving row issue</PackageReleaseNotes>
<Version>0.11.0</Version>
<PackageReleaseNotes>Add triggers feature</PackageReleaseNotes>
<PackageIconUrl>https://msdnshared.blob.core.windows.net/media/2018/04/Blazor-300x280.jpg</PackageIconUrl>
<RestoreAdditionalProjectSources>
https://dotnet.myget.org/F/aspnetcore-dev/api/v3/index.json;
Expand Down
2 changes: 1 addition & 1 deletion src/Blazor.FlexGrid/Components/GridViewGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ protected override Task OnInitializedAsync()

protected override async Task OnParametersSetAsync()
{
tableDataSet = GetTableDataSet();
GetTableDataSet();
await tableDataSet.GoToPage(0);
}
}
Expand Down
73 changes: 65 additions & 8 deletions src/Blazor.FlexGrid/Components/GridViewInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,9 +25,14 @@ public class GridViewInternal : ComponentBase
Enumerable.Empty<EmptyDataSetItem>().AsQueryable(), new FilterExpressionTreeBuilder<EmptyDataSetItem>());

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<IFeature> features;
protected ITableDataSet tableDataSet;

Expand All @@ -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<SaveResultArgs> SaveOperationFinished { get; set; }

[Parameter] public Action<DeleteResultArgs> DeleteOperationFinished { get; set; }
Expand All @@ -61,6 +101,7 @@ public GridViewInternal()
protected GridViewInternal(IEnumerable<IFeature> features)
{
this.features = features ?? throw new ArgumentNullException(nameof(features));
this.actionTriggerCollection = new TriggerActionCollection();
}

protected override void BuildRenderTree(RenderTreeBuilder builder)
Expand Down Expand Up @@ -114,7 +155,7 @@ protected override async Task OnInitializedAsync()
ConventionsSet.ApplyConventions(DataAdapter.UnderlyingTypeOfItem);
}

tableDataSet = GetTableDataSet();
GetTableDataSet();
await tableDataSet.GoToPage(0);

if (DataAdapter != null)
Expand All @@ -129,19 +170,26 @@ 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()
=> new FlexGridContext(new FilterContext(), new FeatureCollection(features));

protected ITableDataSet GetTableDataSet()
{
var tableDataSet = DataAdapter?.GetTableDataSet(conf =>
tableDataSet = DataAdapter?.GetTableDataSet(conf =>
{
conf.LazyLoadingOptions = LazyLoadingOptions;
conf.PageableOptions.PageSize = PageSize;
Expand All @@ -160,6 +208,7 @@ protected ITableDataSet GetTableDataSet()
}
else
{
DataAdapter.AfterReloadPage = fixedFlexGridContext.RequestRerenderTableRowsNotification;
tableDataSet = MasterDetailTableDataSetFactory.ConvertToMasterTableIfIsRequired(tableDataSet);
if (fixedFlexGridContext.IsFeatureActive<FilteringFeature>())
{
Expand Down Expand Up @@ -190,5 +239,13 @@ private void FilterChanged(object sender, FilterChangedEventArgs e)
tableDataSet.ApplyFilters(e.Filters);
fixedFlexGridContext.RequestRerenderTableRowsNotification?.Invoke();
}

private void AddTrigger(Func<IParamterChangedTrigger> createTrigger)
{
if (tableDataSetInitialized)
{
actionTriggerCollection.AddTrigger(createTrigger());
}
}
}
}
16 changes: 16 additions & 0 deletions src/Blazor.FlexGrid/DataAdapters/BaseTableDataAdapter.cs
Original file line number Diff line number Diff line change
@@ -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<TableDataSetOptions> configureDataSet);

public abstract object Clone();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public override ITableDataSet GetTableDataSet(Action<TableDataSetOptions> config
var tableDataSetOptions = new TableDataSetOptions();
configureDataSet?.Invoke(tableDataSetOptions);

var tableDataSet = new TableDataSet<TItem>(items.Where(Filter.Compile()).AsQueryable(), new FilterExpressionTreeBuilder<TItem>())
currentTableDataSet = new TableDataSet<TItem>(items.Where(Filter.Compile()).AsQueryable(), new FilterExpressionTreeBuilder<TItem>())
{
PageableOptions = tableDataSetOptions.PageableOptions,
SortingOptions = tableDataSetOptions.SortingOptions,
GridViewEvents = tableDataSetOptions.GridViewEvents,
GroupingOptions = tableDataSetOptions.GroupingOptions,
};

return tableDataSet;
return currentTableDataSet;
}

public override object Clone()
Expand Down
12 changes: 12 additions & 0 deletions src/Blazor.FlexGrid/DataAdapters/IRefreshableDataSource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Threading.Tasks;

namespace Blazor.FlexGrid.DataAdapters
{
public interface IRefreshableDataSource
{
Action AfterReloadPage { get; set; }

Task ReloadCurrentPage();
}
}
2 changes: 1 addition & 1 deletion src/Blazor.FlexGrid/DataAdapters/ITableDataAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Blazor.FlexGrid.DataAdapters
/// <summary>
/// Define contract which create and configure <seealso cref="ITableDataSet"/> for GridComponent
/// </summary>
public interface ITableDataAdapter : ICloneable
public interface ITableDataAdapter : IRefreshableDataSource, ICloneable
{
Type UnderlyingTypeOfItem { get; }

Expand Down
Loading

0 comments on commit dcbe863

Please sign in to comment.