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

v0.0.6-alpha #12

Merged
merged 3 commits into from
Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# BlazorTable
[![Demo](https://img.shields.io/badge/Live-Demo-Blue?style=flat-square)](https://BlazorTable.netlify.com/)
[![Nuget (with prereleases)](https://img.shields.io/nuget/vpre/BlazorTable.svg?style=flat-square)](https://www.nuget.org/packages/BlazorTable)
[![Nuget (with prereleases)](https://img.shields.io/nuget/dt/BlazorTable.svg?style=flat-square)](https://www.nuget.org/packages/BlazorTable)
![](https://github.com/IvanJosipovic/BlazorTable/workflows/CI/CD/badge.svg)

**Work in progress!**
Expand Down
2 changes: 1 addition & 1 deletion src/BlazorTable.Sample/Pages/EditMode.razor
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
<input type="number" step=".01" @bind-value="@context.fund" class="form-control form-control-sm" />
</EditorTemplate>
</Column>
<Column TableItem="PersonData" Title="Created Date" Property="@(x => x.created_date)" Sortable="true" Width="10%">
<Column TableItem="PersonData" Title="Created Date" Property="@(x => x.created_date)" Sortable="true" Filterable="true" Width="10%">
<Template>
@(context.created_date.HasValue ? context.created_date.Value.ToShortDateString() : string.Empty)
</Template>
Expand Down
2 changes: 1 addition & 1 deletion src/BlazorTable.Sample/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
@(context.fund.HasValue ? context.fund.Value.ToString("C") : string.Empty)
</Template>
</Column>
<Column TableItem="PersonData" Title="Created Date" Property="@(x => x.created_date)" Sortable="true" Width="10%">
<Column TableItem="PersonData" Title="Created Date" Property="@(x => x.created_date)" Sortable="true" Filterable="true" Width="10%">
<Template>
@(context.created_date.HasValue ? context.created_date.Value.ToShortDateString() : string.Empty)
</Template>
Expand Down
2 changes: 1 addition & 1 deletion src/BlazorTable/BlazorTable.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<Authors>Ivan Josipovic</Authors>
<PackageProjectUrl>https://github.com/IvanJosipovic/BlazorTable</PackageProjectUrl>
<PackageTags>Blazor;Table;Grid;DataTable;Data;Sort;Filter;AspNetCore;AspNet</PackageTags>
<Version>0.0.5-alpha</Version>
<Version>0.0.6-alpha</Version>
<PackageReleaseNotes>Experimental</PackageReleaseNotes>
<PackageIcon>icon.png</PackageIcon>
</PropertyGroup>
Expand Down
6 changes: 5 additions & 1 deletion src/BlazorTable/Components/Column.razor
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
<StringFilter TableItem="TableItem" />
<BooleanFilter TableItem="TableItem" />
<NumberFilter TableItem="TableItem" />
@*@this.CustomIFilters(this)*@
<DateFilter TableItem="TableItem" />
@*@if (this.CustomIFilters != null)
{
this.CustomIFilters(this);
}*@
</FilterManager>

</BlazorStrap.BSPopoverBody>
Expand Down
3 changes: 1 addition & 2 deletions src/BlazorTable/Components/Column.razor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.AspNetCore.Components;
using System;
using System.Linq;
using System.Linq.Expressions;

namespace BlazorTable
Expand Down Expand Up @@ -50,7 +49,7 @@ public string Title

public Type Type { get; private set; }

public IFilterManager<TableItem> FilterManager { get; set; }
public IFilter<TableItem> FilterControl { get; set; }

public void Dispose()
{
Expand Down
2 changes: 1 addition & 1 deletion src/BlazorTable/Components/FilterManager.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@namespace BlazorTable
@typeparam TableItem

<CascadingValue Value="(IFilterManager<TableItem>)this" Name="FilterManager">
<CascadingValue Value="(IColumn<TableItem>)this.Column" Name="Column">
@ChildContent
</CascadingValue>
<br />
Expand Down
12 changes: 6 additions & 6 deletions src/BlazorTable/Components/FilterManager.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@

namespace BlazorTable
{
public partial class FilterManager<TableItem> : IFilterManager<TableItem>
public partial class FilterManager<TableItem>
{
[CascadingParameter(Name = "Column")] public IColumn<TableItem> Column { get; set; }

[Parameter] public RenderFragment ChildContent { get; set; }

[Inject] public ILogger<FilterManager<TableItem>> Logger { get; set; }

public IFilter<TableItem> Filter { get; set; }

protected override void OnInitialized()
{
Column.FilterManager = this;
}

private void ApplyFilter()
{
Column.ToggleFilter();
if (Filter != null)
if (Column.FilterControl != null)
{
Filter.ApplyFilter();
Column.Filter = Column.FilterControl.GetFilter();
Column.Table.Update();
Column.Table.FirstPage();
} else
{
Logger.LogInformation("Filter is null");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/BlazorTable/Filters/BooleanFilter.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@namespace BlazorTable
@typeparam TableItem

@if (FilterTypes.Contains(FilterManager.Column.Type.GetNonNullableType()))
@if (FilterTypes.Contains(Column.Type.GetNonNullableType()))
{
<select class="form-control form-control-sm" value="@Condition" @onchange="@((x) => Condition = (BooleanCondition)Enum.Parse(typeof(BooleanCondition), x.Value.ToString()))">
@foreach (var option in (BooleanCondition[])Enum.GetValues(typeof(BooleanCondition)))
Expand Down
51 changes: 26 additions & 25 deletions src/BlazorTable/Filters/BooleanFilter.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace BlazorTable
{
public partial class BooleanFilter<TableItem> : IFilter<TableItem>
{
[CascadingParameter(Name = "FilterManager")] public IFilterManager<TableItem> FilterManager { get; set; }
[CascadingParameter(Name = "Column")] public IColumn<TableItem> Column { get; set; }

[Inject] public ILogger<NumberFilter<TableItem>> Logger { get; set; }

Expand All @@ -22,15 +22,15 @@ public partial class BooleanFilter<TableItem> : IFilter<TableItem>

protected override void OnInitialized()
{
if (FilterTypes.Contains(FilterManager.Column.Type.GetNonNullableType()))
if (FilterTypes.Contains(Column.Type.GetNonNullableType()))
{
FilterManager.Filter = this;
Column.FilterControl = this;

if (FilterManager.Column.Filter != null)
if (Column.Filter != null)
{
var nodeType = FilterManager.Column.Filter.Body.NodeType;
var nodeType = Column.Filter.Body.NodeType;

if (FilterManager.Column.Filter.Body is BinaryExpression binaryExpression
if (Column.Filter.Body is BinaryExpression binaryExpression
&& binaryExpression.NodeType == ExpressionType.AndAlso)
{
nodeType = binaryExpression.Right.NodeType;
Expand All @@ -55,35 +55,36 @@ protected override void OnInitialized()
}
}

public void ApplyFilter()
public Expression<Func<TableItem, bool>> GetFilter()
{
switch (Condition)
{
case BooleanCondition.True:
FilterManager.Column.Filter = Expression.Lambda<Func<TableItem, bool>>(
return Expression.Lambda<Func<TableItem, bool>>(
Expression.AndAlso(
Expression.NotEqual(FilterManager.Column.Property.Body, Expression.Constant(null)),
Expression.IsTrue(Expression.Convert(FilterManager.Column.Property.Body, FilterManager.Column.Type.GetNonNullableType()))),
FilterManager.Column.Property.Parameters);
break;
Expression.NotEqual(Column.Property.Body, Expression.Constant(null)),
Expression.IsTrue(Expression.Convert(Column.Property.Body, Column.Type.GetNonNullableType()))),
Column.Property.Parameters);

case BooleanCondition.False:
FilterManager.Column.Filter = Expression.Lambda<Func<TableItem, bool>>(
return Expression.Lambda<Func<TableItem, bool>>(
Expression.AndAlso(
Expression.NotEqual(FilterManager.Column.Property.Body, Expression.Constant(null)),
Expression.IsFalse(Expression.Convert(FilterManager.Column.Property.Body, FilterManager.Column.Type.GetNonNullableType()))),
FilterManager.Column.Property.Parameters);
break;
Expression.NotEqual(Column.Property.Body, Expression.Constant(null)),
Expression.IsFalse(Expression.Convert(Column.Property.Body, Column.Type.GetNonNullableType()))),
Column.Property.Parameters);

case BooleanCondition.IsNull:
FilterManager.Column.Filter = Expression.Lambda<Func<TableItem, bool>>(
Expression.Equal(FilterManager.Column.Property.Body, Expression.Constant(null)),
FilterManager.Column.Property.Parameters);
break;
return Expression.Lambda<Func<TableItem, bool>>(
Expression.Equal(Column.Property.Body, Expression.Constant(null)),
Column.Property.Parameters);

case BooleanCondition.IsNotNull:
FilterManager.Column.Filter = Expression.Lambda<Func<TableItem, bool>>(
Expression.NotEqual(FilterManager.Column.Property.Body, Expression.Constant(null)),
FilterManager.Column.Property.Parameters);
break;
return Expression.Lambda<Func<TableItem, bool>>(
Expression.NotEqual(Column.Property.Body, Expression.Constant(null)),
Column.Property.Parameters);
}

return null;
}
}

Expand Down
16 changes: 16 additions & 0 deletions src/BlazorTable/Filters/DateFilter.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@namespace BlazorTable
@typeparam TableItem

@if (Column.Type.GetNonNullableType() == typeof(DateTime))
{
<select class="form-control form-control-sm" value="@Condition" @onchange="@((x) => Condition = (NumberCondition)Enum.Parse(typeof(NumberCondition), x.Value.ToString()))">
@foreach (var option in (NumberCondition[])Enum.GetValues(typeof(NumberCondition)))
{
<option value="@option">@option.GetDescription()</option>
}
</select>
@if (Condition != NumberCondition.IsNull && Condition != NumberCondition.IsNotNull)
{
<input type="date" class="form-control form-control-sm" @bind-value="FilterValue" />
}
}
145 changes: 145 additions & 0 deletions src/BlazorTable/Filters/DateFilter.razor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Logging;
using System;
using System.Globalization;
using System.Linq.Expressions;

namespace BlazorTable
{
public partial class DateFilter<TableItem> : IFilter<TableItem>
{
[CascadingParameter(Name = "Column")] public IColumn<TableItem> Column { get; set; }

[Inject] public ILogger<NumberFilter<TableItem>> Logger { get; set; }

private NumberCondition Condition { get; set; }

private DateTime FilterValue { get; set; } = DateTime.Now;

protected override void OnInitialized()
{
if (Column.Type.GetNonNullableType() == typeof(DateTime))
{
Column.FilterControl = this;

if (Column.Filter?.Body is BinaryExpression binaryExpression)
{
if (binaryExpression.NodeType == ExpressionType.AndAlso)
{
switch (binaryExpression.Right.NodeType)
{
case ExpressionType.Equal:
Condition = NumberCondition.IsEqualTo;
break;
case ExpressionType.NotEqual:
Condition = NumberCondition.IsNotEqualTo;
break;
case ExpressionType.GreaterThanOrEqual:
Condition = NumberCondition.IsGreaterThanOrEqualTo;
break;
case ExpressionType.GreaterThan:
Condition = NumberCondition.IsGreaterThan;
break;
case ExpressionType.LessThanOrEqual:
Condition = NumberCondition.IsLessThanOrEqualTo;
break;
case ExpressionType.LessThan:
Condition = NumberCondition.IsLessThan;
break;
}
}
else
{
if (binaryExpression.NodeType == ExpressionType.Equal)
{
Condition = NumberCondition.IsNull;
}
else if (binaryExpression.NodeType == ExpressionType.NotEqual)
{
Condition = NumberCondition.IsNotNull;
}
}

if (binaryExpression.Right is BinaryExpression binaryExpression2
&& binaryExpression2.Right is ConstantExpression constantExpression)
{
FilterValue = DateTime.Parse(constantExpression.Value.ToString(), CultureInfo.InvariantCulture);
}
}
}
}

public Expression<Func<TableItem, bool>> GetFilter()
{
switch (Condition)
{
case NumberCondition.IsEqualTo:
return Expression.Lambda<Func<TableItem, bool>>(
Expression.AndAlso(
Expression.NotEqual(Column.Property.Body, Expression.Constant(null)),
Expression.Equal(
Expression.Convert(Column.Property.Body, Column.Type.GetNonNullableType()),
Expression.Constant(FilterValue))),
Column.Property.Parameters);

case NumberCondition.IsNotEqualTo:
return Expression.Lambda<Func<TableItem, bool>>(
Expression.AndAlso(
Expression.NotEqual(Column.Property.Body, Expression.Constant(null)),
Expression.NotEqual(
Expression.Convert(Column.Property.Body, Column.Type.GetNonNullableType()),
Expression.Constant(FilterValue))),
Column.Property.Parameters);

case NumberCondition.IsGreaterThanOrEqualTo:
return Expression.Lambda<Func<TableItem, bool>>(
Expression.AndAlso(
Expression.NotEqual(Column.Property.Body, Expression.Constant(null)),
Expression.GreaterThanOrEqual(
Expression.Convert(Column.Property.Body, Column.Type.GetNonNullableType()),
Expression.Constant(FilterValue))),
Column.Property.Parameters);

case NumberCondition.IsGreaterThan:
return Expression.Lambda<Func<TableItem, bool>>(
Expression.AndAlso(
Expression.NotEqual(Column.Property.Body, Expression.Constant(null)),
Expression.GreaterThan(
Expression.Convert(Column.Property.Body, Column.Type.GetNonNullableType()),
Expression.Constant(FilterValue))),
Column.Property.Parameters);

case NumberCondition.IsLessThanOrEqualTo:
return Expression.Lambda<Func<TableItem, bool>>(
Expression.AndAlso(
Expression.NotEqual(Column.Property.Body, Expression.Constant(null)),
Expression.LessThanOrEqual(
Expression.Convert(Column.Property.Body, Column.Type.GetNonNullableType()),
Expression.Constant(FilterValue))),
Column.Property.Parameters);

case NumberCondition.IsLessThan:
return Expression.Lambda<Func<TableItem, bool>>(
Expression.AndAlso(
Expression.NotEqual(Column.Property.Body, Expression.Constant(null)),
Expression.LessThan(
Expression.Convert(Column.Property.Body, Column.Type.GetNonNullableType()),
Expression.Constant(FilterValue))),
Column.Property.Parameters);

case NumberCondition.IsNull:
return Expression.Lambda<Func<TableItem, bool>>(
Expression.Equal(Column.Property.Body, Expression.Constant(null)),
Column.Property.Parameters);

case NumberCondition.IsNotNull:
return Expression.Lambda<Func<TableItem, bool>>(
Expression.NotEqual(Column.Property.Body, Expression.Constant(null)),
Column.Property.Parameters);

default:
throw new ArgumentException(Condition + " is not defined!");
}
}
}
}
7 changes: 5 additions & 2 deletions src/BlazorTable/Filters/NumberFilter.razor
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
@namespace BlazorTable
@typeparam TableItem

@if (FilterManager.Column.Type.IsNumeric())
@if (Column.Type.IsNumeric())
{
<select class="form-control form-control-sm" value="@Condition" @onchange="@((x) => Condition = (NumberCondition)Enum.Parse(typeof(NumberCondition), x.Value.ToString()))">
@foreach (var option in (NumberCondition[])Enum.GetValues(typeof(NumberCondition)))
{
<option value="@option">@option.GetDescription()</option>
}
</select>
<input type="number" class="form-control form-control-sm" @bind-value="FilterValue" />
@if (Condition != NumberCondition.IsNull && Condition != NumberCondition.IsNotNull)
{
<input type="number" class="form-control form-control-sm" @bind-value="FilterValue" />
}
}
Loading