Skip to content

Commit

Permalink
Add DateTimeOffset filter support (gustavnavar/Grid.Blazor#249)
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavnavar committed Jan 22, 2021
1 parent 45790b7 commit dbfc043
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
67 changes: 67 additions & 0 deletions GridShared/Filtering/Types/DateTimeOffsetFilterType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Globalization;
using System.Linq.Expressions;

namespace GridShared.Filtering.Types
{
/// <summary>
/// Object contains some logic for filtering DateTimeOffset columns
/// </summary>
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);
}

/// <summary>
/// There are filter types that allowed for DateTime column
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
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;
}
}
}
1 change: 1 addition & 0 deletions GridShared/Filtering/Types/FilterTypeResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit dbfc043

Please sign in to comment.