Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
Feat145 (#184)
Browse files Browse the repository at this point in the history
* Added Dynamic Columns
* NuGet Update
  • Loading branch information
IvanJosipovic authored Oct 5, 2020
1 parent 77ebd7c commit 0932b24
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.6" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.6" />
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.8" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.8" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="System.Net.Http.Json" Version="3.2.1" />
</ItemGroup>
Expand Down
5 changes: 5 additions & 0 deletions src/BlazorTable.Sample.Shared/NavMenu.razor
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@
<span class="oi oi-home" aria-hidden="true"></span> Table Footer
</NavLink>
</li>
<li class="nav-item px-3">
<NavLink class="nav-link" href="/DynamicColumns" Match="NavLinkMatch.All">
<span class="oi oi-home" aria-hidden="true"></span> Dynamic Columns
</NavLink>
</li>
</ul>
</div>

Expand Down
45 changes: 45 additions & 0 deletions src/BlazorTable.Sample.Shared/Pages/DynamicColumn.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
@page "/DynamicColumns"

<h1>Dynamic Columns</h1>

<Table TableItem="PersonData" Items="data" PageSize="15" ShowSearchBar="true">
<DynamicColumns TableItem="PersonData" Sortable="true" Filterable="true" />
<Pager ShowPageNumber="true" ShowTotalCount="true" ShowPageSizes="true" />
</Table>

@code
{
[Inject] private HttpClient Http { get; set; }

private PersonData[] data;

protected override async Task OnInitializedAsync()
{
data = await Http.GetFromJsonAsync<PersonData[]>("sample-data/MOCK_DATA.json");
}

public class PersonData
{
public int? id { get; set; }
public string full_name { get; set; }
public string email { get; set; }
public bool? paid { get; set; }
public decimal? price { get; set; }
public CreditCard? cc_type { get; set; }
public DateTime? created_date { get; set; }
//public SubData SubData {get;set;}
}

public class SubData
{
public string test { get; set; }
}

public enum CreditCard
{
none = 0,
[Description("MasterCard")]
MasterCard = 1,
Visa = 2
}
}
4 changes: 2 additions & 2 deletions src/BlazorTable.Tests/BlazorTable.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="PuppeteerSharp" Version="2.0.4" />
<PackageReference Include="PuppeteerSharp.Contrib.Extensions" Version="2.0.0" />
<PackageReference Include="PuppeteerSharp.Contrib.Extensions" Version="3.0.0" />
<PackageReference Include="Shouldly" Version="3.0.2" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
Expand Down
6 changes: 3 additions & 3 deletions src/BlazorTable/BlazorTable.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@

<ItemGroup>
<PackageReference Include="LINQKit.Core" Version="1.1.17" />
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.6" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.6" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.0.0">
<PackageReference Include="Microsoft.AspNetCore.Components" Version="3.1.8" />
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="3.1.8" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="3.3.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
19 changes: 19 additions & 0 deletions src/BlazorTable/Components/DynamicColumns.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
@namespace BlazorTable
@typeparam TableItem

@foreach (var propertyInfo in (Type == null ? typeof(TableItem).GetProperties() : Type.GetProperties() ))
{
if(propertyInfo.PropertyType.MemberType == System.Reflection.MemberTypes.NestedType)
{
<DynamicColumns TableItem="TableItem" Type="@propertyInfo.PropertyType" Sortable="Sortable" Filterable="Sortable" />
} else {

var member = GenerateMemberExpression<TableItem, object>(propertyInfo);

<Column TableItem="TableItem" Title="@propertyInfo.Name" Type="Utilities.GetNonNullableType(propertyInfo.PropertyType)" Field="member" Sortable="Sortable" Filterable="Filterable" Class="Class">
<Template>
@RenderProperty(context, propertyInfo, member.Compile())
</Template>
</Column>
}
}
112 changes: 112 additions & 0 deletions src/BlazorTable/Components/DynamicColumns.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using Microsoft.AspNetCore.Components;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

namespace BlazorTable
{
/// <summary>
/// BlazorTable Dynamic Columns
/// </summary>
/// <typeparam name="TableItem"></typeparam>
public partial class DynamicColumns<TableItem>
{
/// <summary>
/// Parent Table
/// </summary>
[CascadingParameter(Name = "Table")]
public ITable<TableItem> Table { get; set; }

/// <summary>
/// Column can be sorted
/// </summary>
[Parameter]
public bool Sortable { get; set; }

/// <summary>
/// Column can be filtered
/// </summary>
[Parameter]
public bool Filterable { get; set; }

/// <summary>
/// Horizontal alignment
/// </summary>
[Parameter]
public Align Align { get; set; }

/// <summary>
/// Column CSS Class
/// </summary>
[Parameter]
public string Class { get; set; }

[Parameter]
public Type Type { get; set; }

private static Expression<Func<TModel, T>> GenerateMemberExpression<TModel, T>(PropertyInfo propertyInfo)
{
var entityParam = Expression.Parameter(typeof(TModel), "x");

Expression columnExpr = null;

if (propertyInfo.ReflectedType.Name == "JObject")
{
var assembly = AppDomain.CurrentDomain.GetAssemblies().Where(x => x.ManifestModule.Name == "Newtonsoft.Json.dll").First();

var type = propertyInfo.ReflectedType;// assembly.GetType("Newtonsoft.Json.Linq.JToken");
var exttype = assembly.GetType("Newtonsoft.Json.Linq.Extensions");

columnExpr = Expression.Call(exttype.GetMethod("Value", new[] { typeof(IEnumerable<>).MakeGenericType(type) }).MakeGenericMethod(new[] {type}),
Expression.Property(
Expression.Call(entityParam, "Property", null, Expression.Constant(propertyInfo.Name)),
"Value")
);
}
else
{
columnExpr = Expression.Property(entityParam, propertyInfo);

if (propertyInfo.PropertyType != typeof(T))
columnExpr = Expression.Convert(columnExpr, typeof(T));
}

return Expression.Lambda<Func<TModel, T>>(columnExpr, entityParam);
}

private string RenderProperty(TableItem data, PropertyInfo property, Func<TableItem, object> func = null)
{
if (property.ReflectedType.Name == "JObject")
{
return "";// func.Invoke(data)?.ToString();
}

object rawData = property.GetValue(data);

if (rawData == null)
return "";

if (rawData.GetType().IsEnum)
{
Type enumType = property.GetValue(data).GetType();

MemberInfo[] memberInfo = enumType.GetMember(rawData.ToString());
if (memberInfo != null && memberInfo.Length > 0)
{
object[] attrs = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);

if (attrs != null && attrs.Length > 0)
{
//Pull out the description value
return ((DescriptionAttribute)attrs[0]).Description;
}
}
}

return rawData.ToString();
}
}
}
16 changes: 8 additions & 8 deletions src/BlazorTable/Filters/StringFilter.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public Expression<Func<TableItem, bool>> GetFilter()
Column.Field.Body.CreateNullChecks(),
Expression.GreaterThanOrEqual(
Expression.Call(
Column.Field.Body,
Expression.Call(Column.Field.Body, "ToString", Type.EmptyTypes),
typeof(string).GetMethod(nameof(string.IndexOf), new[] { typeof(string), typeof(StringComparison) }),
new[] { Expression.Constant(FilterText), Expression.Constant(StringComparison.OrdinalIgnoreCase) }),
Expression.Constant(0))),
Expand All @@ -112,7 +112,7 @@ public Expression<Func<TableItem, bool>> GetFilter()
Column.Field.Body.CreateNullChecks(),
Expression.LessThanOrEqual(
Expression.Call(
Column.Field.Body,
Expression.Call(Column.Field.Body, "ToString", Type.EmptyTypes),
typeof(string).GetMethod(nameof(string.IndexOf), new[] { typeof(string), typeof(StringComparison) }),
new[] { Expression.Constant(FilterText), Expression.Constant(StringComparison.OrdinalIgnoreCase) }),
Expression.Constant(-1))),
Expand All @@ -123,7 +123,7 @@ public Expression<Func<TableItem, bool>> GetFilter()
Expression.AndAlso(
Column.Field.Body.CreateNullChecks(),
Expression.Call(
Column.Field.Body,
Expression.Call(Column.Field.Body, "ToString", Type.EmptyTypes),
typeof(string).GetMethod(nameof(string.StartsWith), new[] { typeof(string), typeof(StringComparison) }),
new[] { Expression.Constant(FilterText), Expression.Constant(StringComparison.OrdinalIgnoreCase) })),
Column.Field.Parameters),
Expand All @@ -133,7 +133,7 @@ public Expression<Func<TableItem, bool>> GetFilter()
Expression.AndAlso(
Column.Field.Body.CreateNullChecks(),
Expression.Call(
Column.Field.Body,
Expression.Call(Column.Field.Body, "ToString", Type.EmptyTypes),
typeof(string).GetMethod(nameof(string.EndsWith), new[] { typeof(string), typeof(StringComparison) }),
new[] { Expression.Constant(FilterText), Expression.Constant(StringComparison.OrdinalIgnoreCase) })),
Column.Field.Parameters),
Expand All @@ -143,7 +143,7 @@ public Expression<Func<TableItem, bool>> GetFilter()
Expression.AndAlso(
Column.Field.Body.CreateNullChecks(),
Expression.Call(
Column.Field.Body,
Expression.Call(Column.Field.Body, "ToString", Type.EmptyTypes),
typeof(string).GetMethod(nameof(string.Equals), new[] { typeof(string), typeof(StringComparison) }),
new[] { Expression.Constant(FilterText), Expression.Constant(StringComparison.OrdinalIgnoreCase) })),
Column.Field.Parameters),
Expand All @@ -154,7 +154,7 @@ public Expression<Func<TableItem, bool>> GetFilter()
Column.Field.Body.CreateNullChecks(),
Expression.Not(
Expression.Call(
Column.Field.Body,
Expression.Call(Column.Field.Body, "ToString", Type.EmptyTypes),
typeof(string).GetMethod(nameof(string.Equals), new[] { typeof(string), typeof(StringComparison) }),
new[] { Expression.Constant(FilterText), Expression.Constant(StringComparison.OrdinalIgnoreCase) }))),
Column.Field.Parameters),
Expand All @@ -165,7 +165,7 @@ public Expression<Func<TableItem, bool>> GetFilter()
Column.Field.Body.CreateNullChecks(true),
Expression.Call(
typeof(string).GetMethod(nameof(string.IsNullOrEmpty), new[] { typeof(string) }),
Column.Field.Body)),
Expression.Call(Column.Field.Body, "ToString", Type.EmptyTypes))),
Column.Field.Parameters),

StringCondition.IsNotNulOrEmpty =>
Expand All @@ -175,7 +175,7 @@ public Expression<Func<TableItem, bool>> GetFilter()
Expression.Not(
Expression.Call(
typeof(string).GetMethod(nameof(string.IsNullOrEmpty), new[] { typeof(string) }),
Column.Field.Body))),
Expression.Call(Column.Field.Body, "ToString", Type.EmptyTypes)))),
Column.Field.Parameters),

_ => throw new ArgumentException(Condition + " is not defined!"),
Expand Down

0 comments on commit 0932b24

Please sign in to comment.