|
| 1 | +--- |
| 2 | +title: Change Grid Search Results on Column Hide or Show |
| 3 | +description: Learn how to modify the Grid search results on the fly when the user hides or shows a column. |
| 4 | +type: how-to |
| 5 | +page_title: How to Change Grid Search Results on Column Hide or Show |
| 6 | +slug: grid-kb-search-match-visible-columns |
| 7 | +tags: telerik, blazor, grid, search |
| 8 | +ticketid: 1565592 |
| 9 | +res_type: kb |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +<table> |
| 15 | + <tbody> |
| 16 | + <tr> |
| 17 | + <td>Product</td> |
| 18 | + <td>Grid for Blazor, <br /> TreeList for Blazor</td> |
| 19 | + </tr> |
| 20 | + </tbody> |
| 21 | +</table> |
| 22 | + |
| 23 | +## Description |
| 24 | + |
| 25 | +This KB article answers the following questions: |
| 26 | + |
| 27 | +* How to change the Grid search results when the user hides or shows a string column? |
| 28 | +* How to refresh and match the Grid search results when the `Visible` attribute of a column changes? |
| 29 | +* How to hide or show search results in the Grid when the visibility of a column changes and a column is no longer displayed? |
| 30 | + |
| 31 | +## Solution |
| 32 | + |
| 33 | +1. Subscribe to the [Grid `OnStateChanged` event]({%slug grid-state%}#onstatechanged). |
| 34 | +1. Check if the `PropertyName` event argument is equal to `"ColumnStates"` to verify that the user has modified the column state. |
| 35 | +1. Check for `FilterDescriptor` instances in `args.GridState.SearchFilter.FilterDescriptors` to verify if a search is active. |
| 36 | +1. [Get the visible columns from `args.GridState.ColumnStates`.]({%slug grid-kb-column-state%}) Use only the columns with a `Field` that points to a `string` property. |
| 37 | +1. Compare the `Field` values of the visible string columns with the `Member` values of the search-related filter descriptors. |
| 38 | +1. Add or remove `FilterDescriptors` in `args.GridState.SearchFilter.FilterDescriptors` to align the search configuration with the currently visible columns. |
| 39 | +1. [Update the Grid state with `SetStateAsync()`]({%slug grid-state%}#methods). |
| 40 | + |
| 41 | +>caption Change search results when the user hides or shows a column |
| 42 | +
|
| 43 | +<div class="skip-repl"></div> |
| 44 | + |
| 45 | +````CSHTML |
| 46 | +@using Telerik.DataSource |
| 47 | +
|
| 48 | +<TelerikGrid @ref="@GridRef" |
| 49 | + Data="@GridData" |
| 50 | + TItem="@SampleModel" |
| 51 | + Pageable="true" |
| 52 | + Sortable="true" |
| 53 | + ShowColumnMenu="true" |
| 54 | + OnStateChanged="@OnGridStateChanged"> |
| 55 | + <GridToolBarTemplate> |
| 56 | + <GridSearchBox Placeholder="Type two identical letters" Width="200px" /> |
| 57 | + </GridToolBarTemplate> |
| 58 | + <GridColumns> |
| 59 | + <GridColumn Field="@nameof(SampleModel.Id)" Width="100px" VisibleInColumnChooser="false" /> |
| 60 | + <GridColumn Field="@nameof(SampleModel.Name)" /> |
| 61 | + <GridColumn Field="@nameof(SampleModel.Description)" /> |
| 62 | + </GridColumns> |
| 63 | +</TelerikGrid> |
| 64 | +
|
| 65 | +@code { |
| 66 | + private TelerikGrid<SampleModel>? GridRef { get; set; } |
| 67 | +
|
| 68 | + private List<SampleModel> GridData { get; set; } = new(); |
| 69 | +
|
| 70 | + // Non-string columns should not take part in the custom logic. |
| 71 | + private readonly List<string> GridStringFields = new List<string>() { nameof(SampleModel.Name), nameof(SampleModel.Description) }; |
| 72 | +
|
| 73 | + private async Task OnGridStateChanged(GridStateEventArgs<SampleModel> args) |
| 74 | + { |
| 75 | + // This will be true also for column resizing, reordering and locking. |
| 76 | + // Some additional checks exist below. |
| 77 | + if (args.PropertyName == "ColumnStates") |
| 78 | + { |
| 79 | + var searchFilterDescriptors = ((CompositeFilterDescriptor)args.GridState.SearchFilter).FilterDescriptors; |
| 80 | +
|
| 81 | + if (searchFilterDescriptors.Any()) |
| 82 | + { |
| 83 | + var searchValue = ((FilterDescriptor)searchFilterDescriptors.First()).Value; |
| 84 | + bool shouldRebindGrid = false; |
| 85 | +
|
| 86 | + var visibleStringColumnFields = new List<string>(); |
| 87 | + var filterDescriptorsToRemove = new List<IFilterDescriptor>(); |
| 88 | +
|
| 89 | + foreach (GridColumnState colState in args.GridState.ColumnStates) |
| 90 | + { |
| 91 | + if (!string.IsNullOrEmpty(colState.Field) && |
| 92 | + GridStringFields.Contains(colState.Field) && |
| 93 | + (!colState.Visible.HasValue || colState.Visible.Value)) |
| 94 | + { |
| 95 | + visibleStringColumnFields.Add(colState.Field); |
| 96 | + } |
| 97 | + } |
| 98 | +
|
| 99 | + // Find FilterDescriptors for hidden columns. |
| 100 | + foreach (FilterDescriptor fd in searchFilterDescriptors) |
| 101 | + { |
| 102 | + if (!visibleStringColumnFields.Contains(fd.Member)) |
| 103 | + { |
| 104 | + filterDescriptorsToRemove.Add(fd); |
| 105 | + } |
| 106 | + } |
| 107 | + // Remove FilterDescriptors for hidden columns. |
| 108 | + foreach (FilterDescriptor fd in filterDescriptorsToRemove) |
| 109 | + { |
| 110 | + searchFilterDescriptors.Remove(fd); |
| 111 | + shouldRebindGrid = true; |
| 112 | + } |
| 113 | +
|
| 114 | + // Add FilterDescriptors for newly shown columns. |
| 115 | + foreach (string field in visibleStringColumnFields) |
| 116 | + { |
| 117 | + if (!searchFilterDescriptors.Any(x => ((FilterDescriptor)x).Member == field)) |
| 118 | + { |
| 119 | + searchFilterDescriptors.Add(new FilterDescriptor() |
| 120 | + { |
| 121 | + Member = field, |
| 122 | + MemberType = typeof(string), |
| 123 | + Operator = FilterOperator.Contains, |
| 124 | + Value = searchValue |
| 125 | + }); |
| 126 | + shouldRebindGrid = true; |
| 127 | + } |
| 128 | + } |
| 129 | +
|
| 130 | + // Apply the changes in args.GridState.SearchFilter and rebind the Grid. |
| 131 | + if (shouldRebindGrid) |
| 132 | + { |
| 133 | + await GridRef!.SetStateAsync(args.GridState); |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | +
|
| 139 | + protected override void OnInitialized() |
| 140 | + { |
| 141 | + for (int i = 1; i <= 50; i++) |
| 142 | + { |
| 143 | + char nameCharCode = (char)(64 + i % 26 + 1); |
| 144 | + char descriptionCharCode = (char)(91 - i % 26 - 1); |
| 145 | +
|
| 146 | + GridData.Add(new SampleModel() |
| 147 | + { |
| 148 | + Id = i, |
| 149 | + Name = $"Name {i} {nameCharCode}{nameCharCode}", |
| 150 | + Description = $"Description {i} {descriptionCharCode}{descriptionCharCode}" |
| 151 | + }); |
| 152 | + } |
| 153 | + } |
| 154 | +
|
| 155 | + public class SampleModel |
| 156 | + { |
| 157 | + public int Id { get; set; } |
| 158 | + public string Name { get; set; } = string.Empty; |
| 159 | + public string Description { get; set; } = string.Empty; |
| 160 | + } |
| 161 | +} |
| 162 | +```` |
| 163 | + |
| 164 | +## See Also |
| 165 | + |
| 166 | +* [Search in Hidden Grid Columns]({%slug grid-kb-search-in-hidden-fields%}) |
| 167 | +* [Grid State]({%slug grid-state%}) |
| 168 | +* [Grid SearchBox]({%slug grid-searchbox%}) |
0 commit comments