Skip to content

Commit

Permalink
GetTypeAndValue method on GridColumnBase
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavnavar committed Nov 12, 2019
1 parent 6a7d5bf commit 9628d5b
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 68 deletions.
22 changes: 22 additions & 0 deletions GridBlazor/Columns/GridColumnBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;

namespace GridBlazor.Columns
{
Expand Down Expand Up @@ -230,6 +231,27 @@ public string GetFormatedValue(object value)
return textValue;
}

public ValueTuple<Type,object> GetTypeAndValue(T item)
{
var names = FieldName.Split('.');
PropertyInfo pi = null;
var type = item.GetType();
object value = item;
for (int i = 0; i < names.Length; i++)
{
pi = type.GetProperty(names[i]);
bool isNullable = pi.PropertyType.GetTypeInfo().IsGenericType &&
pi.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>);
type = isNullable ? Nullable.GetUnderlyingType(pi.PropertyType) : pi.PropertyType;

if (value != null)
{
value = pi.GetValue(value, null);
}
}
return (type, value);
}

public abstract bool FilterEnabled { get; set; }

public ColumnFilterValue InitialFilterSettings { get; set; }
Expand Down
21 changes: 4 additions & 17 deletions GridBlazor/GridCreateComponent.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@using GridBlazor.Resources
@using System.Reflection
@using GridShared.Columns

@typeparam T
@inherits GridCreateComponentBase<T>
Expand All @@ -13,22 +13,9 @@
<div class="form-horizontal">
@foreach (var column in GridComponent.Grid.Columns)
{
var names = column.FieldName.Split('.');
PropertyInfo pi = null;
var type = Item.GetType();
object value = Item;
for (int i = 0; i < names.Length; i++)
{
pi = type.GetProperty(names[i]);
bool isNullable = pi.PropertyType.GetTypeInfo().IsGenericType &&
pi.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>);
type = isNullable ? Nullable.GetUnderlyingType(pi.PropertyType) : pi.PropertyType;

if (value != null)
{
value = pi.GetValue(value, null);
}
}
var typeAndValue = ((IGridColumn<T>)column).GetTypeAndValue(Item);
var type = typeAndValue.Item1;
object value = typeAndValue.Item2;
<div class="form-group" style="@(column.CrudHidden?"display:none;":"")">
<label for="@column.FieldName" class="control-label col-md-2">@column.Title</label>
<div class="@(type == typeof(bool)?"col-md-1":"col-md-5")">
Expand Down
21 changes: 4 additions & 17 deletions GridBlazor/GridDeleteComponent.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@using GridBlazor.Resources
@using System.Reflection
@using GridShared.Columns

@typeparam T
@inherits GridDeleteComponentBase<T>
Expand All @@ -9,22 +9,9 @@
<div class="form-horizontal">
@foreach (var column in GridComponent.Grid.Columns)
{
var names = column.FieldName.Split('.');
PropertyInfo pi = null;
var type = Item.GetType();
object value = Item;
for (int i = 0; i < names.Length; i++)
{
pi = type.GetProperty(names[i]);
bool isNullable = pi.PropertyType.GetTypeInfo().IsGenericType &&
pi.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>);
type = isNullable ? Nullable.GetUnderlyingType(pi.PropertyType) : pi.PropertyType;

if (value != null)
{
value = pi.GetValue(value, null);
}
}
var typeAndValue = ((IGridColumn<T>)column).GetTypeAndValue(Item);
var type = typeAndValue.Item1;
object value = typeAndValue.Item2;
<div class="form-group" style="@(column.CrudHidden?"display:none;":"")">
<label for="@column.FieldName" class="control-label col-md-2">@column.Title</label>
<div class="@(type == typeof(bool)?"col-md-1":"col-md-5")">
Expand Down
21 changes: 4 additions & 17 deletions GridBlazor/GridReadComponent.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@using GridBlazor.Resources
@using System.Reflection
@using GridShared.Columns

