Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grid: Filter - Nested filters support #967

Open
keinPeil opened this issue Dec 5, 2024 · 0 comments
Open

Grid: Filter - Nested filters support #967

keinPeil opened this issue Dec 5, 2024 · 0 comments
Labels
area-grid enhancement New feature or request
Milestone

Comments

@keinPeil
Copy link

keinPeil commented Dec 5, 2024

NOTE: We do not look at .zip attachments for issue investigation.

Describe the bug
If i try a to filter a Property of a nested objekt, it dont work!

To Reproduce
Create a nested Item like

public record class EmployeeName
{
  public string? FamilyName { get; set;}
  public string? GivenName { get; set;}
}

public record class Employee5
{
  public int? Id{ get; set;}
  public EmployeeName? Name { get; set;}
  public string? Designation { get; set;}
  public DateOnlyß DOJ { get; set;}
  public bool IsActive { get; set;}
}

Create a grid like this:

<Grid TItem="Employee5"
      Class="table table-hover table-bordered table-striped"
      DataProvider="EmployeesDataProvider"
      AllowFiltering="true"
      Responsive="true">

    <GridColumns>
        <GridColumn TItem="Employee5" HeaderText="Id" PropertyName="Id">
            @context.Id
        </GridColumn>
        <GridColumn TItem="Employee5" HeaderText="Employee Name" PropertyName="Name.GivenName">
            @context.Name.GivenName 
        </GridColumn>
        <GridColumn TItem="Employee5" HeaderText="Designation" PropertyName="Designation">
            @context.Designation
        </GridColumn>
        <GridColumn TItem="Employee5" HeaderText="DOJ" PropertyName="DOJ">
            @context.DOJ
        </GridColumn>
        <GridColumn TItem="Employee5" HeaderText="Active" PropertyName="IsActive">
            @context.IsActive
        </GridColumn>
    </GridColumns>

</Grid>

Expected behavior
It should be possible to work, like on other not nested properties.

Possible Fix
In ExpressionExtensions.cs:

Change all of this line:

var propertyExp = Expression.Property(parameterExpression, filterItem.PropertyName);

=>

Expression propertyExp = parameterExpression;
foreach (var propertyName in filterItem.PropertyName.Split('.'))
{
 propertyExp = Expression.Property(propertyExp, propertyName);
}

In TypeExtensions.cs:

Add this methode:

  public static Type GetNestedPropertyType(Type type, string propertyPath)
  {
    string[] properties = propertyPath.Split('.');
    foreach (var property in properties)
    {
      PropertyInfo propInfo = type.GetProperty(property);
      if (propInfo == null)
      {
        return null;
      }
      type = propInfo.PropertyType;
    }
    return type;
  }

Change this line:

var propertyType = type.GetProperty(propertyName)?.PropertyType;

=>

var propertyType = GetNestedPropertyType(type, propertyName);
@keinPeil keinPeil changed the title GRID No Filter of Nestet Objekt GRID No Filter of Nested Objekt Dec 5, 2024
@gvreddy04 gvreddy04 changed the title GRID No Filter of Nested Objekt Grid: Filter - Nested filters support Dec 5, 2024
@gvreddy04 gvreddy04 added this to the 3.3.0 milestone Dec 5, 2024
@gvreddy04 gvreddy04 added the enhancement New feature or request label Dec 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-grid enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants