|
| 1 | +--- |
| 2 | +title: Filter Icon Not Highlighted |
| 3 | +description: This article demonstrates how to fix the filter menu icon not being highlighted when filters are applied programmatically. |
| 4 | +type: troubleshooting |
| 5 | +page_title: How to Highlight Filter Icon in a Blazor Grid with Pre-applied Filters |
| 6 | +slug: grid-highlight-filter-icon |
| 7 | +tags: grid, blazor, filter, highlight, initialization, compositefilterdescriptor |
| 8 | +res_type: kb |
| 9 | +ticketid: 1668133 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | + <tbody> |
| 16 | + <tr> |
| 17 | + <td>Product</td> |
| 18 | + <td>Grid for Blazor</td> |
| 19 | + <td>TreeList for Blazor</td> |
| 20 | + </tr> |
| 21 | + </tbody> |
| 22 | +</table> |
| 23 | + |
| 24 | +## Description |
| 25 | + |
| 26 | +When using the TelerikGrid for Blazor, applying a filter programmatically on initialization does not highlight the header filter icon. This behavior is expected when filters are manually applied by the user, indicating which columns are currently filtered. |
| 27 | + |
| 28 | +## Cause |
| 29 | + |
| 30 | +The issue arises due to the direct use of `FilterDescriptor` without wrapping it in a `CompositeFilterDescriptor`. The `CompositeFilterDescriptor` is necessary to group individual filters and ensure the UI reflects the applied filters correctly. |
| 31 | + |
| 32 | +## Solution |
| 33 | + |
| 34 | +To highlight the filter menu icon upon initialization, wrap the filter definitions in a `CompositeFilterDescriptor`. This approach ensures the Grid's UI accurately displays which filters are active. |
| 35 | + |
| 36 | +Below is an example demonstrating how to initialize a Grid with a predefined filter on the "Released" column that highlights the filter icon correctly: |
| 37 | + |
| 38 | +```RAZOR |
| 39 | +@using Telerik.DataSource |
| 40 | +
|
| 41 | +<TelerikGrid Data="@GridData" |
| 42 | + Pageable="true" |
| 43 | + PageSize="5" |
| 44 | + Sortable="true" |
| 45 | + FilterMode="@GridFilterMode.FilterMenu" |
| 46 | + Groupable="true" |
| 47 | + OnStateInit="@( (GridStateEventArgs<Product> args) => OnGridStateInit(args) )"> |
| 48 | + <GridColumns> |
| 49 | + <GridColumn Field="@nameof(Product.Name)" /> |
| 50 | + <GridColumn Field="@nameof(Product.Released)" /> |
| 51 | + <GridColumn Field="@nameof(Product.Discontinued)" /> |
| 52 | + </GridColumns> |
| 53 | +</TelerikGrid> |
| 54 | +
|
| 55 | +@code { |
| 56 | + private List<Product> GridData { get; set; } |
| 57 | +
|
| 58 | + private async Task OnGridStateInit(GridStateEventArgs<Product> args) |
| 59 | + { |
| 60 | + var discontinuedColumnFilter = new CompositeFilterDescriptor() |
| 61 | + { |
| 62 | + FilterDescriptors = new FilterDescriptorCollection() { |
| 63 | + new FilterDescriptor() |
| 64 | + { |
| 65 | + Member = "Released", |
| 66 | + Operator = FilterOperator.IsLessThan, |
| 67 | + Value = DateTime.Today, |
| 68 | + MemberType = typeof(DateTime) |
| 69 | + } |
| 70 | + } |
| 71 | + }; |
| 72 | + args.GridState.FilterDescriptors.Add(discontinuedColumnFilter); |
| 73 | + } |
| 74 | +
|
| 75 | + protected override void OnInitialized() |
| 76 | + { |
| 77 | + GridData = new List<Product>(); |
| 78 | + var rnd = new Random(); |
| 79 | +
|
| 80 | + for (int i = 1; i <= 12; i++) |
| 81 | + { |
| 82 | + GridData.Add(new Product() |
| 83 | + { |
| 84 | + Id = i, |
| 85 | + Name = $"Product {i}", |
| 86 | + Released = DateTime.Now.AddDays(-rnd.Next(1, 365)).AddYears(-rnd.Next(1, 10)).Date, |
| 87 | + Discontinued = i % 3 == 0 |
| 88 | + }); |
| 89 | + } |
| 90 | + } |
| 91 | +
|
| 92 | + public class Product |
| 93 | + { |
| 94 | + public int Id { get; set; } |
| 95 | + public string Name { get; set; } |
| 96 | + public DateTime Released { get; set; } |
| 97 | + public bool Discontinued { get; set; } |
| 98 | + } |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +## See Also |
| 103 | + |
| 104 | +- [Grid Overview](https://docs.telerik.com/blazor-ui/components/grid/overview) |
| 105 | +- [Grid OnStateInit Event](https://docs.telerik.com/blazor-ui/components/grid/state#onstateinit) |
0 commit comments