@typeparam T
@inherits GridReadComponentBase<T>
Expand All @@ -9,22 +9,9 @@
<div class="form-horizontal">
@foreach (var column in GridComponent.Grid.Columns)
{
var names = column.FieldName.Split('.');
PropertyInfo pi = null;
var type = Item.GetType();
object value = Item;
for (int i = 0; i < names.Length; i++)
{
pi = type.GetProperty(names[i]);
bool isNullable = pi.PropertyType.GetTypeInfo().IsGenericType &&
pi.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>);
type = isNullable ? Nullable.GetUnderlyingType(pi.PropertyType) : pi.PropertyType;

if (value != null)
{
value = pi.GetValue(value, null);
}
}
var typeAndValue = ((IGridColumn<T>)column).GetTypeAndValue(Item);
var type = typeAndValue.Item1;
object value = typeAndValue.Item2;
<div class="form-group" style="@(column.CrudHidden?"display:none;":"")">
<label for="@column.FieldName" class="control-label col-md-2">@column.Title</label>
<div class="@(type == typeof(bool)?"col-md-1":"col-md-5")">
Expand Down
21 changes: 4 additions & 17 deletions GridBlazor/GridUpdateComponent.razor
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@using GridBlazor.Resources
@using System.Reflection
@using GridShared.Columns

@typeparam T
@inherits GridUpdateComponentBase<T>
Expand All @@ -13,22 +13,9 @@
<div class="form-horizontal">
@foreach (var column in GridComponent.Grid.Columns)
{
var names = column.FieldName.Split('.');
PropertyInfo pi = null;
var type = Item.GetType();
object value = Item;
for (int i = 0; i < names.Length; i++)
{
pi = type.GetProperty(names[i]);
bool isNullable = pi.PropertyType.GetTypeInfo().IsGenericType &&
pi.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>);
type = isNullable ? Nullable.GetUnderlyingType(pi.PropertyType) : pi.PropertyType;

if (value != null)
{
value = pi.GetValue(value, null);
}
}
var typeAndValue = ((IGridColumn<T>)column).GetTypeAndValue(Item);
var type = typeAndValue.Item1;
object value = typeAndValue.Item2;
<div class="form-group" style="@(column.CrudHidden?"display:none;":"")">
<label for="@column.FieldName" class="control-label col-md-2">@column.Title</label>
<div class="@(type == typeof(bool)?"col-md-1":"col-md-5")">
Expand Down
22 changes: 22 additions & 0 deletions GridMvc/Columns/GridColumnBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Collections.Generic;
using System.Globalization;
using System.Linq.Expressions;
using System.Reflection;

namespace GridMvc.Columns
{
Expand Down Expand Up @@ -236,6 +237,27 @@ public string GetFormatedValue(object value)
return textValue;
}

public ValueTuple<Type, object> GetTypeAndValue(T item)
{
var names = FieldName.Split('.');
PropertyInfo pi = null;
var type = item.GetType();
object value = item;
for (int i = 0; i < names.Length; i++)
{
pi = type.GetProperty(names[i]);
bool isNullable = pi.PropertyType.GetTypeInfo().IsGenericType &&
pi.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>);
type = isNullable ? Nullable.GetUnderlyingType(pi.PropertyType) : pi.PropertyType;

if (value != null)
{
value = pi.GetValue(value, null);
}
}
return (type, value);
}

public abstract bool FilterEnabled { get; set; }

public ColumnFilterValue InitialFilterSettings { get; set; }
Expand Down
1 change: 1 addition & 0 deletions GridShared/Columns/IGridColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public interface IGridColumn<T> : IGridColumn, IColumn<T>, ISortableColumn<T>,
IFilterableColumn<T>, ISearchableColumn<T>, IGroupableColumn<T>
{
IGridCell GetValue(T instance);
ValueTuple<Type, object> GetTypeAndValue(T item);
}

public interface IGridColumn : ISortableColumn, IFilterableColumn
Expand Down

0 comments on commit 9628d5b

Please sign in to comment.