This repository has been archived by the owner on Jul 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
196 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters