From dbfc043c0e271f5a1c8edbe5a597ec55a4a13469 Mon Sep 17 00:00:00 2001 From: Gustau Navarro Date: Fri, 22 Jan 2021 11:24:46 +0100 Subject: [PATCH] Add DateTimeOffset filter support (https://github.com/gustavnavar/Grid.Blazor/issues/249) --- .../Types/DateTimeOffsetFilterType.cs | 67 +++++++++++++++++++ .../Filtering/Types/FilterTypeResolver.cs | 1 + 2 files changed, 68 insertions(+) create mode 100644 GridShared/Filtering/Types/DateTimeOffsetFilterType.cs diff --git a/GridShared/Filtering/Types/DateTimeOffsetFilterType.cs b/GridShared/Filtering/Types/DateTimeOffsetFilterType.cs new file mode 100644 index 00000000..907c537b --- /dev/null +++ b/GridShared/Filtering/Types/DateTimeOffsetFilterType.cs @@ -0,0 +1,67 @@ +using System; +using System.Globalization; +using System.Linq.Expressions; + +namespace GridShared.Filtering.Types +{ + /// + /// Object contains some logic for filtering DateTimeOffset columns + /// + public sealed class DateTimeOffsetFilterType : FilterTypeBase + { + public override Type TargetType + { + get { return typeof(DateTimeOffset); } + } + + public override Expression GetFilterExpression(Expression leftExpr, string value, GridFilterType filterType) + { + //var dateExpr = Expression.Property(leftExpr, leftExpr.Type, "Date"); + + //if (filterType == GridFilterType.Equals) + //{ + // var dateObj = GetTypedValue(value); + // if (dateObj == null) return null;//not valid + + // var startDate = Expression.Constant(dateObj); + // var endDate = Expression.Constant(((DateTime)dateObj).AddDays(1)); + + // var left = Expression.GreaterThanOrEqual(leftExpr, startDate); + // var right = Expression.LessThan(leftExpr, endDate); + + // return Expression.And(left, right); + //} + + return base.GetFilterExpression(leftExpr, value, filterType); + } + + /// + /// There are filter types that allowed for DateTime column + /// + /// + /// + public override GridFilterType GetValidType(GridFilterType type) + { + switch (type) + { + case GridFilterType.Equals: + case GridFilterType.NotEquals: + case GridFilterType.GreaterThan: + case GridFilterType.GreaterThanOrEquals: + case GridFilterType.LessThan: + case GridFilterType.LessThanOrEquals: + return type; + default: + return GridFilterType.Equals; + } + } + + public override object GetTypedValue(string value) + { + DateTimeOffset date; + if (!DateTimeOffset.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.None, out date)) + return null; + return date; + } + } +} \ No newline at end of file diff --git a/GridShared/Filtering/Types/FilterTypeResolver.cs b/GridShared/Filtering/Types/FilterTypeResolver.cs index 0818f7a2..05834e0c 100644 --- a/GridShared/Filtering/Types/FilterTypeResolver.cs +++ b/GridShared/Filtering/Types/FilterTypeResolver.cs @@ -15,6 +15,7 @@ public FilterTypeResolver() _filterCollection.Add(new Int16FilterType()); _filterCollection.Add(new BooleanFilterType()); _filterCollection.Add(new DateTimeFilterType()); + _filterCollection.Add(new DateTimeOffsetFilterType()); _filterCollection.Add(new DecimalFilterType()); _filterCollection.Add(new ByteFilterType()); _filterCollection.Add(new SingleFilterType());