Skip to content

Commit

Permalink
RadzenDataFilterProperty FilterProperty property added similar to Dat…
Browse files Browse the repository at this point in the history
…aGrid columns to be able to filter collection sub property

Fix #1792
  • Loading branch information
enchev committed Nov 18, 2024
1 parent 24f30bf commit a56981b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
39 changes: 35 additions & 4 deletions Radzen.Blazor/QueryableExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1133,8 +1133,16 @@ private static void AddWhereExpression<T>(Func<CompositeFilterDescriptor, bool>

if (IsEnumerable(column.FilterPropertyType) && column.FilterPropertyType != typeof(string) && comparison == "Contains")
{
filterExpressions.Add($@"(@{index}).Contains({property})");
filterValues.Add(new object[] { filter.FilterValue });
if (column.Property != column.FilterProperty && !string.IsNullOrEmpty(column.FilterProperty))
{
filterExpressions.Add($@"{column.Property}.Any(i => i.{column.FilterProperty}{filterCaseSensitivityOperator}.Contains(@{index}{filterCaseSensitivityOperator}))");
filterValues.Add(new object[] { filter.FilterValue });
}
else
{
filterExpressions.Add($@"(@{index}).Contains({property})");
filterValues.Add(new object[] { filter.FilterValue });
}
}
else
{
Expand All @@ -1153,8 +1161,16 @@ private static void AddWhereExpression<T>(Func<CompositeFilterDescriptor, bool>

if (IsEnumerable(column.FilterPropertyType) && column.FilterPropertyType != typeof(string) && comparison == "DoesNotContain")
{
filterExpressions.Add($@"!(@{index}).Contains({property})");
filterValues.Add(new object[] { filter.FilterValue });
if (column.Property != column.FilterProperty && !string.IsNullOrEmpty(column.FilterProperty))
{
filterExpressions.Add($@"!{column.Property}.Any(i => i.{column.FilterProperty}{filterCaseSensitivityOperator}.Contains(@{index}{filterCaseSensitivityOperator}))");
filterValues.Add(new object[] { filter.FilterValue });
}
else
{
filterExpressions.Add($@"!(@{index}).Contains({property})");
filterValues.Add(new object[] { filter.FilterValue });
}
}
else
{
Expand All @@ -1173,6 +1189,21 @@ private static void AddWhereExpression<T>(Func<CompositeFilterDescriptor, bool>

index++;
}
else if (IsEnumerable(column.FilterPropertyType) && column.FilterPropertyType != typeof(string) &&
column.Property != column.FilterProperty && !string.IsNullOrEmpty(column.FilterProperty))
{
filterExpressions.Add($@"{(comparison == "NotIn" ? "!" : "")}{column.Property}.Any(i => i.{column.FilterProperty} in @{index})");
filterValues.Add(new object[] { filter.FilterValue });

index++;
}
}
else if (IsEnumerable(column.FilterPropertyType) && column.FilterPropertyType != typeof(string) && column.Property != column.FilterProperty && !string.IsNullOrEmpty(column.FilterProperty))
{
filterExpressions.Add($@"{column.Property}.Any(i => i.{column.FilterProperty}{filterCaseSensitivityOperator} {comparison} @{index}{filterCaseSensitivityOperator})");
filterValues.Add(new object[] { filter.FilterValue });

index++;
}
else if (!(IsEnumerable(column.FilterPropertyType) && column.FilterPropertyType != typeof(string)))
{
Expand Down
7 changes: 7 additions & 0 deletions Radzen.Blazor/RadzenDataFilterProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ internal void SetVisible(bool? value)
[Parameter]
public string Property { get; set; }

/// <summary>
/// Gets or sets the filter property name.
/// </summary>
/// <value>The filter property name.</value>
[Parameter]
public string FilterProperty { get; set; }

/// <summary>
/// Gets or sets a value indicating whether this property is selected in the filter.
/// </summary>
Expand Down
9 changes: 8 additions & 1 deletion RadzenBlazorDemos/Pages/DataFilterIQueryable.razor
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<RadzenButton Text="Apply Filter" Click="@ApplyFilter" Disabled="@auto" Size="ButtonSize.Small" />
</RadzenStack>

<RadzenDataFilter @ref="dataFilter" Auto=auto Data="@orders" TItem="Order" ViewChanged=@ViewChanged>
<RadzenDataFilter @ref="dataFilter" Auto=auto Data="@orders" TItem="Order" ViewChanged=@ViewChanged FilterCaseSensitivity="FilterCaseSensitivity.CaseInsensitive">
<Properties>
<RadzenDataFilterProperty Property="@nameof(Order.OrderID)" Title="Order ID" FilterValue="@finalSelectedIds"
Type="typeof(IEnumerable<int>)" FilterOperator="FilterOperator.Contains">
Expand All @@ -27,6 +27,8 @@
Change=@OnSelectedProductIdsChange Data="@(productIds)" AllowClear="true" Multiple="true" />
</FilterTemplate>
</RadzenDataFilterProperty>
<RadzenDataFilterProperty Property="OrderDetails" FilterProperty="Product.ProductName" Title="Products"
Type="typeof(IEnumerable<OrderDetail>)" FilterOperator="FilterOperator.Contains"/>
<RadzenDataFilterProperty Property="Employee.LastName" Title="Employee Last Name" />
<MyCustomDataFilterProperty Property="@nameof(Order.OrderDate)" Title="Order Date">
<FilterTemplate>
Expand All @@ -46,6 +48,11 @@
Displayed orders: <b>@ordersGrid.View.Count()</b> of <b>@orders.Count()</b>
</FooterTemplate>
</RadzenDataGridColumn>
<RadzenDataGridColumn Property="OrderDetails" FilterProperty="Product.ProductName" Title="Products" Type="typeof(IEnumerable<OrderDetail>)" Sortable="false">
<Template>
@(string.Join(',', context.OrderDetails.Select(od => od.Product.ProductName)))
</Template>
</RadzenDataGridColumn>
<RadzenDataGridColumn Width="200px" Property="Customer.CompanyName" Title="Customer" />
<RadzenDataGridColumn Property="Employee.LastName" Title="Employee">
<Template Context="order">
Expand Down

0 comments on commit a56981b

Please sign in to comment.