How to filter Enum type column in Kendo Grid with multi select

1 Answer 33 Views
Grid
Pol
Top achievements
Rank 1
Iron
Pol asked on 06 Mar 2024, 10:12 PM
0

I have an Enum Model called TargetType and ReportViewModel. All the records from the Report table is listing in a kendo grid using the viewmodel ReportViewModel. But the problem is If I try to filter the enum type column 'EmpTypes' it will break the application and cannot filter it. How can I make it filter the column EmpType in Kendo?

 
public enum EmpTypes
{
[Display(Name="Service")
Service = 0,
[Display(Name="Sales")
Sales = 1
[Display(Name="Purchase")
Purchase = 2,
[Display(Name="Office")
Office = 3
}
public class ReportViewModel
{
public string Name {get;set;}
public EmpTypes EmpTypes {get;set;}
}

The Report Table consists the following record

{Name = "AbC",  EmpTypes = 1},
{Name = "xyz",  EmpTypes = 2},

In ReportController GetEmpTypes() , I am fetching the data into Kendo

IQueryable<Report> reportquery = dbContext.Reports;
var kendoList = reportquery.Select(r=>new ReportViewModel()
{
Name = r.Name,
EmpTypes = r.EmpTypes
});
return kendolist.ToDataSourceResultAsync(request);

In Kendo Grid

Columns(columns =>
{
    columns.Bound(c => c.Name).Title("Report Name");
    columns.Bound(c => c.EmpTypes).Title("Type")
        .Filterable(filterable => filterable
            .Multi(true))
            .DataSource(dataSource => dataSource
                .Read(read => read.Action("GetEmpTypes", "Report"))
            )
        );
})

1 Answer, 1 is accepted

Sort by
0
Anton Mironov
Telerik team
answered on 11 Mar 2024, 10:38 AM

Hi Pol,

Thank you for the code snippets and details provided.

In order to achieve the desired behavior, I would recommend implementing a custom filter for the enum field.

Here is a dojo example:

I hope this achieves the desired result.

Kind Regards,
Anton Mironov
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages. If you're new to the Telerik family, be sure to check out our getting started resources, as well as the only REPL playground for creating, saving, running, and sharing server-side code.

Pol
Top achievements
Rank 1
Iron
commented on 11 Mar 2024, 05:46 PM | edited

Hi Anton Mirnov
How can I call Action method from the controller for the column 'EmpTypes'.  from your example it is not clear how can  pull the Enum values from an Enum model class  from the controller Action method
 @(Html.Kendo().Grid<webkendo.Models.EmployeeDetails>()
            .Name("grid")
            .Columns(columns =>
            {
                columns.Bound(c => c.FirstName);

                columns.Bound(c => c.SecondName);
                columns.Bound(c => c.Email);
                columns.Bound(c => c.EmpTypes).Width(150); // Enum types
            })            
            .Scrollable()            
            .Sortable()

            .DataSource(dataSource => dataSource
                .Ajax()
                .Read(read => read.Action("getEmployees", "Employee"))
                .PageSize(20)
            )
Anton Mironov
Telerik team
commented on 14 Mar 2024, 07:19 AM

Hi Pol,

Here is a project implemented to use an Enum named "Category":

    public enum Category
    {
        Beverages,
        Foods
    }

The enum above is used in a Telerik UI MVC Grid.

Here is the example:

Best Regards,
Anton Mironov

 

 

Tags
Grid
Asked by
Pol
Top achievements
Rank 1
Iron
Answers by
Anton Mironov
Telerik team
Share this question
or