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: How filter nested object? #542

Open
ariadna-aldeguer opened this issue Feb 2, 2024 · 2 comments
Open

Grid Filter: How filter nested object? #542

ariadna-aldeguer opened this issue Feb 2, 2024 · 2 comments

Comments

@ariadna-aldeguer
Copy link

Describe the bug
Example, I have an employee who has a relationship with the jobs table.
I want to filter by job name, how to do it?

I tried:

  • PropertyName="Job.Name"
  • PropertyName="Employee.Job.Name"
    SortKeySelector="item => item.Job.Name"
public class Job
 {
     public int Id { get; set; }
     public string Name { get; set; }
 }
 public class Employee
 {
     public int Id { get; set; }
     public int JobId { get; set; }
     public virtual Job Job { get; set; }
 }

Versions (please complete the following information):

  • BlazorBootstrap: [e.g. 1.9.0, 19.2, 19.3]
  • Blazor WebAssembly / Server: Server
  • .NET Version: NET8

Sample code

@rendermode InteractiveServer
@page "/sample"
@attribute [StreamRendering]

<Grid @ref="grid"
      TItem="Employee"
      Class="table table-hover table-bordered table-striped"
      DataProvider="EmployeesDataProvider"
      AllowFiltering="true"
      Responsive="true"
      AllowPaging="true" PageSize="20"
      AllowSorting="true"
      EmptyText="No hay registros para mostrar"
      ItemsPerPageText="Items por página"
      Height="500"
      AutoHidePaging="true"
      FixedHeader="true"
      PageSizeSelectorItems="@(new int[] { 5,10,20 })"
      PaginationItemsTextFormat="{0} - {1} de {2} items">

    <GridColumn TItem="Employee" HeaderText="Id" PropertyName="Id">
        @context.Id
    </GridColumn>
    <GridColumn TItem="Employee" HeaderText="Job" PropertyName="Job.Name">
        @context.Job.Name
    </GridColumn>
   

</Grid>

@code {
    BlazorBootstrap.Grid<Employee> grid = default!;
    private IEnumerable<Employee> employees = default!;

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        await base.OnAfterRenderAsync(firstRender);
    }

    private async Task<GridDataProviderResult<Employee>> EmployeesDataProvider(GridDataProviderRequest<Employee> request)
    {
        if (employees is null) // pull employees only one time for client-side filtering, sorting, and paging
            employees = GetEmployees(); // call a service or an API to pull the employees

        return await Task.FromResult(request.ApplyTo(employees));
    }

    private IEnumerable<Employee> GetEmployees()
    {
        List<Job> jobs = new List<Job>
        {
            new Job { Id = 1, Name = "Software Developer" },
            new Job { Id = 2, Name = "Database Administrator" },
        };

        return new List<Employee>
        {
            new Employee { Id = 107, JobId = 1, Job = jobs.Find(job => job.Id == 1) },
            new Employee { Id = 108, JobId = 2, Job = jobs.Find(job => job.Id == 2) },
        };
    }
    public class Job
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    public class Employee
    {
        public int Id { get; set; }
        public int JobId { get; set; }
        public virtual Job Job { get; set; }
    }
}
@OllieFox131
Copy link

Are there any plans to support this @gvreddy04? Would be a good feature to see!

@gvreddy04
Copy link
Contributor

@OllieFox131 Yes, we will include it in the April/May release. We are planning more grid enhancements during the April/May timeframe.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants