Skip to content

Commit

Permalink
Blazor WebAssembly support
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavnavar committed Nov 22, 2019
1 parent 3e84b95 commit 6bf353c
Show file tree
Hide file tree
Showing 25 changed files with 346 additions and 342 deletions.
2 changes: 1 addition & 1 deletion GridBlazor.Tests/GridBlazor.Tests.csproj
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.0.0" />
<PackageReference Include="Moq" Version="4.13.1" />
<PackageReference Include="MSTest.TestAdapter" Version="2.0.0" />
Expand Down
6 changes: 4 additions & 2 deletions GridBlazor/CGrid.cs
Expand Up @@ -697,8 +697,10 @@ public async Task UpdateGrid()
string urlParameters = ((GridPager)_pager).GetLink();
if (Url.Contains("?"))
urlParameters = urlParameters.Replace("?", "&");
HttpClient httpClient = new HttpClient();
response = await httpClient.GetJsonAsync<ItemsDTO<T>>(Url + urlParameters);
using (HttpClient httpClient = new HttpClient())
{
response = await httpClient.GetJsonAsync<ItemsDTO<T>>(Url + urlParameters);
}
}
else
{
Expand Down
14 changes: 11 additions & 3 deletions GridBlazor/Columns/GridColumnBase.cs
Expand Up @@ -47,7 +47,9 @@ public abstract class GridColumnBase<T> : GridStyledColumn, IGridColumn<T>, ICGr

public bool IsPrimaryKey { get; protected set; } = false;

public (bool IsSelectKey, Func<T, string> Expression, Func<IEnumerable<Tuple<string, string>>> KeyValues) IsSelectField { get; protected set; } = (false, null, null);
public (bool IsSelectKey, Func<T, string> Expression, string Url, Func<IEnumerable<SelectItem>> SelectItemExpr) IsSelectField { get; protected set; } = (false, null, null, null);

public IEnumerable<SelectItem> SelectItems { get; internal set; }

public bool IsSumEnabled { get; internal set; } = false;

Expand Down Expand Up @@ -191,9 +193,15 @@ public IGridColumn<T> SetPrimaryKey(bool enabled)
return this;
}

public IGridColumn<T> SetSelectField(bool enabled, Func<T,string> expression, Func<IEnumerable<Tuple<string, string>>> keyValues)
public IGridColumn<T> SetSelectField(bool enabled, Func<T,string> expression, Func<IEnumerable<SelectItem>> selectItemExpr)
{
IsSelectField = (enabled, expression, null, selectItemExpr);
return this;
}

public IGridColumn<T> SetSelectField(bool enabled, Func<T, string> expression, string url)
{
IsSelectField = (enabled, expression, keyValues);
IsSelectField = (enabled, expression, url, null);
return this;
}

Expand Down
4 changes: 2 additions & 2 deletions GridBlazor/GridBlazor.csproj
Expand Up @@ -27,8 +27,8 @@
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Blazor.HttpClient" Version="3.0.0-preview9.19465.2" />
<PackageReference Include="Microsoft.AspNetCore.Html.Abstractions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.Primitives" Version="3.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="3.0.1" />
<PackageReference Include="Microsoft.Extensions.Primitives" Version="3.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
40 changes: 39 additions & 1 deletion GridBlazor/GridComponentBase.cs
Expand Up @@ -8,7 +8,9 @@
using GridShared.Utility;
using Microsoft.AspNetCore.Components;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

namespace GridBlazor
Expand All @@ -31,6 +33,9 @@ public class GridComponentBase<T> : ComponentBase

protected RenderFragment CrudRender { get; set; }

[Inject]
public NavigationManager NavigationManager { get; set; }

[Parameter]
public ICGrid Grid { get; set; }

Expand Down Expand Up @@ -173,8 +178,9 @@ public async Task RemoveExtSorting(ColumnOrderValue column)
await UpdateGrid();
}

public void CreateHandler()
public async Task CreateHandler()
{
await SetSelectFields();
_item = (T)Activator.CreateInstance(typeof(T));
((CGrid<T>)Grid).Mode = GridMode.Create;
if (Grid.CreateComponent != null)
Expand All @@ -197,6 +203,7 @@ public void ReadHandler(object item)

public async Task UpdateHandler(object item)
{
await SetSelectFields();
var keys = Grid.GetPrimaryKeyValues(item);
try
{
Expand Down Expand Up @@ -226,6 +233,37 @@ public void DeleteHandler(object item)
StateHasChanged();
}

protected async Task SetSelectFields()
{
foreach (var column in Grid.Columns)
{
var isSelectField = ((IGridColumn<T>)column).IsSelectField;
if (isSelectField.IsSelectKey)
{
try
{
if (isSelectField.SelectItemExpr == null)
{
using (HttpClient httpClient = new HttpClient())
{
var selectItems = await httpClient.GetJsonAsync<SelectItem[]>(NavigationManager.BaseUri + isSelectField.Url);
((GridColumnBase<T>)column).SelectItems = selectItems.ToList();
}
}
else
{
((GridColumnBase<T>)column).SelectItems = isSelectField.SelectItemExpr.Invoke();
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
((GridColumnBase<T>)column).SelectItems = new List<SelectItem>();
}
}
}
}

protected RenderFragment CreateCrudComponent() => builder =>
{
var componentType = Grid.CreateComponent;
Expand Down

0 comments on commit 6bf353c

Please sign in to comment.