Skip to content

Commit

Permalink
#503 - Grid filters - DoesNotContain support added. (#709)
Browse files Browse the repository at this point in the history
  • Loading branch information
gvreddy04 authored May 4, 2024
1 parent f1c3f22 commit 15541be
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ private FilterOperator GetFilterOperator() =>
{
StringFilterOperator.Equals => FilterOperator.Equals,
StringFilterOperator.Contains => FilterOperator.Contains,
StringFilterOperator.DoesNotContain => FilterOperator.DoesNotContain,
StringFilterOperator.StartsWith => FilterOperator.StartsWith,
StringFilterOperator.EndsWith => FilterOperator.EndsWith,
_ => FilterOperator.Contains
Expand Down
5 changes: 5 additions & 0 deletions blazorbootstrap/Enums/StringFilterOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ public enum StringFilterOperator
/// </summary>
Contains,

/// <summary>
/// Satisfied if the current value does not contain the specified value.
/// </summary>
DoesNotContain,

/// <summary>
/// Satisfied if the current value starts with the specified value.
/// </summary>
Expand Down
23 changes: 23 additions & 0 deletions blazorbootstrap/Extensions/ExpressionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ or StringConstants.PropertyTypeNameDecimal
return filterItem.Operator switch
{
FilterOperator.Contains => GetStringContainsExpressionDelegate<TItem>(parameterExpression, filterItem),
FilterOperator.DoesNotContain => GetStringDoesNotContainExpressionDelegate<TItem>(parameterExpression, filterItem),
FilterOperator.StartsWith => GetStringStartsWithExpressionDelegate<TItem>(parameterExpression, filterItem),
FilterOperator.EndsWith => GetStringEndsWithExpressionDelegate<TItem>(parameterExpression, filterItem),
FilterOperator.Equals => GetStringEqualsExpressionDelegate<TItem>(parameterExpression, filterItem),
Expand Down Expand Up @@ -675,6 +676,28 @@ public static Expression<Func<TItem, bool>> GetStringContainsExpressionDelegate<
return Expression.Lambda<Func<TItem, bool>>(finalExpression, parameterExpression);
}

public static Expression<Func<TItem, bool>> GetStringDoesNotContainExpressionDelegate<TItem>(ParameterExpression parameterExpression, FilterItem filterItem)
{
var propertyExp = Expression.Property(parameterExpression, filterItem.PropertyName);
var someValue = Expression.Constant(filterItem.Value, typeof(string));
var comparisonExpression = Expression.Constant(filterItem.StringComparison);

// Handle null check
var nullCheckExpression = Expression.NotEqual(propertyExp, Expression.Constant(null, typeof(string)));

// Create method call expression for Contains method
var methodInfo = typeof(string).GetMethod(nameof(string.Contains), new[] { typeof(string), typeof(StringComparison) });
var containsExpression = Expression.Call(propertyExp, methodInfo!, someValue, comparisonExpression);

// "not contains" expression
var notContainsExpression = Expression.Not(containsExpression);

// Combine null check and contains expression using AndAlso
var finalExpression = Expression.AndAlso(nullCheckExpression, notContainsExpression);

return Expression.Lambda<Func<TItem, bool>>(finalExpression, parameterExpression);
}

public static Expression<Func<TItem, bool>> GetStringEndsWithExpressionDelegate<TItem>(ParameterExpression parameterExpression, FilterItem filterItem)
{
var propertyExp = Expression.Property(parameterExpression, filterItem.PropertyName);
Expand Down
14 changes: 0 additions & 14 deletions blazorbootstrap/Extensions/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,6 @@ public static string GetPropertyTypeName(this Type type, string propertyName)
return string.Empty;

var propertyTypeName = propertyType?.ToString();

//if(propertyTypeName is null) // Nullable type scenario
//{
// var props = type.GetProperties();
// if(props.Length > 0)
// {
// var propertyInfo = props?.FirstOrDefault(x=>x.Name == propertyName);
// if (propertyInfo is null)
// throw new InvalidDataException($"PropertyName `{propertyName}` is invalid.");

// propertyTypeName = Nullable.GetUnderlyingType(propertyInfo.PropertyType)?.FullName;
// }
//}

if (string.IsNullOrWhiteSpace(propertyTypeName))
return string.Empty;

Expand Down
2 changes: 1 addition & 1 deletion blazorbootstrap/Utilities/FilterOperatorHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public static IEnumerable<FilterOperatorInfo> GetStringFilterOperators()
List<FilterOperatorInfo> result = new()
{
new FilterOperatorInfo("*a*", "Contains", FilterOperator.Contains),
//result.Add(new("!*a*", "Does not contain", FilterOperator.DoesNotContain));
new FilterOperatorInfo("!*a*", "Does not contain", FilterOperator.DoesNotContain),
new FilterOperatorInfo("a**", "Starts with", FilterOperator.StartsWith),
new FilterOperatorInfo("**a", "Ends with", FilterOperator.EndsWith),
//result.Add(new("=''", "Is empty", FilterOperator.IsEmpty));
Expand Down

0 comments on commit 15541be

Please sign in to comment.