[PR #1186] Fix DataGrid ClearFilters with FilterOperator.Custom #2573

Closed
opened 2026-01-29 18:19:27 +00:00 by claunia · 0 comments
Owner

Original Pull Request: https://github.com/radzenhq/radzen-blazor/pull/1186

State: closed
Merged: Yes


Added a check for FilterOperator.Custom in ClearFilters so that the FilterOperator does not change and remains FilterOperator.Custom.

Before:

https://github.com/radzenhq/radzen-blazor/assets/18440948/0791d786-de4c-43cb-af64-cbe648db4d09

After:

https://github.com/radzenhq/radzen-blazor/assets/18440948/b4d44223-e0fd-48bd-9b5f-c7bc873e3cd0

Demo:

@using System.Linq.Dynamic.Core
@using System.ComponentModel.DataAnnotations
    
<RadzenDataGrid LoadData="LoadData" IsLoading=@isLoading Count="count" Data=@employees 
                FilterMode="FilterMode.Simple" AllowFiltering="true" AllowPaging="true" 
                AllowSorting="true" TItem="Employee" ColumnWidth="200px" >
    <Columns>
        <RadzenDataGridColumn TItem="Employee" Property="ID" Title="ID" />
        <RadzenDataGridColumn TItem="Employee" Property="Gender" Title="Gender" />
        <RadzenDataGridColumn TItem="Employee" Property="Colors" Title="Colors" 
                              FilterOperator="FilterOperator.Custom" Type="typeof(string)" >  
	        <Template Context="item">@string.Join(",", item.Colors)</Template>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn TItem="Employee" Property="Status" Title="Nullable Status" />
        <RadzenDataGridColumn TItem="Employee" Property="Color" Title="Favorite Color (Display Attribute in Filter)" />
    </Columns>
</RadzenDataGrid>

@code {
    int count;
    IEnumerable<Employee> initialEmployees;
    IEnumerable<Employee> employees;
    bool isLoading = false;

    public class Employee
    {
        public int ID { get; set; }
        public GenderType Gender { get; set; }
        public StatusType? Status { get; set; }
        public ColorType Color { get; set; }
        public List<string> Colors { get; set; }
    }

    public enum GenderType
    {
        Ms,
        Mr,
        Unknown,
    }

    public enum ColorType
    {
        Red,
        Green,
        Blue,
        [Display(Description = "Almond Green")]
        AlmondGreen,
        [Display(Description = "Amber Gray")]
        AmberGray,
        [Display(Description = "Apple Blue... ")]
        AppleBlueSeaGreen,
        //[Display(Description = "Miss", ResourceType = typeof(ResourceFile)] localization example
        [Display(Description = "Azure")]
        AzureBlue,

    }

    public enum StatusType
    {
        Inactive,
        Active,
    }

    protected override void OnInitialized()
    {
        initialEmployees = Enumerable.Range(0, 10).Select(i =>
            new Employee
            {
                ID = i,
                Gender = i < 3 ? GenderType.Mr : i < 6 ? GenderType.Ms : GenderType.Unknown,
                Status = i < 3 ? StatusType.Active : i < 6 ? StatusType.Inactive : null,
                Color = i < 2 ? ColorType.Red: i < 4 ? ColorType.AlmondGreen : i < 6 ? ColorType.AppleBlueSeaGreen : ColorType.AzureBlue,
                    Colors = i < 2 ? new List<string>() { "Red", "AmberGray", } 
	                    : i < 4 
                                ? new List<string>() { "AlmondGreen", "AmberGray", }
		                    : new List<string>() { "Red", "Green", "Blue", },
                    
            });
    }

    void LoadData(LoadDataArgs args)
    {
        isLoading = true;
        var query = initialEmployees.AsQueryable();

        if (!string.IsNullOrEmpty(args.Filter))
        {
            query = query.Where(args.Filter);
        }
	    var colorFilter = args.Filters.FirstOrDefault(f => f.FilterOperator == FilterOperator.Custom)?.FilterValue?.ToString();
        if (!string.IsNullOrWhiteSpace(colorFilter))
	    {
            query = query.Where(e => e.Colors.Contains(colorFilter));
	    }

        if (!string.IsNullOrEmpty(args.OrderBy))
        {
            query = query.OrderBy(args.OrderBy);
        }

        count = query.Count();

        employees = query.Skip(args.Skip.Value).Take(args.Top.Value).ToList();
        isLoading = false;

    }
}

**Original Pull Request:** https://github.com/radzenhq/radzen-blazor/pull/1186 **State:** closed **Merged:** Yes --- Added a check for FilterOperator.Custom in ClearFilters so that the FilterOperator does not change and remains FilterOperator.Custom. Before: https://github.com/radzenhq/radzen-blazor/assets/18440948/0791d786-de4c-43cb-af64-cbe648db4d09 After: https://github.com/radzenhq/radzen-blazor/assets/18440948/b4d44223-e0fd-48bd-9b5f-c7bc873e3cd0 Demo: ``` @using System.Linq.Dynamic.Core @using System.ComponentModel.DataAnnotations <RadzenDataGrid LoadData="LoadData" IsLoading=@isLoading Count="count" Data=@employees FilterMode="FilterMode.Simple" AllowFiltering="true" AllowPaging="true" AllowSorting="true" TItem="Employee" ColumnWidth="200px" > <Columns> <RadzenDataGridColumn TItem="Employee" Property="ID" Title="ID" /> <RadzenDataGridColumn TItem="Employee" Property="Gender" Title="Gender" /> <RadzenDataGridColumn TItem="Employee" Property="Colors" Title="Colors" FilterOperator="FilterOperator.Custom" Type="typeof(string)" > <Template Context="item">@string.Join(",", item.Colors)</Template> </RadzenDataGridColumn> <RadzenDataGridColumn TItem="Employee" Property="Status" Title="Nullable Status" /> <RadzenDataGridColumn TItem="Employee" Property="Color" Title="Favorite Color (Display Attribute in Filter)" /> </Columns> </RadzenDataGrid> @code { int count; IEnumerable<Employee> initialEmployees; IEnumerable<Employee> employees; bool isLoading = false; public class Employee { public int ID { get; set; } public GenderType Gender { get; set; } public StatusType? Status { get; set; } public ColorType Color { get; set; } public List<string> Colors { get; set; } } public enum GenderType { Ms, Mr, Unknown, } public enum ColorType { Red, Green, Blue, [Display(Description = "Almond Green")] AlmondGreen, [Display(Description = "Amber Gray")] AmberGray, [Display(Description = "Apple Blue... ")] AppleBlueSeaGreen, //[Display(Description = "Miss", ResourceType = typeof(ResourceFile)] localization example [Display(Description = "Azure")] AzureBlue, } public enum StatusType { Inactive, Active, } protected override void OnInitialized() { initialEmployees = Enumerable.Range(0, 10).Select(i => new Employee { ID = i, Gender = i < 3 ? GenderType.Mr : i < 6 ? GenderType.Ms : GenderType.Unknown, Status = i < 3 ? StatusType.Active : i < 6 ? StatusType.Inactive : null, Color = i < 2 ? ColorType.Red: i < 4 ? ColorType.AlmondGreen : i < 6 ? ColorType.AppleBlueSeaGreen : ColorType.AzureBlue, Colors = i < 2 ? new List<string>() { "Red", "AmberGray", } : i < 4 ? new List<string>() { "AlmondGreen", "AmberGray", } : new List<string>() { "Red", "Green", "Blue", }, }); } void LoadData(LoadDataArgs args) { isLoading = true; var query = initialEmployees.AsQueryable(); if (!string.IsNullOrEmpty(args.Filter)) { query = query.Where(args.Filter); } var colorFilter = args.Filters.FirstOrDefault(f => f.FilterOperator == FilterOperator.Custom)?.FilterValue?.ToString(); if (!string.IsNullOrWhiteSpace(colorFilter)) { query = query.Where(e => e.Colors.Contains(colorFilter)); } if (!string.IsNullOrEmpty(args.OrderBy)) { query = query.OrderBy(args.OrderBy); } count = query.Count(); employees = query.Skip(args.Skip.Value).Take(args.Top.Value).ToList(); isLoading = false; } } ```
claunia added the pull-request label 2026-01-29 18:19:27 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/radzen-blazor#2573