diff --git a/README.md b/README.md index 347f1210..ae86d326 100644 --- a/README.md +++ b/README.md @@ -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!** diff --git a/src/BlazorTable.Sample/Pages/EditMode.razor b/src/BlazorTable.Sample/Pages/EditMode.razor index 3c4d3c53..8e9f7f45 100644 --- a/src/BlazorTable.Sample/Pages/EditMode.razor +++ b/src/BlazorTable.Sample/Pages/EditMode.razor @@ -56,7 +56,7 @@ - + diff --git a/src/BlazorTable.Sample/Pages/Index.razor b/src/BlazorTable.Sample/Pages/Index.razor index d558a477..6e767def 100644 --- a/src/BlazorTable.Sample/Pages/Index.razor +++ b/src/BlazorTable.Sample/Pages/Index.razor @@ -41,7 +41,7 @@ @(context.fund.HasValue ? context.fund.Value.ToString("C") : string.Empty) - + diff --git a/src/BlazorTable/BlazorTable.csproj b/src/BlazorTable/BlazorTable.csproj index b08ca3fc..c5a811a4 100644 --- a/src/BlazorTable/BlazorTable.csproj +++ b/src/BlazorTable/BlazorTable.csproj @@ -9,7 +9,7 @@ Ivan Josipovic https://github.com/IvanJosipovic/BlazorTable Blazor;Table;Grid;DataTable;Data;Sort;Filter;AspNetCore;AspNet - 0.0.5-alpha + 0.0.6-alpha Experimental icon.png diff --git a/src/BlazorTable/Components/Column.razor b/src/BlazorTable/Components/Column.razor index 64d40abc..ee1fafc3 100644 --- a/src/BlazorTable/Components/Column.razor +++ b/src/BlazorTable/Components/Column.razor @@ -10,7 +10,11 @@ - @*@this.CustomIFilters(this)*@ + + @*@if (this.CustomIFilters != null) + { + this.CustomIFilters(this); + }*@ diff --git a/src/BlazorTable/Components/Column.razor.cs b/src/BlazorTable/Components/Column.razor.cs index a9610122..cc175207 100644 --- a/src/BlazorTable/Components/Column.razor.cs +++ b/src/BlazorTable/Components/Column.razor.cs @@ -1,6 +1,5 @@ using Microsoft.AspNetCore.Components; using System; -using System.Linq; using System.Linq.Expressions; namespace BlazorTable @@ -50,7 +49,7 @@ public string Title public Type Type { get; private set; } - public IFilterManager FilterManager { get; set; } + public IFilter FilterControl { get; set; } public void Dispose() { diff --git a/src/BlazorTable/Components/FilterManager.razor b/src/BlazorTable/Components/FilterManager.razor index ffa83bff..5e161d5b 100644 --- a/src/BlazorTable/Components/FilterManager.razor +++ b/src/BlazorTable/Components/FilterManager.razor @@ -1,7 +1,7 @@ @namespace BlazorTable @typeparam TableItem - + @ChildContent
diff --git a/src/BlazorTable/Components/FilterManager.razor.cs b/src/BlazorTable/Components/FilterManager.razor.cs index 0f47abec..012b3037 100644 --- a/src/BlazorTable/Components/FilterManager.razor.cs +++ b/src/BlazorTable/Components/FilterManager.razor.cs @@ -3,7 +3,7 @@ namespace BlazorTable { - public partial class FilterManager : IFilterManager + public partial class FilterManager { [CascadingParameter(Name = "Column")] public IColumn Column { get; set; } @@ -11,21 +11,21 @@ public partial class FilterManager : IFilterManager [Inject] public ILogger> Logger { get; set; } - public IFilter 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"); } } diff --git a/src/BlazorTable/Filters/BooleanFilter.razor b/src/BlazorTable/Filters/BooleanFilter.razor index 96bd214d..d9ef07ae 100644 --- a/src/BlazorTable/Filters/BooleanFilter.razor +++ b/src/BlazorTable/Filters/BooleanFilter.razor @@ -1,7 +1,7 @@ @namespace BlazorTable @typeparam TableItem -@if (FilterTypes.Contains(FilterManager.Column.Type.GetNonNullableType())) +@if (FilterTypes.Contains(Column.Type.GetNonNullableType())) { + @foreach (var option in (NumberCondition[])Enum.GetValues(typeof(NumberCondition))) + { + + } + + @if (Condition != NumberCondition.IsNull && Condition != NumberCondition.IsNotNull) + { + + } +} \ No newline at end of file diff --git a/src/BlazorTable/Filters/DateFilter.razor.cs b/src/BlazorTable/Filters/DateFilter.razor.cs new file mode 100644 index 00000000..0f202ba0 --- /dev/null +++ b/src/BlazorTable/Filters/DateFilter.razor.cs @@ -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 : IFilter + { + [CascadingParameter(Name = "Column")] public IColumn Column { get; set; } + + [Inject] public ILogger> 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> GetFilter() + { + switch (Condition) + { + case NumberCondition.IsEqualTo: + return Expression.Lambda>( + 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>( + 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>( + 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>( + 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>( + 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>( + 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>( + Expression.Equal(Column.Property.Body, Expression.Constant(null)), + Column.Property.Parameters); + + case NumberCondition.IsNotNull: + return Expression.Lambda>( + Expression.NotEqual(Column.Property.Body, Expression.Constant(null)), + Column.Property.Parameters); + + default: + throw new ArgumentException(Condition + " is not defined!"); + } + } + } +} \ No newline at end of file diff --git a/src/BlazorTable/Filters/NumberFilter.razor b/src/BlazorTable/Filters/NumberFilter.razor index 88a2bb2b..96ec84f8 100644 --- a/src/BlazorTable/Filters/NumberFilter.razor +++ b/src/BlazorTable/Filters/NumberFilter.razor @@ -1,7 +1,7 @@ @namespace BlazorTable @typeparam TableItem -@if (FilterManager.Column.Type.IsNumeric()) +@if (Column.Type.IsNumeric()) { - + @if (Condition != NumberCondition.IsNull && Condition != NumberCondition.IsNotNull) + { + + } } \ No newline at end of file diff --git a/src/BlazorTable/Filters/NumberFilter.razor.cs b/src/BlazorTable/Filters/NumberFilter.razor.cs index 0afdfa44..820e7908 100644 --- a/src/BlazorTable/Filters/NumberFilter.razor.cs +++ b/src/BlazorTable/Filters/NumberFilter.razor.cs @@ -2,13 +2,14 @@ using Microsoft.Extensions.Logging; using System; using System.ComponentModel; +using System.Globalization; using System.Linq.Expressions; namespace BlazorTable { public partial class NumberFilter : IFilter { - [CascadingParameter(Name = "FilterManager")] public IFilterManager FilterManager { get; set; } + [CascadingParameter(Name = "Column")] public IColumn Column { get; set; } [Inject] public ILogger> Logger { get; set; } @@ -18,130 +19,124 @@ public partial class NumberFilter : IFilter protected override void OnInitialized() { - if (FilterManager.Column.Type.IsNumeric()) + if (Column.Type.IsNumeric()) { - FilterManager.Filter = this; + Column.FilterControl = this; - if (FilterManager.Column.Filter != null) + if (Column.Filter?.Body is BinaryExpression binaryExpression) { - if (FilterManager.Column.Filter.Body is BinaryExpression binaryExpression) + if (binaryExpression.NodeType == ExpressionType.AndAlso) { - if (binaryExpression.NodeType == ExpressionType.AndAlso) + switch (binaryExpression.Right.NodeType) { - 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; - } + 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 + } + else + { + if (binaryExpression.NodeType == ExpressionType.Equal) { - if (binaryExpression.NodeType == ExpressionType.Equal) - { - Condition = NumberCondition.IsNull; - } - else if (binaryExpression.NodeType == ExpressionType.NotEqual) - { - Condition = NumberCondition.IsNotNull; - } + Condition = NumberCondition.IsNull; } - - if (binaryExpression.Right is BinaryExpression binaryExpression2) + else if (binaryExpression.NodeType == ExpressionType.NotEqual) { - if (binaryExpression2.Right is ConstantExpression constantExpression) - { - FilterValue = constantExpression.Value.ToString(); - } + Condition = NumberCondition.IsNotNull; } } + + if (binaryExpression.Right is BinaryExpression binaryExpression2 + && binaryExpression2.Right is ConstantExpression constantExpression) + { + FilterValue = constantExpression.Value.ToString(); + } } } } - public void ApplyFilter() + public Expression> GetFilter() { switch (Condition) { case NumberCondition.IsEqualTo: - FilterManager.Column.Filter = Expression.Lambda>( + return Expression.Lambda>( Expression.AndAlso( - Expression.NotEqual(FilterManager.Column.Property.Body, Expression.Constant(null)), + Expression.NotEqual(Column.Property.Body, Expression.Constant(null)), Expression.Equal( - Expression.Convert(FilterManager.Column.Property.Body, FilterManager.Column.Type.GetNonNullableType()), - Expression.Constant(Convert.ChangeType(FilterValue, FilterManager.Column.Type.GetNonNullableType())))), - FilterManager.Column.Property.Parameters); - break; + Expression.Convert(Column.Property.Body, Column.Type.GetNonNullableType()), + Expression.Constant(Convert.ChangeType(FilterValue, Column.Type.GetNonNullableType(), CultureInfo.InvariantCulture)))), + Column.Property.Parameters); + case NumberCondition.IsNotEqualTo: - FilterManager.Column.Filter = Expression.Lambda>( + return Expression.Lambda>( Expression.AndAlso( - Expression.NotEqual(FilterManager.Column.Property.Body, Expression.Constant(null)), + Expression.NotEqual(Column.Property.Body, Expression.Constant(null)), Expression.NotEqual( - Expression.Convert(FilterManager.Column.Property.Body, FilterManager.Column.Type.GetNonNullableType()), - Expression.Constant(Convert.ChangeType(FilterValue, FilterManager.Column.Type.GetNonNullableType())))), - FilterManager.Column.Property.Parameters); - break; + Expression.Convert(Column.Property.Body, Column.Type.GetNonNullableType()), + Expression.Constant(Convert.ChangeType(FilterValue, Column.Type.GetNonNullableType(), CultureInfo.InvariantCulture)))), + Column.Property.Parameters); + case NumberCondition.IsGreaterThanOrEqualTo: - FilterManager.Column.Filter = Expression.Lambda>( + return Expression.Lambda>( Expression.AndAlso( - Expression.NotEqual(FilterManager.Column.Property.Body, Expression.Constant(null)), + Expression.NotEqual(Column.Property.Body, Expression.Constant(null)), Expression.GreaterThanOrEqual( - Expression.Convert(FilterManager.Column.Property.Body, FilterManager.Column.Type.GetNonNullableType()), - Expression.Constant(Convert.ChangeType(FilterValue, FilterManager.Column.Type.GetNonNullableType())))), - FilterManager.Column.Property.Parameters); - break; + Expression.Convert(Column.Property.Body, Column.Type.GetNonNullableType()), + Expression.Constant(Convert.ChangeType(FilterValue, Column.Type.GetNonNullableType(), CultureInfo.InvariantCulture)))), + Column.Property.Parameters); + case NumberCondition.IsGreaterThan: - FilterManager.Column.Filter = Expression.Lambda>( + return Expression.Lambda>( Expression.AndAlso( - Expression.NotEqual(FilterManager.Column.Property.Body, Expression.Constant(null)), + Expression.NotEqual(Column.Property.Body, Expression.Constant(null)), Expression.GreaterThan( - Expression.Convert(FilterManager.Column.Property.Body, FilterManager.Column.Type.GetNonNullableType()), - Expression.Constant(Convert.ChangeType(FilterValue, FilterManager.Column.Type.GetNonNullableType())))), - FilterManager.Column.Property.Parameters); - break; + Expression.Convert(Column.Property.Body, Column.Type.GetNonNullableType()), + Expression.Constant(Convert.ChangeType(FilterValue, Column.Type.GetNonNullableType(), CultureInfo.InvariantCulture)))), + Column.Property.Parameters); + case NumberCondition.IsLessThanOrEqualTo: - FilterManager.Column.Filter = Expression.Lambda>( + return Expression.Lambda>( Expression.AndAlso( - Expression.NotEqual(FilterManager.Column.Property.Body, Expression.Constant(null)), + Expression.NotEqual(Column.Property.Body, Expression.Constant(null)), Expression.LessThanOrEqual( - Expression.Convert(FilterManager.Column.Property.Body, FilterManager.Column.Type.GetNonNullableType()), - Expression.Constant(Convert.ChangeType(FilterValue, FilterManager.Column.Type.GetNonNullableType())))), - FilterManager.Column.Property.Parameters); - break; + Expression.Convert(Column.Property.Body, Column.Type.GetNonNullableType()), + Expression.Constant(Convert.ChangeType(FilterValue, Column.Type.GetNonNullableType(), CultureInfo.InvariantCulture)))), + Column.Property.Parameters); + case NumberCondition.IsLessThan: - FilterManager.Column.Filter = Expression.Lambda>( + return Expression.Lambda>( Expression.AndAlso( - Expression.NotEqual(FilterManager.Column.Property.Body, Expression.Constant(null)), + Expression.NotEqual(Column.Property.Body, Expression.Constant(null)), Expression.LessThan( - Expression.Convert(FilterManager.Column.Property.Body, FilterManager.Column.Type.GetNonNullableType()), - Expression.Constant(Convert.ChangeType(FilterValue, FilterManager.Column.Type.GetNonNullableType())))), - FilterManager.Column.Property.Parameters); - break; + Expression.Convert(Column.Property.Body, Column.Type.GetNonNullableType()), + Expression.Constant(Convert.ChangeType(FilterValue, Column.Type.GetNonNullableType(), CultureInfo.InvariantCulture)))), + Column.Property.Parameters); + case NumberCondition.IsNull: - FilterManager.Column.Filter = Expression.Lambda>( - Expression.Equal(FilterManager.Column.Property.Body, Expression.Constant(null)), - FilterManager.Column.Property.Parameters); - break; + return Expression.Lambda>( + Expression.Equal(Column.Property.Body, Expression.Constant(null)), + Column.Property.Parameters); + case NumberCondition.IsNotNull: - FilterManager.Column.Filter = Expression.Lambda>( - Expression.NotEqual(FilterManager.Column.Property.Body, Expression.Constant(null)), - FilterManager.Column.Property.Parameters); - break; + return Expression.Lambda>( + Expression.NotEqual(Column.Property.Body, Expression.Constant(null)), + Column.Property.Parameters); default: throw new ArgumentException(Condition + " is not defined!"); } diff --git a/src/BlazorTable/Filters/StringFilter.razor b/src/BlazorTable/Filters/StringFilter.razor index 1475c337..700f15e2 100644 --- a/src/BlazorTable/Filters/StringFilter.razor +++ b/src/BlazorTable/Filters/StringFilter.razor @@ -1,7 +1,7 @@ @namespace BlazorTable @typeparam TableItem -@if (FilterManager.Column.Type == FilterType) +@if (Column.Type == FilterType) {